EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
rbtree.h
1 /* etcpal/rbtree.h: C-language red-black tree implementation. .his module is
2  * based heavily on a public-domain red-black tree implementation. The license
3  * of that implementation follows as it was when retrieved from Github on
4  * 2018-02-28.
5  *
6  ******************************************************************************
7  * Based on Julienne Walker's <http://eternallyconfuzzled.com/> rb_tree
8  * implementation.
9  *
10  * Modified by Mirek Rusin <http://github.com/mirek/rb_tree>.
11  *
12  * This is free and unencumbered software released into the public domain.
13  *
14  * Anyone is free to copy, modify, publish, use, compile, sell, or
15  * distribute this software, either in source code form or as a compiled
16  * binary, for any purpose, commercial or non-commercial, and by any
17  * means.
18  *
19  * In jurisdictions that recognize copyright laws, the author or authors
20  * of this software dedicate any and all copyright interest in the
21  * software to the public domain. We make this dedication for the benefit
22  * of the public at large and to the detriment of our heirs and
23  * successors. We intend this dedication to be an overt act of
24  * relinquishment in perpetuity of all present and future rights to this
25  * software under copyright law.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  *
35  * For more information, please refer to <http://unlicense.org/>
36  */
37 
38 #ifndef ETCPAL_RBTREE_H_
39 #define ETCPAL_RBTREE_H_
40 
41 #include <stddef.h>
42 #include "etcpal/error.h"
43 
126 #ifndef ETCPAL_RB_ITER_MAX_HEIGHT
127 #define ETCPAL_RB_ITER_MAX_HEIGHT 64
128 #endif
129 
131 typedef struct EtcPalRbNode EtcPalRbNode;
132 typedef struct EtcPalRbTree EtcPalRbTree;
153 typedef int (*EtcPalRbTreeNodeCmpFunc)(const EtcPalRbTree* self, const void* value_a, const void* value_b);
154 
166 typedef void (*EtcPalRbTreeNodeFunc)(const EtcPalRbTree* self, EtcPalRbNode* node);
167 
177 typedef EtcPalRbNode* (*EtcPalRbNodeAllocFunc)(void);
178 
188 typedef void (*EtcPalRbNodeDeallocFunc)(EtcPalRbNode* node);
189 
196 {
197  int red;
199  void* value;
200 };
201 
208 {
211  size_t size;
214  void* info;
215 };
216 
222 typedef struct EtcPalRbIter
223 {
227  size_t top;
228  void* info;
230 
231 #ifdef __cplusplus
232 extern "C" {
233 #endif
234 
235 int etcpal_rbtree_node_cmp_ptr_cb(const EtcPalRbTree* self, const void* a, const void* b);
237 
238 EtcPalRbNode* etcpal_rbnode_init(EtcPalRbNode* self, void* value);
239 
241  EtcPalRbTreeNodeCmpFunc node_cmp_cb,
242  EtcPalRbNodeAllocFunc alloc_f,
243  EtcPalRbNodeDeallocFunc dealloc_f);
244 void* etcpal_rbtree_find(EtcPalRbTree* self, const void* value);
246 etcpal_error_t etcpal_rbtree_remove(EtcPalRbTree* self, const void* value);
248 size_t etcpal_rbtree_size(EtcPalRbTree* self);
249 
253 
255 
257 void* etcpal_rbiter_first(EtcPalRbIter* self, EtcPalRbTree* tree);
258 void* etcpal_rbiter_last(EtcPalRbIter* self, EtcPalRbTree* tree);
259 void* etcpal_rbiter_next(EtcPalRbIter* self);
260 void* etcpal_rbiter_prev(EtcPalRbIter* self);
261 void* etcpal_rbiter_lower_bound(EtcPalRbIter* self, EtcPalRbTree* tree, const void* value);
262 void* etcpal_rbiter_upper_bound(EtcPalRbIter* self, EtcPalRbTree* tree, const void* value);
263 
264 #ifdef __cplusplus
265 }
266 #endif
267 
272 #endif /* ETCPAL_RBTREE_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
etcpal_error_t etcpal_rbtree_insert_node(EtcPalRbTree *self, EtcPalRbNode *node)
Insert a node containing a new value into a red-black tree.
Definition: rbtree.c:294
int(* EtcPalRbTreeNodeCmpFunc)(const EtcPalRbTree *self, const void *value_a, const void *value_b)
A function type that compares two value instances contained in a tree.
Definition: rbtree.h:153
etcpal_error_t etcpal_rbtree_clear(EtcPalRbTree *self)
Clear all values from a red-black tree.
Definition: rbtree.c:529
void * etcpal_rbiter_first(EtcPalRbIter *self, EtcPalRbTree *tree)
Point a red-black tree iterator at the first value in the tree.
Definition: rbtree.c:750
void etcpal_rbtree_node_dealloc_cb(const EtcPalRbTree *self, EtcPalRbNode *node)
The default node deallocation callback.
Definition: rbtree.c:139
void * etcpal_rbiter_upper_bound(EtcPalRbIter *self, EtcPalRbTree *tree, const void *value)
Point a red-black tree iterator to the upper-bound of a value.
Definition: rbtree.c:857
etcpal_error_t etcpal_rbtree_clear_with_cb(EtcPalRbTree *self, EtcPalRbTreeNodeFunc node_cb)
Clear all values from a red-black tree, calling back into the application for each node and value bei...
Definition: rbtree.c:552
#define ETCPAL_RB_ITER_MAX_HEIGHT
The tallest allowable tree that can be iterated over.
Definition: rbtree.h:127
int etcpal_rbtree_test(EtcPalRbTree *self, EtcPalRbNode *root)
Test the validity of a red-black tree.
Definition: rbtree.c:612
void * etcpal_rbiter_lower_bound(EtcPalRbIter *self, EtcPalRbTree *tree, const void *value)
Point a red-black tree iterator to the lower-bound of a value.
Definition: rbtree.c:820
etcpal_error_t etcpal_rbtree_remove(EtcPalRbTree *self, const void *value)
Remove a value from a red-black tree.
Definition: rbtree.c:392
void * etcpal_rbtree_find(EtcPalRbTree *self, const void *value)
Find a value in a red-black tree.
Definition: rbtree.c:214
void(* EtcPalRbTreeNodeFunc)(const EtcPalRbTree *self, EtcPalRbNode *node)
A function type to be called for each node in a tree.
Definition: rbtree.h:166
void * etcpal_rbiter_last(EtcPalRbIter *self, EtcPalRbTree *tree)
Point a red-black tree iterator at the last value in the tree.
Definition: rbtree.c:768
EtcPalRbNode *(* EtcPalRbNodeAllocFunc)(void)
A function type to allocate a new node.
Definition: rbtree.h:177
struct EtcPalRbIter EtcPalRbIter
A red-black tree iterator.
void * etcpal_rbiter_next(EtcPalRbIter *self)
Advance a red-black tree iterator.
Definition: rbtree.c:785
void(* EtcPalRbNodeDeallocFunc)(EtcPalRbNode *node)
A function type to deallocate a node.
Definition: rbtree.h:188
EtcPalRbTree * etcpal_rbtree_init(EtcPalRbTree *self, EtcPalRbTreeNodeCmpFunc node_cmp_cb, EtcPalRbNodeAllocFunc alloc_f, EtcPalRbNodeDeallocFunc dealloc_f)
Initialize a red-black tree node.
Definition: rbtree.c:158
int etcpal_rbtree_node_cmp_ptr_cb(const EtcPalRbTree *self, const void *a, const void *b)
The default node comparison callback.
Definition: rbtree.c:127
void * etcpal_rbiter_prev(EtcPalRbIter *self)
Reverse-advance a red-black tree iterator.
Definition: rbtree.c:802
size_t etcpal_rbtree_size(EtcPalRbTree *self)
Get the current number of values in a red-black tree.
Definition: rbtree.c:591
EtcPalRbIter * etcpal_rbiter_init(EtcPalRbIter *self)
Initialize a red-black tree iterator.
Definition: rbtree.c:665
etcpal_error_t etcpal_rbtree_insert(EtcPalRbTree *self, void *value)
Insert a new value into a red-black tree.
Definition: rbtree.c:255
etcpal_error_t etcpal_rbtree_remove_with_cb(EtcPalRbTree *self, const void *value, EtcPalRbTreeNodeFunc node_cb)
Remove a value from a red-black tree, calling back into the application with the node and value being...
Definition: rbtree.c:418
EtcPalRbNode * etcpal_rbnode_init(EtcPalRbNode *self, void *value)
Initialize a red-black tree node.
Definition: rbtree.c:64
A red-black tree iterator.
Definition: rbtree.h:223
EtcPalRbTree * tree
The tree being iterated over.
Definition: rbtree.h:224
EtcPalRbNode * node
The current node.
Definition: rbtree.h:225
EtcPalRbNode * path[ETCPAL_RB_ITER_MAX_HEIGHT]
The traversal path to the current node.
Definition: rbtree.h:226
size_t top
Top of the traversal stack.
Definition: rbtree.h:227
void * info
User provided, not used by etcpal_rbiter.
Definition: rbtree.h:228
A red-black tree node.
Definition: rbtree.h:196
void * value
The value object represented by this node.
Definition: rbtree.h:199
EtcPalRbNode * link[2]
Child node links: left [0], right [1].
Definition: rbtree.h:198
int red
The node color: red (1), black (0)
Definition: rbtree.h:197
A red-black tree.
Definition: rbtree.h:208
EtcPalRbNodeAllocFunc alloc_f
A function to use for allocating a new node.
Definition: rbtree.h:212
EtcPalRbNodeDeallocFunc dealloc_f
A function to use for deallocating a node.
Definition: rbtree.h:213
EtcPalRbTreeNodeCmpFunc cmp
A function to use for comparing two nodes.
Definition: rbtree.h:210
size_t size
The current count of nodes in the tree.
Definition: rbtree.h:211
void * info
User provided, not used by etcpal_rbtree.
Definition: rbtree.h:214
EtcPalRbNode * root
The root node of the tree.
Definition: rbtree.h:209