XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
map.c
浏览该文件的文档.
1
13#include <xwos/standard.h>
14#include <xwos/lib/map.h>
15
18{
19 struct xwlib_rbtree_node ** pos;
20 struct xwlib_rbtree_node * rbn;
21 struct xwlib_map_container * mc;
22 struct xwlib_map_container * parent;
23 xwptr_t lpc;
24 xwssq_t cmprc;
25 xwer_t rc;
26
27 pos = &map->rbtree.root;
28 lpc = (xwptr_t)pos;
29 rbn = *pos;
30 while (NULL != rbn) {
32 cmprc = map->cmp(newmc->key, mc->key);
33 if (cmprc < 0) {
34 pos = &rbn->left;
35 lpc = (xwptr_t)pos;
36 rbn = rbn->left;
37 } else if (cmprc > 0) {
38 pos = &rbn->right;
40 rbn = rbn->right;
41 } else {
42 lpc = (xwptr_t)0;
43 break;
44 }
45 }
46 if ((xwptr_t)0 != lpc) {
49 xwlib_bclst_add_head(&map->bclh, &newmc->bcln);
50 } else {
53 rbn);
54 if (xwlib_rbtree_tst_left(lpc)) {
55 xwlib_bclst_add_front(&newmc->bcln, &parent->bcln);
56 } else {
57 xwlib_bclst_add_behind(&newmc->bcln, &parent->bcln);
58 }
59 }
60 xwlib_rbtree_link(&newmc->rbn, lpc);
61 xwlib_rbtree_insert_color(&map->rbtree, &newmc->rbn);
62 rc = XWOK;
63 } else {
64 rc = -EEXIST;
65 }
66 return rc;
67}
68
71{
72 xwer_t rc;
73
75 rc = -ESRCH;
76 } else {
77 xwlib_rbtree_remove(&map->rbtree, &mc->rbn);
80 rc = XWOK;
81 }
82 return rc;
83}
84
86xwer_t xwlib_map_find(struct xwlib_map * map, void * key,
87 struct xwlib_map_container ** mcbuf)
88{
89 struct xwlib_rbtree_node * rbn;
90 struct xwlib_map_container * mc;
91 xwssq_t cmprc;
92 xwer_t rc;
93
94 rbn = map->rbtree.root;
95 while (NULL != rbn) {
97 cmprc = map->cmp(key, mc->key);
98 if (cmprc < 0) {
99 rbn = rbn->left;
100 } else if (cmprc > 0) {
101 rbn = rbn->right;
102 } else {
103 break;
104 }
105 }
106 if (NULL != rbn) {
108 rc = XWOK;
109 } else {
110 *mcbuf = NULL;
111 rc = -ESRCH;
112 }
113 return rc;
114}
static void xwlib_bclst_add_front(struct xwlib_bclst_node *newn, struct xwlib_bclst_node *next)
将一个节点加入到另一个节点的前面
Definition bclst.h:320
static void xwlib_bclst_add_head(struct xwlib_bclst_node *head, struct xwlib_bclst_node *newn)
将一个节点加入链表头部(链表头的后面)
Definition bclst.h:345
static void xwlib_bclst_del_init(struct xwlib_bclst_node *node)
删除一个节点,并重新初始化它
Definition bclst.h:391
static void xwlib_bclst_add_behind(struct xwlib_bclst_node *newn, struct xwlib_bclst_node *prev)
将一个节点加入到另一个节点的后面
Definition bclst.h:333
#define __xwlib_code
Definition compiler.h:199
#define EEXIST
File exists
Definition errno.h:47
#define ESRCH
No such process
Definition errno.h:33
#define XWOK
No error
Definition errno.h:182
xwer_t xwlib_map_find(struct xwlib_map *map, void *key, struct xwlib_map_container **mcbuf)
根据 键值 查找容器
Definition map.c:86
xwer_t xwlib_map_insert(struct xwlib_map *map, struct xwlib_map_container *newmc)
插入键值对容器
Definition map.c:17
xwer_t xwlib_map_erase(struct xwlib_map *map, struct xwlib_map_container *mc)
删除键值对容器
Definition map.c:70
#define xwlib_rbtree_tst_left(lpc)
测试当前lpc中的位置信息是否为左侧
Definition rbtree.h:159
static bool xwlib_rbtree_tst_link_root(struct xwlib_rbtree *rbt, struct xwlib_rbtree_node **link)
测试是否连接到根节点(是否为根节点的子节点)
Definition rbtree.h:308
static bool xwlib_rbtree_tst_node_unlinked(struct xwlib_rbtree_node *n)
测试红黑树节点是否已经链接到红黑树中
Definition rbtree.h:449
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
void xwlib_rbtree_insert_color(struct xwlib_rbtree *tree, struct xwlib_rbtree_node *node)
插入一个红色节点后修正红黑树的颜色
Definition rbtree.c:17
static struct xwlib_rbtree_node * xwlib_rbtree_get_parent_from_lpc(xwptr_t lpc)
从lpc信息返回父节点指针
Definition rbtree.h:271
#define xwlib_rbtree_entry(ptr, type, member)
从红黑树节点指针值计算出包含该节点成员的外层结构体的指针值
Definition rbtree.h:236
void xwlib_rbtree_remove(struct xwlib_rbtree *tree, struct xwlib_rbtree_node *node)
删除一个节点,并修正红黑树的颜色
Definition rbtree.c:320
static void xwlib_rbtree_link(struct xwlib_rbtree_node *node, xwptr_t lpc)
链接节点,并设置位置及颜色信息
Definition rbtree.h:350
@ XWLIB_RBTREE_POS_RIGHT
Definition rbtree.h:67
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
signed long xwssq_t
Definition type.h:461
unsigned long xwptr_t
Definition type.h:375
XWOS通用库:键值对容器
键值对容器
Definition map.h:32
struct xwlib_bclst_node bcln
Definition map.h:34
struct xwlib_rbtree_node rbn
Definition map.h:33
键值对集合
Definition map.h:47
xwlib_map_cmp_f cmp
Definition map.h:50
struct xwlib_rbtree rbtree
Definition map.h:48
struct xwlib_bclst_node bclh
Definition map.h:49
红黑树节点
Definition rbtree.h:81
struct xwlib_rbtree_node * left
Definition rbtree.h:82
struct xwlib_rbtree_node * right
Definition rbtree.h:83
xwptr_t pos
Definition rbtree.h:86
struct xwlib_rbtree_node * root
Definition rbtree.h:96
XWOS的标准头文件