1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| #include <stdlib.h> #include <string.h> #include <stdio.h> #include "commonTree.h"
static int cmp_default(const void *p1,const void *p2) { return ((unsigned int)p1 - (unsigned int)p2); }
cTree cTreeCreate(int (*cmp)(const void *p1,const void *p2)) { cTree p = (cTreeManager *)malloc(sizeof(cTreeManager)); if(!p) return NULL; p->total = 0; p->root = RB_ROOT; p->cmp = (cmp == NULL) ? cmp_default : cmp; return p; }
cTreeNodeId cTreeSearch(cTree ctree,void *kdata) { cTreeNode *pTreeNode = NULL; int cmpRes = 0; struct rb_root *pRoot = &(ctree->root); struct rb_node *pNode = pRoot->rb_node;
while(pNode != NULL) { pTreeNode = container_of(pNode,cTreeNode,node); cmpRes = (*(ctree->cmp))(kdata,pTreeNode->kdata); if(cmpRes < 0) { pNode = pNode->rb_left; } else if(cmpRes > 0) { pNode = pNode->rb_right; } else { return pTreeNode; } } return NULL; }
void *getKdataFromNodeId(cTreeNodeId id) { return (id->kdata); } void *getVdataFromNodeId(cTreeNodeId id) { return (id->vdata); }
void *cTreeInsert(cTree ctree,void *kdata,unsigned int kdataSize,void *vdata) { void *oldVdata = NULL; cTreeNode *pTreeNode = NULL; int cmpRes = 0;
struct rb_root *pRoot = &(ctree->root); struct rb_node **ppNode = &(pRoot->rb_node); struct rb_node *pParent = NULL;
while(*ppNode) { pTreeNode = container_of(*ppNode,cTreeNode,node); pParent = *ppNode; cmpRes = (*(ctree->cmp))(kdata,pTreeNode->kdata); if(cmpRes < 0) { ppNode = &(*ppNode)->rb_left; } else if(cmpRes > 0) { ppNode = &(*ppNode)->rb_right; } else { oldVdata = pTreeNode->vdata; pTreeNode->vdata = vdata; return oldVdata; } } cTreeNode *pTargetTreeNode = (cTreeNode *)malloc(sizeof(cTreeNode)+kdataSize+1); if(!pTargetTreeNode) return NULL; pTargetTreeNode->kdata = (void *)(pTargetTreeNode+1); memset(pTargetTreeNode->kdata,0,kdataSize+1); memcpy(pTargetTreeNode->kdata,kdata,kdataSize); pTargetTreeNode->vdata = vdata;
rb_link_node(&pTargetTreeNode->node, pParent,ppNode); rb_insert_color(&pTargetTreeNode->node, pRoot);
ctree->total++; return NULL; }
void *cTreeEraseFromNodeId(cTree ctree,cTreeNodeId *pid) { void *oldVdata = NULL; struct rb_root *pRoot = &(ctree->root); if(!pid || !(*pid)) return NULL; cTreeNode *pTreeNode = *pid;
oldVdata = pTreeNode->vdata; rb_erase(&pTreeNode->node,pRoot); free(pTreeNode); ctree->total--; *pid = NULL; return oldVdata; }
void *cTreeErase(cTree ctree,void *kdata) { void *oldVdata = NULL; cTreeNode *pTreeNode = NULL; int cmpRes = 0; struct rb_root *pRoot = &(ctree->root); struct rb_node *pNode = pRoot->rb_node;
while(pNode != NULL) { pTreeNode = container_of(pNode,cTreeNode,node); cmpRes = (*(ctree->cmp))(kdata,pTreeNode->kdata); if(cmpRes < 0) { pNode = pNode->rb_left; } else if(cmpRes > 0) { pNode = pNode->rb_right; } else { oldVdata = pTreeNode->vdata; rb_erase(pNode,pRoot); free(pTreeNode); ctree->total--; return oldVdata; } } return NULL; }
int cTreeMap(cTree ctree,void apply(void *k,void **pv,void *cl), void *cl) { struct rb_root *pRoot = &(ctree->root); struct rb_node *pNode = NULL; cTreeNode *pTreeNode = NULL; for(pNode = rb_first(pRoot);pNode;pNode = rb_next(pNode)) { pTreeNode = container_of(pNode,cTreeNode,node); apply(pTreeNode->kdata,&pTreeNode->vdata,cl); } return 0; }
static int cTreeMapInternal(struct rb_node *pNode,void apply(void *k,void **x,void *cl,int lev), void *cl, int level) { cTreeNode *pTreeNode = NULL; if(!pNode) return 0;
if(pNode->rb_left) cTreeMapInternal(pNode->rb_left,apply,cl,level+1);
pTreeNode = container_of(pNode,cTreeNode,node); apply(pTreeNode->kdata,&pTreeNode->vdata,cl,level);
if(pNode->rb_right) cTreeMapInternal(pNode->rb_right,apply,cl,level+1); return 0; }
int cTreeMap2(cTree ctree,void apply(void *k,void **x,void *cl,int lev), void *cl) { struct rb_root *pRoot = &(ctree->root); struct rb_node *pNode = pRoot->rb_node;
return cTreeMapInternal(pNode,apply,cl,0); }
int cTreeDestroy(cTree *pctree) { if(!pctree || !(*pctree)) return 0; cTree ctree = *pctree; struct rb_root *pRoot = &(ctree->root); struct rb_node *pNode = NULL; struct rb_node *pNodeTmp = NULL; cTreeNode *pTreeNodeTmp = NULL;
pNode = rb_first(pRoot); while(pNode) { pNodeTmp = pNode; pNode = rb_next(pNode);
pTreeNodeTmp = container_of(pNodeTmp,cTreeNode,node); rb_erase(pNodeTmp,pRoot); free(pTreeNodeTmp); ctree->total--; } free(ctree); *pctree = NULL; return 0; }
|