XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
rbtree.h
浏览该文件的文档.
1
13#ifndef __xwos_lib_rbtree_h__
14#define __xwos_lib_rbtree_h__
15
16#include <xwos/standard.h>
17
68};
69
76};
77
84 union {
89 } lpc;
90};
91
97};
98
103static __xwlib_inline
105{
106 rbt->root = NULL;
107}
108
113static __xwlib_inline
115{
116 rbn->left = NULL;
117 rbn->right = NULL;
118 rbn->lpc.v = 0;
119}
120
126#define xwlib_rbtree_get_link(lpc) \
127 ((struct xwlib_rbtree_node **)(((xwptr_t)(lpc)) & (~((xwptr_t)3UL))))
128
134#define xwlib_rbtree_get_lnpos(lpc) ((xwptr_t)(lpc) & (~((xwptr_t)2UL)))
135
141#define xwlib_rbtree_get_pos(lpc) ((xwptr_t)(((xwptr_t)(lpc)) & (xwptr_t)1UL))
142
150#define xwlib_rbtree_tst_right(lpc) (!!xwlib_rbtree_get_pos(lpc))
151
159#define xwlib_rbtree_tst_left(lpc) (!xwlib_rbtree_tst_right(lpc))
160
167#define xwlib_rbtree_gen_lc(n, color) \
168 ((xwptr_t)(&((n)->left)) | (xwptr_t)(color))
169
176#define xwlib_rbtree_gen_rc(n, color) \
177 ((xwptr_t)(&((n)->right)) | (xwptr_t)(color) | (xwptr_t)XWLIB_RBTREE_POS_RIGHT)
178
184#define xwlib_rbtree_gen_lr(n) xwlib_rbtree_gen_lc((n), XWLIB_RBTREE_COLOR_RED)
185
191#define xwlib_rbtree_gen_lb(n) xwlib_rbtree_gen_lc((n), XWLIB_RBTREE_COLOR_BLACK)
192
198#define xwlib_rbtree_gen_rr(n) xwlib_rbtree_gen_rc((n), XWLIB_RBTREE_COLOR_RED)
199
205#define xwlib_rbtree_gen_rb(n) xwlib_rbtree_gen_rc((n), XWLIB_RBTREE_COLOR_BLACK)
206
212#define xwlib_rbtree_get_color(lpc) (((xwptr_t)(lpc)) & (xwptr_t)2UL)
213
220#define xwlib_rbtree_tst_black(lpc) (!!xwlib_rbtree_get_color(lpc))
221
228#define xwlib_rbtree_tst_red(lpc) (!xwlib_rbtree_get_color(lpc))
229
236#define xwlib_rbtree_entry(ptr, type, member) xwcc_derof((ptr), type, member)
237
252static __xwlib_inline
254{
255 xwptr_t pos;
256 struct xwlib_rbtree_node ** link;
257
259 link = &(xwlib_rbtree_get_link(node->lpc.link)[-pos]);
260 return (struct xwlib_rbtree_node *)link;
261}
262
270static __xwlib_inline
272{
273 xwptr_t pos;
274 struct xwlib_rbtree_node ** link;
275
278 return (struct xwlib_rbtree_node *)link;
279}
280
289static __xwlib_inline
291 struct xwlib_rbtree_node * node)
292{
293 struct xwlib_rbtree_node * p;
294
295 p = xwlib_rbtree_get_parent(node);
296 return !!(p == (struct xwlib_rbtree_node *)&rbt->root);
297}
298
307static __xwlib_inline
309 struct xwlib_rbtree_node ** link)
310{
311 return !!(link == &rbt->root);
312}
313
318static __xwlib_inline
320{
321 node->lpc.color ^= (xwptr_t)2UL;
322}
323
328static __xwlib_inline
330{
331 node->lpc.color |= (xwptr_t)2UL;
332}
333
338static __xwlib_inline
340{
341 node->lpc.color &= ~((xwptr_t)(2UL));
342}
343
349static __xwlib_inline
351{
352 node->lpc.v = lpc;
353 *xwlib_rbtree_get_link(lpc) = node;
354}
355
360static __xwlib_inline
362{
363 *link = NULL;
364}
365
371static __xwlib_inline
372struct xwlib_rbtree_node *
374{
375 struct xwlib_rbtree_node * c = node->left;
376 if (!c) {
377 c = node;
378 } else {
379 while (NULL != c->right) {
380 c = c->right;
381 }
382 }
383 return c;
384}
385
391static __xwlib_inline
392struct xwlib_rbtree_node *
394{
395 struct xwlib_rbtree_node * c = node->right;
396 if (!c) {
397 c = node;
398 } else {
399 while (NULL != c->left) {
400 c = c->left;
401 }
402 }
403 return c;
404}
405
412static __xwlib_inline
414 struct xwlib_rbtree_node * oldn)
415{
416 newn->lpc.v = oldn->lpc.v;
417 *xwlib_rbtree_get_link(oldn->lpc.v) = newn;
418}
419
424static __xwlib_inline
426{
428}
429
436static __xwlib_inline
438{
439 return !!(NULL == t->root);
440}
441
448static __xwlib_inline
450{
451 return !!((xwptr_t)0 == n->lpc.v);
452}
453
459void xwlib_rbtree_insert_color(struct xwlib_rbtree * tree,
460 struct xwlib_rbtree_node * node);
461
469void xwlib_rbtree_remove(struct xwlib_rbtree * tree,
470 struct xwlib_rbtree_node * node);
471
477void xwlib_rbtree_replace(struct xwlib_rbtree_node * newn,
478 struct xwlib_rbtree_node * oldn);
479
484#endif /* xwos/lib/rbtree.h */
#define __xwlib_inline
Definition compiler.h:203
#define __xwcc_alignptr
Definition compiler.h:129
static void xwlib_rbtree_link_nil(struct xwlib_rbtree_node **link)
链接空(叶子)节点
Definition rbtree.h:361
static bool xwlib_rbtree_tst_link_root(struct xwlib_rbtree *rbt, struct xwlib_rbtree_node **link)
测试是否连接到根节点(是否为根节点的子节点)
Definition rbtree.h:308
xwlib_rbtree_pos_em
红黑树节点位置枚举
Definition rbtree.h:65
static struct xwlib_rbtree_node * xwlib_rbtree_get_successor(struct xwlib_rbtree_node *node)
返回节点的后继节点
Definition rbtree.h:393
static bool xwlib_rbtree_tst_node_unlinked(struct xwlib_rbtree_node *n)
测试红黑树节点是否已经链接到红黑树中
Definition rbtree.h:449
static struct xwlib_rbtree_node * xwlib_rbtree_get_predecessor(struct xwlib_rbtree_node *node)
返回节点的前趋节点
Definition rbtree.h:373
static void xwlib_rbtree_flip_color(struct xwlib_rbtree_node *node)
反转节点的颜色
Definition rbtree.h:319
static void xwlib_rbtree_transplant_nil(struct xwlib_rbtree_node *oldn)
用叶子(NIL)替换一个旧节点
Definition rbtree.h:425
xwlib_rbtree_color_em
红黑树节点颜色枚举
Definition rbtree.h:73
static void xwlib_rbtree_init_node(struct xwlib_rbtree_node *rbn)
初始化红黑树节点
Definition rbtree.h:114
#define xwlib_rbtree_get_link(lpc)
获取lpc信息中的link指针
Definition rbtree.h:126
static bool xwlib_rbtree_tst_empty(struct xwlib_rbtree *t)
测试一棵红黑树是否为空树
Definition rbtree.h:437
static void xwlib_rbtree_set_black(struct xwlib_rbtree_node *node)
将节点设置为黑色
Definition rbtree.h:329
static void xwlib_rbtree_set_red(struct xwlib_rbtree_node *node)
将节点设置为红色
Definition rbtree.h:339
static void xwlib_rbtree_transplant(struct xwlib_rbtree_node *newn, struct xwlib_rbtree_node *oldn)
用新节点代替旧节点并继承它的链指针、颜色和位置信息, 但新节点并不接管旧节点的子节点
Definition rbtree.h:413
#define xwlib_rbtree_get_pos(lpc)
获取lpc信息中的位置信息
Definition rbtree.h:141
static struct xwlib_rbtree_node * xwlib_rbtree_get_parent(struct xwlib_rbtree_node *node)
返回节点的父节点指针
Definition rbtree.h:253
void xwlib_rbtree_insert_color(struct xwlib_rbtree *tree, struct xwlib_rbtree_node *node)
插入一个红色节点后修正红黑树的颜色
Definition rbtree.c:17
static void xwlib_rbtree_init(struct xwlib_rbtree *rbt)
初始化红黑树
Definition rbtree.h:104
static struct xwlib_rbtree_node * xwlib_rbtree_get_parent_from_lpc(xwptr_t lpc)
从lpc信息返回父节点指针
Definition rbtree.h:271
void xwlib_rbtree_remove(struct xwlib_rbtree *tree, struct xwlib_rbtree_node *node)
删除一个节点,并修正红黑树的颜色
Definition rbtree.c:320
static bool xwlib_rbtree_tst_root(struct xwlib_rbtree *rbt, struct xwlib_rbtree_node *node)
测试当前节点是否为根节点
Definition rbtree.h:290
static void xwlib_rbtree_link(struct xwlib_rbtree_node *node, xwptr_t lpc)
链接节点,并设置位置及颜色信息
Definition rbtree.h:350
void xwlib_rbtree_replace(struct xwlib_rbtree_node *newn, struct xwlib_rbtree_node *oldn)
用一个新节点代替旧节点,继承它的颜色、位置信息并接管它的子树
Definition rbtree.c:1007
@ XWLIB_RBTREE_POS_RIGHT
Definition rbtree.h:67
@ XWLIB_RBTREE_POS_LEFT
Definition rbtree.h:66
@ XWLIB_RBTREE_COLOR_RED
Definition rbtree.h:74
@ XWLIB_RBTREE_COLOR_BLACK
Definition rbtree.h:75
#define NULL
Definition type.h:28
unsigned long xwptr_t
Definition type.h:375
红黑树节点
Definition rbtree.h:81
struct xwlib_rbtree_node * left
Definition rbtree.h:82
struct xwlib_rbtree_node ** link
Definition rbtree.h:85
xwptr_t color
Definition rbtree.h:87
struct xwlib_rbtree_node * right
Definition rbtree.h:83
xwptr_t pos
Definition rbtree.h:86
union xwlib_rbtree_node::@5 lpc
红黑树
Definition rbtree.h:95
struct xwlib_rbtree_node * root
Definition rbtree.h:96
XWOS的标准头文件