summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 203956f)
raw | patch | inline | side by side (parent: 203956f)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 11 Jul 2014 06:06:45 +0000 (08:06 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 11 Jul 2014 06:06:45 +0000 (08:06 +0200) |
We don't need a custom compare function (for now) and correctly passing in a
const string when comparing based on objects is annoying to get right.
const string when comparing based on objects is annoying to get right.
src/include/utils/avltree.h | patch | blob | history | |
src/utils/avltree.c | patch | blob | history | |
t/unit/utils/avltree_test.c | patch | blob | history |
index 7e70758e215de2ae622f0dc6cbf9fe7597c7f04b..28efbdfb23d6590882f1a8f35cbba88f59ef766e 100644 (file)
/*
* sdb_avltree_create:
- * Creates an AVL tree. Insert and lookup operations will use the specified
- * compare function to determine the location of an object in the tree. If no
- * function has been specified, it defaults to sdb_object_cmp_by_name, that
- * is, objects will be compared by their names.
+ * Creates an AVL tree. Objects will be compared by their names.
*/
sdb_avltree_t *
-sdb_avltree_create(sdb_object_cmp_cb cmp);
+sdb_avltree_create(void);
/*
* sdb_avltree_destroy:
/*
* sdb_avltree_insert:
- * Insert a new node into the tree (using the tree's compare function to find
- * the right location). Each object must be unique (based on the compare
- * function). This operation may change the structure of the tree by
- * rebalancing subtrees which no longer comply with the rules of AVL.
+ * Insert a new node into the tree. Each object must be unique. This operation
+ * may change the structure of the tree by rebalancing subtrees which no
+ * longer comply with the rules of AVL.
*
* Returns:
* - 0 on success
/*
* sdb_avltree_lookup:
- * Lookup an object from a tree. The object matching the specified reference
- * object (using the tree's compare function) will be returned.
+ * Lookup an object from a tree by name.
*
* Returns:
* - the requested object
* - NULL if no such object exists
*/
sdb_object_t *
-sdb_avltree_lookup(sdb_avltree_t *tree, const sdb_object_t *ref);
+sdb_avltree_lookup(sdb_avltree_t *tree, const char *name);
/*
* sdb_avltree_get_iter, sdb_avltree_iter_has_next, sdb_avltree_iter_get_next,
* sdb_avltree_iter_destroy:
* Iterate through all nodes of the tree. The iterator will start at the
- * smallest element (based on the tree's compare function) and then iterate
- * through the sorted sequence of all nodes.
+ * smallest element (based on the name) and then iterate through the sorted
+ * sequence of all nodes.
*
* sdb_avltree_iter_get_next returns NULL if there is no next element.
*/
diff --git a/src/utils/avltree.c b/src/utils/avltree.c
index 56bc57d3087b69bccc42c20fde8cdfcd835f8162..7397a34fdbb56dfd54d4db07c82f797cf3d93b76 100644 (file)
--- a/src/utils/avltree.c
+++ b/src/utils/avltree.c
#include <assert.h>
#include <stdlib.h>
+#include <string.h>
#include <pthread.h>
/*
struct sdb_avltree {
pthread_rwlock_t lock;
- sdb_object_cmp_cb cmp;
-
node_t *root;
size_t size;
};
*/
sdb_avltree_t *
-sdb_avltree_create(sdb_object_cmp_cb cmp)
+sdb_avltree_create(void)
{
sdb_avltree_t *tree;
if (! tree)
return NULL;
- if (! cmp)
- cmp = sdb_object_cmp_by_name;
-
pthread_rwlock_init(&tree->lock, /* attr = */ NULL);
- tree->cmp = cmp;
tree->root = NULL;
tree->size = 0;
while (42) {
assert(parent);
- diff = tree->cmp(obj, parent->obj);
+ diff = strcasecmp(obj->name, parent->obj->name);
if (! diff) {
node_destroy(n);
pthread_rwlock_unlock(&tree->lock);
} /* sdb_avltree_insert */
sdb_object_t *
-sdb_avltree_lookup(sdb_avltree_t *tree, const sdb_object_t *ref)
+sdb_avltree_lookup(sdb_avltree_t *tree, const char *name)
{
node_t *n;
n = tree->root;
while (n) {
- int diff = tree->cmp(n->obj, ref);
+ int diff = strcasecmp(n->obj->name, name);
if (! diff) {
sdb_object_ref(n->obj);
index 3526e4107df078c5fa2f3aa99913f25659bccbf3..4f6933acf6acb94e185acf0f39fd3c8e55a48b77 100644 (file)
static void
setup(void)
{
- tree = sdb_avltree_create(NULL);
+ tree = sdb_avltree_create();
fail_unless(tree != NULL,
"sdb_avltree_create() = NULL; expected AVL-tree object");
} /* setup */
populate();
for (i = 0; i < SDB_STATIC_ARRAY_LEN(test_data); ++i) {
- sdb_object_t ref = SDB_OBJECT_STATIC(test_data[i].name);
sdb_object_t *obj;
- obj = sdb_avltree_lookup(tree, &ref);
+ obj = sdb_avltree_lookup(tree, test_data[i].name);
fail_unless(obj != NULL,
- "sdb_avltree_lookup(<tree>, <%s>) = NULL; "
- "expected: <obj>", ref.name);
+ "sdb_avltree_lookup(<tree>, %s) = NULL; "
+ "expected: <obj>", test_data[i].name);
fail_unless(obj == &test_data[i],
- "sdb_avltree_lookup(<tree>, <%s>) = %p (%s); "
- "expected: %p, (%s)", ref.name, obj, obj->name,
+ "sdb_avltree_lookup(<tree>, %s) = %p (%s); "
+ "expected: %p, (%s)", test_data[i].name, obj, obj->name,
&test_data[i], test_data[i].name);
}
for (i = 0; i < SDB_STATIC_ARRAY_LEN(unused_names); ++i) {
- sdb_object_t ref = SDB_OBJECT_STATIC(unused_names[i]);
sdb_object_t *obj;
- obj = sdb_avltree_lookup(tree, &ref);
+ obj = sdb_avltree_lookup(tree, unused_names[i]);
fail_unless(obj == NULL,
- "sdb_avltree_lookup(<tree>, <%s> = %p (%s); "
- "expected: NULL", ref.name, obj, obj ? obj->name : "<nil>");
+ "sdb_avltree_lookup(<tree>, %s) = %p (%s); "
+ "expected: NULL", unused_names[i],
+ obj, obj ? obj->name : "<nil>");
}
}
END_TEST