liba 0.1.15
An algorithm library based on C/C++
 
Loading...
Searching...
No Matches
rbt.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_RBT_H
7#define LIBA_RBT_H
8
9#include "a.h"
10
16
17/* clang-format off */
18#define A_RBT_ROOT {A_NULL}
19/* clang-format on */
20
24typedef struct a_rbt_node
25{
26 struct a_rbt_node *left;
27 struct a_rbt_node *right;
32#if defined(A_SIZE_POINTER) && (A_SIZE_POINTER + 0 > 1)
34#else /* !A_SIZE_POINTER */
35 struct a_rbt_node *parent;
36 unsigned int color;
37#endif /* A_SIZE_POINTER */
39
46A_INTERN a_rbt_node *a_rbt_parent(a_rbt_node const *node)
47{
48#if defined(A_SIZE_POINTER) && (A_SIZE_POINTER + 0 > 1)
49 return a_cast_r(a_rbt_node *, node->parent_ & ~a_uptr_c(1));
50#else /* !A_SIZE_POINTER */
51 return node->parent;
52#endif /* A_SIZE_POINTER */
53}
54
61A_INTERN a_rbt_node *a_rbt_init(a_rbt_node *node, a_rbt_node *parent)
62{
63#if defined(A_SIZE_POINTER) && (A_SIZE_POINTER + 0 > 1)
64 node->parent_ = a_cast_r(a_uptr, parent);
65#else /* !A_SIZE_POINTER */
66 node->parent = parent;
67 node->color = 0;
68#endif /* A_SIZE_POINTER */
69 node->right = A_NULL;
70 node->left = A_NULL;
71 return node;
72}
73
77typedef union a_rbt
78{
79 struct a_rbt_node *node;
81
86A_INTERN void a_rbt_root(a_rbt *root) { root->node = A_NULL; }
87
88#if defined(__cplusplus)
89extern "C" {
90#endif /* __cplusplus */
91
97A_EXTERN a_rbt_node *a_rbt_head(a_rbt const *root);
98
104A_EXTERN a_rbt_node *a_rbt_tail(a_rbt const *root);
105
112
119
126
133
139A_EXTERN a_rbt_node *a_rbt_post_head(a_rbt const *root);
140
146A_EXTERN a_rbt_node *a_rbt_post_tail(a_rbt const *root);
147
154
161
169A_EXTERN a_rbt_node *a_rbt_tear(a_rbt *root, a_rbt_node **next);
170
181A_EXTERN a_rbt_node *a_rbt_search(a_rbt const *root, void const *ctx, int (*cmp)(void const *, void const *));
182
193A_EXTERN a_rbt_node *a_rbt_insert(a_rbt *root, a_rbt_node *node, int (*cmp)(void const *, void const *));
194
200A_EXTERN void a_rbt_insert_adjust(a_rbt *root, a_rbt_node *node);
201
207A_EXTERN void a_rbt_remove(a_rbt *root, a_rbt_node *node);
208
209#if defined(__cplusplus)
210} /* extern "C" */
211#endif /* __cplusplus */
212
219#define a_rbt_entry(ptr, type, member) a_container_of(ptr, type, member)
220
237#define a_rbt_foreach(cur, root) \
238 for (a_rbt_node *cur = a_rbt_head(root); cur; cur = a_rbt_next(cur))
239#define A_RBT_FOREACH(cur, root) \
240 for (cur = a_rbt_head(root); cur; cur = a_rbt_next(cur))
241
258#define a_rbt_foreach_reverse(cur, root) \
259 for (a_rbt_node *cur = a_rbt_tail(root); cur; cur = a_rbt_prev(cur))
260#define A_RBT_FOREACH_REVERSE(cur, root) \
261 for (cur = a_rbt_tail(root); cur; cur = a_rbt_prev(cur))
262
279#define a_rbt_pre_foreach(cur, root) \
280 for (a_rbt_node *cur = (root)->node; cur; cur = a_rbt_pre_next(cur))
281#define A_RBT_PRE_FOREACH(cur, root) \
282 for (cur = (root)->node; cur; cur = a_rbt_pre_next(cur))
283
300#define a_rbt_pre_foreach_reverse(cur, root) \
301 for (a_rbt_node *cur = (root)->node; cur; cur = a_rbt_pre_prev(cur))
302#define A_RBT_PRE_FOREACH_REVERSE(cur, root) \
303 for (cur = (root)->node; cur; cur = a_rbt_pre_prev(cur))
304
321#define a_rbt_post_foreach(cur, root) \
322 for (a_rbt_node *cur = a_rbt_post_head(root); cur; cur = a_rbt_post_next(cur))
323#define A_RBT_POST_FOREACH(cur, root) \
324 for (cur = a_rbt_post_head(root); cur; cur = a_rbt_post_next(cur))
325
342#define a_rbt_post_foreach_reverse(cur, root) \
343 for (a_rbt_node *cur = a_rbt_post_tail(root); cur; cur = a_rbt_post_prev(cur))
344#define A_RBT_POST_FOREACH_REVERSE(cur, root) \
345 for (cur = a_rbt_post_tail(root); cur; cur = a_rbt_post_prev(cur))
346
364#define a_rbt_fortear(cur, next, root) \
365 for (a_rbt_node *next = A_NULL, *cur = a_rbt_tear(root, &next); cur; cur = a_rbt_tear(root, &next))
366#define A_RBT_FORTEAR(cur, next, root) \
367 for ((void)(next = A_NULL), cur = a_rbt_tear(root, &next); cur; cur = a_rbt_tear(root, &next))
368
370
371#endif /* a/rbt.h */
algorithm library
a_rbt_node * a_rbt_pre_next(a_rbt_node *node)
access next node of red–black binary search tree node preorder
a_rbt_node * a_rbt_head(a_rbt const *root)
access head node of red–black binary search tree in-order
a_rbt_node * a_rbt_next(a_rbt_node *node)
access next node of red–black binary search tree node in-order
void a_rbt_remove(a_rbt *root, a_rbt_node *node)
remove specified node from red–black binary search tree
a_rbt_node * a_rbt_post_prev(a_rbt_node *node)
access prev node of red–black binary search tree node postorder
a_rbt_node * a_rbt_post_head(a_rbt const *root)
access head node of red–black binary search tree postorder
a_rbt_node * a_rbt_parent(a_rbt_node const *node)
access parent of red–black binary search tree node
Definition rbt.h:46
a_rbt_node * a_rbt_prev(a_rbt_node *node)
access prev node of red–black binary search tree node in-order
void a_rbt_insert_adjust(a_rbt *root, a_rbt_node *node)
rebalance the tree after insertion of the specified node
a_rbt_node * a_rbt_insert(a_rbt *root, a_rbt_node *node, int(*cmp)(void const *, void const *))
insert specified node into red–black binary search tree
a_rbt_node * a_rbt_pre_prev(a_rbt_node *node)
access prev node of red–black binary search tree node preorder
a_rbt_node * a_rbt_init(a_rbt_node *node, a_rbt_node *parent)
initialize for red–black binary search tree node
Definition rbt.h:61
a_rbt_node * a_rbt_tear(a_rbt *root, a_rbt_node **next)
tear a node from red–black binary search tree
a_rbt_node * a_rbt_post_next(a_rbt_node *node)
access next node of red–black binary search tree node postorder
a_rbt_node * a_rbt_post_tail(a_rbt const *root)
access tail node of red–black binary search tree postorder
void a_rbt_root(a_rbt *root)
initialize for red–black binary search tree root
Definition rbt.h:86
a_rbt_node * a_rbt_tail(a_rbt const *root)
access tail node of red–black binary search tree in-order
a_rbt_node * a_rbt_search(a_rbt const *root, void const *ctx, int(*cmp)(void const *, void const *))
search specified content from red–black binary search tree
#define a_uptr_c(x)
static cast to a_uptr
Definition a.h:735
unsigned long a_uptr
unsigned integer type capable of holding a pointer to void
Definition a.h:738
instance structure for red–black binary search tree node
Definition rbt.h:25
struct a_rbt_node * right
Definition rbt.h:27
struct a_rbt_node * left
Definition rbt.h:26
a_uptr parent_
Definition rbt.h:33
instance structure for red–black binary search tree root
Definition rbt.h:78
struct a_rbt_node * node
Definition rbt.h:79