rbtree.c: rbtree_walk: pass rbnode->key instead of rbnode to callback function
This commit is contained in:
parent
2c54173a52
commit
0fba36440c
4 changed files with 8 additions and 7 deletions
|
|
@ -83,7 +83,8 @@ NOTE: The memory pointed to by the 'key' pointer is *not* copied so you must ens
|
||||||
|
|
||||||
`rbtree_walk()`::
|
`rbtree_walk()`::
|
||||||
|
|
||||||
Walks the tree in in-order and calls the 'action' callback function for every node.
|
Walks the tree in-order and passes a pointer to `rbnode->key`
|
||||||
|
for the given node to the 'action' callback function.
|
||||||
|
|
||||||
`rbtree_search()`::
|
`rbtree_search()`::
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,13 +50,13 @@ static void node_dealloc(rbnode *n, void (*dealloc)(void *)) {
|
||||||
/*
|
/*
|
||||||
* Recursivly walks a tree, applying action function on every node
|
* Recursivly walks a tree, applying action function on every node
|
||||||
*/
|
*/
|
||||||
static void rbwalk(rbnode *n, void (*action)(rbnode *)) {
|
static void rbwalk(rbnode *n, void (*action)(const void *)) {
|
||||||
|
|
||||||
if (n == NULL)
|
if (n == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
rbwalk(n->child[0], action);
|
rbwalk(n->child[0], action);
|
||||||
action(n);
|
action(n->key);
|
||||||
rbwalk(n->child[1], action);
|
rbwalk(n->child[1], action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,7 +106,7 @@ void* rbtree_search(rbtree *tree, const void *key) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rbtree_walk(rbtree *tree, void (*action)(rbnode *)) {
|
void rbtree_walk(rbtree *tree, void (*action)(const void *)) {
|
||||||
|
|
||||||
if (tree == NULL || action == NULL)
|
if (tree == NULL || action == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ int rbtree_is_empty(rbtree *tree);
|
||||||
|
|
||||||
void* rbtree_search(rbtree *tree, const void *key);
|
void* rbtree_search(rbtree *tree, const void *key);
|
||||||
|
|
||||||
void rbtree_walk(rbtree *tree, void (*action)(rbnode *));
|
void rbtree_walk(rbtree *tree, void (*action)(const void *));
|
||||||
|
|
||||||
void rbtree_free(rbtree *tree);
|
void rbtree_free(rbtree *tree);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,14 +94,14 @@ static int keyref_exists(int low, int high, int value) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void walk_fn(rbnode *n) {
|
static void walk_fn(const void *key) {
|
||||||
|
|
||||||
static int i = 0;
|
static int i = 0;
|
||||||
|
|
||||||
while(keyref_exists(0, i-1, keyref[i]))
|
while(keyref_exists(0, i-1, keyref[i]))
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
assert(keyref[i] == *((int*)n->key));
|
assert(keyref[i] == *((int*)key));
|
||||||
|
|
||||||
if (++i > NODES)
|
if (++i > NODES)
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
|
||||||
Reference in a new issue