add helpers to add/lookup/delete nodes in a tree where the key is an
[vlendec/samba-autobuild/.git] / ctdb / common / rb_tree.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
20
21
22 #define TRBT_RED                0x00
23 #define TRBT_BLACK              0x01
24 typedef struct _trbt_node_t {
25         struct _trbt_tree_t *tree;
26         struct _trbt_node_t *parent;
27         struct _trbt_node_t *left;
28         struct _trbt_node_t *right;
29         uint32_t rb_color;
30         uint32_t key32;
31         void *data;
32 } trbt_node_t;
33
34 typedef struct _trbt_tree_t {
35         trbt_node_t *root;
36 } trbt_tree_t;
37
38
39
40 /* Create a RB tree */
41 trbt_tree_t *trbt_create(TALLOC_CTX *memctx);
42
43 /* Lookup a node in the tree and return a pointer to data or NULL */
44 void *trbt_lookup32(trbt_tree_t *tree, uint32_t key);
45
46 /* Insert a new node into the tree. If there was already a node with this
47    key the pointer to the previous data is returned.
48    The tree will talloc_steal() the data inserted into the tree .
49 */
50 void *trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data);
51
52 /* Insert a new node into the tree.
53    If this is a new node:
54      callback is called with data==NULL and param=param
55      the returned value from the callback is talloc_stolen and inserted in the
56      tree.
57    If a node already exists for this key then:
58      callback is called with data==existing data and param=param
59      the returned calue is talloc_stolen and inserted in the tree
60 */
61 void trbt_insert32_callback(trbt_tree_t *tree, uint32_t key, void *(*callback)(void *param, void *data), void *param);
62
63 /* Delete a node from the tree and free all data associated with it */
64 void trbt_delete32(trbt_tree_t *tree, uint32_t key);
65
66
67 /* insert into the tree with a key based on an array of uint32 */
68 void trbt_insertarray32_callback(trbt_tree_t *tree, uint32_t keylen, uint32_t *key, void *(*callback)(void *param, void *data), void *param);
69
70 /* Lookup a node in the tree with a key based on an array of uint32 
71    and return a pointer to data or NULL */
72 void *trbt_lookuparray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key);
73
74 /* Delete a node in the tree with a key based on an array of uint32 
75    and return a pointer to data or NULL */
76 void trbt_deletearray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key);