From e6f60618198ce0cf9cc17a2b3064eef25d28936d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 27 Feb 2011 11:49:01 -0800 Subject: [PATCH] quicklist.replace makes changes to the content --- src/util/quicklist.c | 10 ++++++++++ src/util/quicklist.h | 1 + src/util/quicklist_test.c | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/util/quicklist.c b/src/util/quicklist.c index 215bb0065..92eb38c08 100644 --- a/src/util/quicklist.c +++ b/src/util/quicklist.c @@ -33,6 +33,16 @@ void * ql_get(const quicklist * ql, int index) { return (ql && indexnum_elements)?ql->elements[index]:ql_get(ql->next, index-ql->num_elements); } +void * ql_replace(quicklist * ql, int index, void * data) { + if (ql && indexnum_elements) { + void * orig = ql->elements[index]; + ql->elements[index] = data; + return orig; + } else { + return ql_replace(ql->next, index-ql->num_elements, data); + } +} + int ql_length(const quicklist * ql) { return ql?ql->num_elements+ql_length(ql->next):0; } diff --git a/src/util/quicklist.h b/src/util/quicklist.h index f3068de48..91eb55468 100644 --- a/src/util/quicklist.h +++ b/src/util/quicklist.h @@ -31,6 +31,7 @@ int ql_insert(struct quicklist ** qlp, int index, void * data); void ql_foreach(struct quicklist * ql, void (*cb)(void *)); int ql_advance(struct quicklist ** iterator, int * index, int stride); void ql_free(struct quicklist * ql); +void * ql_replace(struct quicklist * ql, int index, void * data); /* you can use it as a set (sorted pointers)*/ int ql_set_insert(struct quicklist ** qlp, void * data); diff --git a/src/util/quicklist_test.c b/src/util/quicklist_test.c index 8dd677405..5592599f8 100644 --- a/src/util/quicklist_test.c +++ b/src/util/quicklist_test.c @@ -24,6 +24,15 @@ static void test_insert_delete_gives_null(CuTest * tc) { CuAssertPtrEquals(tc, 0, ql); } +static void test_replace(CuTest * tc) { + struct quicklist * ql = NULL; + int a; + ql_push(&ql, (void*)42); + a = (int)ql_replace(ql, 0, (void *)43); + CuAssertIntEquals(tc, 42, a); + CuAssertIntEquals(tc, 43, (int)ql_get(ql, 0)); +} + static void test_set_insert(CuTest * tc) { struct quicklist * ql = NULL; int a, qi; @@ -167,6 +176,7 @@ CuSuite* get_quicklist_suite(void) { CuSuite* suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_advance); + SUITE_ADD_TEST(suite, test_replace); SUITE_ADD_TEST(suite, test_push); SUITE_ADD_TEST(suite, test_insert); SUITE_ADD_TEST(suite, test_set_remove);