]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
add a function to return the first entry that is stored in a tree where
authorRonnie Sahlberg <sahlberg@ronnie>
Wed, 15 Aug 2007 00:57:21 +0000 (10:57 +1000)
committerRonnie Sahlberg <sahlberg@ronnie>
Wed, 15 Aug 2007 00:57:21 +0000 (10:57 +1000)
the key is an array of uint32_t

(This used to be ctdb commit 99553397aade4f1c4d17ef14dad406934958c80a)

ctdb/common/rb_tree.c
ctdb/common/rb_tree.h
ctdb/tests/rb_test.c

index 9876f654a322ee4869f5d5fe506fbcd70cfdfb71..d541e38ce63ec402370d2ec1382bbab722931656 100644 (file)
@@ -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)
 {
index 09f73e649bfc579ff5611717bd7966006b64e521..cb7cba3bcc54add2d8c9e6d3507957034d6ffb83 100644 (file)
@@ -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);
index 9f39e5b100549b4620cdd1f84f2d3a5d0f2d7d51..e057033172cf0d859ce64ba1cc1f2eb4c111095a 100644 (file)
@@ -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");