Code

avltree: Add sdb_avltree_iter_peek_next().
authorSebastian Harl <sh@tokkee.org>
Thu, 5 Mar 2015 07:13:42 +0000 (08:13 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 5 Mar 2015 07:13:42 +0000 (08:13 +0100)
The function returns the next element without advancing the iterator.

src/include/utils/avltree.h
src/utils/avltree.c
t/unit/utils/avltree_test.c

index de51c7e07dfa007f020a6f6c53017ad55171588c..ba495c665ff6e884c2e6d6efdf308b5fc3c1ec79 100644 (file)
@@ -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.
index 54c1895673a8dd68e6aeccf4655615db00078930..87db2f78d0e9b8e7c1eee042af3f32de0c697887 100644 (file)
@@ -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)
 {
index 9be5e0960821626be4e842350da7314723306015..433f67d7ea42d89151aa0b4f4336696f2e7a7213 100644 (file)
@@ -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(<iter[%zu]>) = false; "
                                "expected: true", i);
 
+               expected_obj = sdb_avltree_iter_peek_next(iter);
+               fail_unless(expected_obj != NULL,
+                               "sdb_avltree_iter_peek_next(<iter[%zu]>) = NULL; "
+                               "expected: <obj>", i);
+
                obj = sdb_avltree_iter_get_next(iter);
                fail_unless(obj != NULL,
                                "sdb_avltree_iter_get_next(<iter[%zu]>) = 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(<iter[%zu]>) = %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(<iter>) = true; "
                        "expected: false");
+       obj = sdb_avltree_iter_peek_next(iter);
+       fail_unless(obj == NULL,
+                       "sdb_avltree_iter_peek_next(<iter>) = <obj>; expected: NULL");
        obj = sdb_avltree_iter_get_next(iter);
        fail_unless(obj == NULL,
                        "sdb_avltree_iter_get_next(<iter>) = <obj>; expected: NULL");