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:
Bailey Thompson
2019-06-09 00:30:30 -04:00
committed by GitHub
parent 2b75351f8e
commit 8d45509d68
3 changed files with 36 additions and 8 deletions

View File

@@ -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();