]> git.ozlabs.org Git - ccan/blob - ccan/rbtree/rbtree.h
compiler, talloc, tap, tdb2: use #if instead of #ifdef.
[ccan] / ccan / rbtree / rbtree.h
1 /*
2    a talloc based red-black tree
3
4    Copyright (C) Ronnie Sahlberg  2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef CCAN_RBTREE_H
20 #define CCAN_RBTREE_H
21 #include <stdint.h>
22 #include <ccan/talloc/talloc.h>
23
24 #define TRBT_RED                0x00
25 #define TRBT_BLACK              0x01
26 typedef struct trbt_node {
27         struct trbt_tree *tree;
28         struct trbt_node *parent;
29         struct trbt_node *left;
30         struct trbt_node *right;
31         uint32_t rb_color;
32         uint32_t key32;
33         void *data;
34 } trbt_node_t;
35
36 typedef struct trbt_tree {
37         trbt_node_t *root;
38 /* automatically free the tree when the last node has been deleted */
39 #define TRBT_AUTOFREE           0x00000001
40         uint32_t flags;
41 } trbt_tree_t;
42
43
44
45 /* Create a RB tree */
46 trbt_tree_t *trbt_create(TALLOC_CTX *memctx, uint32_t flags);
47
48 /* Lookup a node in the tree and return a pointer to data or NULL */
49 void *trbt_lookup32(trbt_tree_t *tree, uint32_t key);
50
51 /* Insert a new node into the tree. If there was already a node with this
52    key the pointer to the previous data is returned.
53    The tree will talloc_steal() the data inserted into the tree .
54 */
55 void *trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data);
56
57 /* Insert a new node into the tree.
58    If this is a new node:
59      callback is called with data==NULL and param=param
60      the returned value from the callback is talloc_stolen and inserted in the
61      tree.
62    If a node already exists for this key then:
63      callback is called with data==existing data and param=param
64      the returned calue is talloc_stolen and inserted in the tree
65 */
66 void trbt_insert32_callback(trbt_tree_t *tree, uint32_t key, void *(*callback)(void *param, void *data), void *param);
67
68 /* Delete a node from the tree and free all data associated with it */
69 void trbt_delete32(trbt_tree_t *tree, uint32_t key);
70
71
72 /* insert into the tree with a key based on an array of uint32 */
73 void trbt_insertarray32_callback(trbt_tree_t *tree, uint32_t keylen, uint32_t *key, void *(*callback)(void *param, void *data), void *param);
74
75 /* Lookup a node in the tree with a key based on an array of uint32
76    and return a pointer to data or NULL */
77 void *trbt_lookuparray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key);
78
79 /* Traverse a tree with a key based on an array of uint32 */
80 void trbt_traversearray32(trbt_tree_t *tree, uint32_t keylen, void (*callback)(void *param, void *data), void *param);
81
82 /* Lookup the first node in the tree with a key based on an array of uint32
83    and return a pointer to data or NULL */
84 void *trbt_findfirstarray32(trbt_tree_t *tree, uint32_t keylen);
85
86 #endif /* CCAN_RBTREE_H */