mirror of
https://github.com/bkthomps/Containers.git
synced 2026-03-27 13:29:52 +00:00
Forward list optimization (#53)
Add a tail pointer which is either NULL or points to the back of the list. This improves the performance when adding to the back of the list many times in a row.
This commit is contained in:
@@ -150,6 +150,25 @@ static void test_basic(void)
|
||||
assert(!forward_list_destroy(me));
|
||||
}
|
||||
|
||||
static void test_add_back(void)
|
||||
{
|
||||
int i;
|
||||
forward_list me = forward_list_init(sizeof(int));
|
||||
assert(me);
|
||||
for (i = 1; i < 10000; i++) {
|
||||
int get = 0xdeadbeef;
|
||||
forward_list_add_last(me, &i);
|
||||
forward_list_get_last(&get, me);
|
||||
assert(get == i);
|
||||
if (i % 5 == 0) {
|
||||
forward_list_remove_last(me);
|
||||
forward_list_get_last(&get, me);
|
||||
assert(get == i - 1);
|
||||
}
|
||||
}
|
||||
assert(!forward_list_destroy(me));
|
||||
}
|
||||
|
||||
static void test_init_out_of_memory(void)
|
||||
{
|
||||
fail_malloc = 1;
|
||||
@@ -264,6 +283,7 @@ void test_forward_list(void)
|
||||
{
|
||||
test_invalid_init();
|
||||
test_basic();
|
||||
test_add_back();
|
||||
test_init_out_of_memory();
|
||||
test_add_first_out_of_memory();
|
||||
test_add_at_out_of_memory();
|
||||
|
||||
Reference in New Issue
Block a user