From: Sebastian Harl Date: Thu, 5 Mar 2015 07:13:42 +0000 (+0100) Subject: avltree: Add sdb_avltree_iter_peek_next(). X-Git-Tag: sysdb-0.8.0~139 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ff6cf34ee7a8db747d8284b5c5532f8d13b7cea5;p=sysdb.git avltree: Add sdb_avltree_iter_peek_next(). The function returns the next element without advancing the iterator. --- diff --git a/src/include/utils/avltree.h b/src/include/utils/avltree.h index de51c7e..ba495c6 100644 --- a/src/include/utils/avltree.h +++ b/src/include/utils/avltree.h @@ -113,6 +113,18 @@ sdb_avltree_iter_has_next(sdb_avltree_iter_t *iter); sdb_object_t * sdb_avltree_iter_get_next(sdb_avltree_iter_t *iter); +/* + * sdb_avltree_iter_peek_next: + * Peek at the next node, if there is one. This is similar to has_next() but + * it returns the actual next element without advancing the iterator. + * + * Returns: + * - the next node, if there is one + * - NULL else + */ +sdb_object_t * +sdb_avltree_iter_peek_next(sdb_avltree_iter_t *iter); + /* * sdb_avltree_size: * Returns the number of nodes in the tree. diff --git a/src/utils/avltree.c b/src/utils/avltree.c index 54c1895..87db2f7 100644 --- a/src/utils/avltree.c +++ b/src/utils/avltree.c @@ -457,6 +457,14 @@ sdb_avltree_iter_get_next(sdb_avltree_iter_t *iter) return n ? n->obj : NULL; } /* sdb_avltree_iter_get_next */ +sdb_object_t * +sdb_avltree_iter_peek_next(sdb_avltree_iter_t *iter) +{ + if ((! iter) || (! iter->node)) + return NULL; + return iter->node->obj; +} /* sdb_avltree_iter_peek_next */ + size_t sdb_avltree_size(sdb_avltree_t *tree) { diff --git a/t/unit/utils/avltree_test.c b/t/unit/utils/avltree_test.c index 9be5e09..433f67d 100644 --- a/t/unit/utils/avltree_test.c +++ b/t/unit/utils/avltree_test.c @@ -209,11 +209,17 @@ START_TEST(test_iter) for (i = 0; i < SDB_STATIC_ARRAY_LEN(test_data); ++i) { char expected_name[] = { (char)('a' + (int)i), '\0' }; + sdb_object_t *expected_obj; _Bool c = sdb_avltree_iter_has_next(iter); fail_unless(c, "sdb_avltree_iter_has_next() = false; " "expected: true", i); + expected_obj = sdb_avltree_iter_peek_next(iter); + fail_unless(expected_obj != NULL, + "sdb_avltree_iter_peek_next() = NULL; " + "expected: ", i); + obj = sdb_avltree_iter_get_next(iter); fail_unless(obj != NULL, "sdb_avltree_iter_get_next() = NULL; " @@ -221,11 +227,18 @@ START_TEST(test_iter) fail_unless(!strcmp(obj->name, expected_name), "sdb_avltree_iter[%zu] = %s; expected: %s", i, obj->name, expected_name); + + fail_unless(obj == expected_obj, + "sdb_avltree_iter_get_next() = %p; " + "expected: %p (from peek())", i, obj, expected_obj); } check = sdb_avltree_iter_has_next(iter) != 0; fail_unless(check == 0, "sdb_avltree_iter_has_next() = true; " "expected: false"); + obj = sdb_avltree_iter_peek_next(iter); + fail_unless(obj == NULL, + "sdb_avltree_iter_peek_next() = ; expected: NULL"); obj = sdb_avltree_iter_get_next(iter); fail_unless(obj == NULL, "sdb_avltree_iter_get_next() = ; expected: NULL");