Electroneum
rbtree.h
Go to the documentation of this file.
1 /*
2  * rbtree.h -- generic red-black tree
3  *
4  * Copyright (c) 2001-2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
43 #ifndef UTIL_RBTREE_H_
44 #define UTIL_RBTREE_H_
45 
51 typedef struct rbnode_type rbnode_type;
55 struct rbnode_type {
63  const void *key;
66 };
67 
69 #define RBTREE_NULL &rbtree_null_node
70 
72 
74 typedef struct rbtree_type rbtree_type;
76 struct rbtree_type {
79 
81  size_t count;
82 
87  int (*cmp) (const void *, const void *);
88 };
89 
95 rbtree_type *rbtree_create(int (*cmpf)(const void *, const void *));
96 
102 void rbtree_init(rbtree_type *rbtree, int (*cmpf)(const void *, const void *));
103 
111 
119 rbnode_type *rbtree_delete(rbtree_type *rbtree, const void *key);
120 
127 rbnode_type *rbtree_search(rbtree_type *rbtree, const void *key);
128 
138 int rbtree_find_less_equal(rbtree_type *rbtree, const void *key,
139  rbnode_type **result);
140 
147 
154 
161 
168 
173 #define RBTREE_FOR(node, type, rbtree) \
174  for(node=(type)rbtree_first(rbtree); \
175  (rbnode_type*)node != RBTREE_NULL; \
176  node = (type)rbtree_next((rbnode_type*)node))
177 
189 void traverse_postorder(rbtree_type* tree, void (*func)(rbnode_type*, void*),
190  void* arg);
191 
192 #endif /* UTIL_RBTREE_H_ */
rbnode_type rbtree_null_node
rbnode_type * rbtree_next(rbnode_type *rbtree)
int(* cmp)(const void *, const void *)
Definition: rbtree.h:87
rbnode_type * rbtree_previous(rbnode_type *rbtree)
rbnode_type * rbtree_first(rbtree_type *rbtree)
const char * key
Definition: hmac_keccak.cpp:39
rbnode_type * parent
Definition: rbtree.h:57
size_t count
Definition: rbtree.h:81
unsigned char uint8_t
Definition: stdint.h:124
void traverse_postorder(rbtree_type *tree, void(*func)(rbnode_type *, void *), void *arg)
rbnode_type * right
Definition: rbtree.h:61
const void * key
Definition: rbtree.h:63
rbnode_type * left
Definition: rbtree.h:59
rbnode_type * rbtree_search(rbtree_type *rbtree, const void *key)
rbtree_type * rbtree_create(int(*cmpf)(const void *, const void *))
rbnode_type * rbtree_last(rbtree_type *rbtree)
rbnode_type * rbtree_insert(rbtree_type *rbtree, rbnode_type *data)
uint8_t color
Definition: rbtree.h:65
void rbtree_init(rbtree_type *rbtree, int(*cmpf)(const void *, const void *))
rbnode_type * rbtree_delete(rbtree_type *rbtree, const void *key)
rbnode_type * root
Definition: rbtree.h:78
int rbtree_find_less_equal(rbtree_type *rbtree, const void *key, rbnode_type **result)