summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRonnie Sahlberg <sahlberg@ronnie>2007-08-15 10:57:21 +1000
committerRonnie Sahlberg <sahlberg@ronnie>2007-08-15 10:57:21 +1000
commite0957ba4a439096d171bd75ccf46ce5912613620 (patch)
treed02070f1ece10f09b4d2ba03ac122890b372ed8a
parent5a02262a06e29e1d2f5fda2865ae235fc79d2cc8 (diff)
downloadsamba-e0957ba4a439096d171bd75ccf46ce5912613620.tar.gz
samba-e0957ba4a439096d171bd75ccf46ce5912613620.tar.xz
samba-e0957ba4a439096d171bd75ccf46ce5912613620.zip
add a function to return the first entry that is stored in a tree where
the key is an array of uint32_t (This used to be ctdb commit 99553397aade4f1c4d17ef14dad406934958c80a)
-rw-r--r--ctdb/common/rb_tree.c37
-rw-r--r--ctdb/common/rb_tree.h4
-rw-r--r--ctdb/tests/rb_test.c1
3 files changed, 42 insertions, 0 deletions
diff --git a/ctdb/common/rb_tree.c b/ctdb/common/rb_tree.c
index 9876f654a3..d541e38ce6 100644
--- a/ctdb/common/rb_tree.c
+++ b/ctdb/common/rb_tree.c
@@ -962,6 +962,43 @@ trbt_traversearray32(trbt_tree_t *tree, uint32_t keylen,
}
+/* this function will return the first node in a tree where
+ the key is an array of uint32_t
+*/
+void *
+trbt_findfirstarray32(trbt_tree_t *tree, uint32_t keylen)
+{
+ trbt_node_t *node;
+
+ if (keylen < 1) {
+ return NULL;
+ }
+
+ if (tree == NULL) {
+ return NULL;
+ }
+
+ node=tree->root;
+ if (node == NULL) {
+ return NULL;
+ }
+
+ while (node->left) {
+ node = node->left;
+ }
+
+ /* we found our node so return the data */
+ if (keylen == 1) {
+ return node->data;
+ }
+
+ /* we are still traversing subtrees so find the first node in the
+ next level of trees
+ */
+ return trbt_findfirstarray32(node->data, keylen-1);
+}
+
+
#if 0
static void printtree(trbt_node_t *node, int levels)
{
diff --git a/ctdb/common/rb_tree.h b/ctdb/common/rb_tree.h
index 09f73e649b..cb7cba3bcc 100644
--- a/ctdb/common/rb_tree.h
+++ b/ctdb/common/rb_tree.h
@@ -76,3 +76,7 @@ void *trbt_lookuparray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key);
/* Traverse a tree with a key based on an array of uint32 */
void trbt_traversearray32(trbt_tree_t *tree, uint32_t keylen, void (*callback)(void *param, void *data), void *param);
+
+/* Lookup the first node in the tree with a key based on an array of uint32
+ and return a pointer to data or NULL */
+void *trbt_findfirstarray32(trbt_tree_t *tree, uint32_t keylen);
diff --git a/ctdb/tests/rb_test.c b/ctdb/tests/rb_test.c
index 9f39e5b100..e057033172 100644
--- a/ctdb/tests/rb_test.c
+++ b/ctdb/tests/rb_test.c
@@ -301,6 +301,7 @@ int main(int argc, const char *argv[])
printf("\niterations passed:%d\n", i);
trbt_traversearray32(tree, 3, random_traverse, NULL);
printf("\n");
+ printf("first node: %s\n", (char *)trbt_findfirstarray32(tree, 3));
printf("\ndeleting all entries\n");