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
6#ifndef LIBA_RBT_H
7#define LIBA_RBT_H
8
9#include "a.h"
10
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
256#define a_rbt_foreach_reverse(cur, root) \
257 for (a_rbt_node *cur = a_rbt_tail(root); cur; cur = a_rbt_prev(cur))
258
275#define a_rbt_pre_foreach(cur, root) \
276 for (a_rbt_node *cur = (root)->node; cur; cur = a_rbt_pre_next(cur))
277
294#define a_rbt_pre_foreach_reverse(cur, root) \
295 for (a_rbt_node *cur = (root)->node; cur; cur = a_rbt_pre_prev(cur))
296
313#define a_rbt_post_foreach(cur, root) \
314 for (a_rbt_node *cur = a_rbt_post_head(root); cur; cur = a_rbt_post_next(cur))
315
332#define a_rbt_post_foreach_reverse(cur, root) \
333 for (a_rbt_node *cur = a_rbt_post_tail(root); cur; cur = a_rbt_post_prev(cur))
334
352#define a_rbt_fortear(cur, next, root) \
353 for (a_rbt_node *next = A_NULL, *cur = a_rbt_tear(root, &next); cur; cur = a_rbt_tear(root, &next))
354
357#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
struct a_rbt_node a_rbt_node
instance structure for red–black binary search tree node
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
union a_rbt a_rbt
instance structure for red–black binary search tree root
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)
Definition a.h:580
#define a_uptr
Definition a.h:583
instance structure for red–black binary search tree node
Definition rbt.h:25
struct a_rbt_node * right
Definition rbt.h:27
uintptr_t parent_
Definition rbt.h:33
struct a_rbt_node * left
Definition rbt.h:26
instance structure for red–black binary search tree root
Definition rbt.h:78
struct a_rbt_node * node
root node
Definition rbt.h:79