diff options
author | cathook <cat.hook31894@gmail.com> | 2013-12-17 13:46:22 +0800 |
---|---|---|
committer | cathook <cat.hook31894@gmail.com> | 2013-12-17 13:46:22 +0800 |
commit | ace4e75ff2fd54a957387fa8a053737ba8c152dd (patch) | |
tree | e1d9c73a101bb7ee58d02a52fc740c992761b114 | |
parent | 9fab4b43f3d8a2fbf8988364af035c4107d64dc5 (diff) | |
download | ctl-ace4e75ff2fd54a957387fa8a053737ba8c152dd.tar.gz ctl-ace4e75ff2fd54a957387fa8a053737ba8c152dd.tar.zst ctl-ace4e75ff2fd54a957387fa8a053737ba8c152dd.zip |
fix bugs
-rw-r--r-- | inc/sptree.h | 8 | ||||
-rw-r--r-- | src/sptree.c | 65 |
2 files changed, 47 insertions, 26 deletions
diff --git a/inc/sptree.h b/inc/sptree.h index dc80d15..5ca63dd 100644 --- a/inc/sptree.h +++ b/inc/sptree.h @@ -28,13 +28,13 @@ void ctl_sptree_clearX(ppvoid sp); #define ctl_sptree_init(A,B,C,D) ctl_sptree_initX(ppVoid(A),B,C,(key_cmp)D) #define ctl_sptree_free(A) ctl_sptree_freeX(ppVoid(A)) -#define ctl_sptree_isEmpty (A) ctl_sptree_isEmptyX (ppcVoid(A)) +#define ctl_sptree_isEmpty(A) ctl_sptree_isEmptyX (ppcVoid(A)) #define ctl_sptree_getKeySize(A) ctl_sptree_getKeySizeX(ppcVoid(A)) #define ctl_sptree_getValSize(A) ctl_sptree_getValSizeX(ppcVoid(A)) -#define ctl_sptree_find (A,B) ctl_sptree_findX (ppVoid(A),pcVoid(B)) -#define ctl_sptree_add (A,B,C) ctl_sptree_addX (ppVoid(A),pcVoid(B),pVoid(C)) -#define ctl_sptree_del (A,B) ctl_sptree_delX (ppVoid(A),pcVoid(B)) +#define ctl_sptree_find(A,B) ctl_sptree_findX (ppVoid(A),pcVoid(B)) +#define ctl_sptree_add(A,B,C) ctl_sptree_addX (ppVoid(A),pcVoid(B),pVoid(C)) +#define ctl_sptree_del(A,B) ctl_sptree_delX (ppVoid(A),pcVoid(B)) #define ctl_sptree_update(A,B,C) ctl_sptree_updateX(ppVoid(A),pcVoid(B),pvoid(C)) #define ctl_sptree_split(A,B,C,D) ctl_sptree_split(ppVoid(A),pcVoid(B),\ diff --git a/src/sptree.c b/src/sptree.c index 5635c82..0952a4d 100644 --- a/src/sptree.c +++ b/src/sptree.c @@ -16,8 +16,8 @@ struct StructSplayTreeNode{ struct StructSplayTreeNode* lchd; struct StructSplayTreeNode* rchd; struct StructSplayTreeNode* fath; - pcvoid* key; - pvoid* val; + pcvoid key; + pvoid val; }; typedef struct StructSplayTree SplayTree; @@ -25,6 +25,9 @@ typedef struct StructSplayTreeNode SplayTreeNode; /******************* static functions *********************/ static void delete_dfs(SplayTreeNode* nd); +static inline void connectL(SplayTreeNode* fath, SplayTreeNode* righ); +static inline void connectR(SplayTreeNode* fath, SplayTreeNode* righ); + static SplayTreeNode* access_node(key_cmp f, SplayTreeNode* nd, pcvoid k); static SplayTreeNode* split_node (key_cmp f, SplayTreeNode* nd, pcvoid k, SplayTreeNode** l, SplayTreeNode** r); @@ -33,16 +36,6 @@ static SplayTreeNode* merge_node(SplayTreeNode* l, SplayTreeNode* r); #define getHeader(A) ((SplayTree*)(A)) #define getSize(A) (sizeof(SplayTreeNode)+(A)->key_size+(A)->val_size) -#define connectL(A,B) \ - do{\ - if((A) != NULL) (A)->lchd = (B);\ - if((B) != NULL) (B)->fath = (A);\ - }while(0) -#define connectR(A,B) \ - do{\ - if((A) != NULL) (A)->rchd = (B);\ - if((B) != NULL) (B)->fath = (A);\ - }while(0) #define disconnL(A) \ do{\ if((A)->lchd != NULL) (A)->lchd->fath = NULL;\ @@ -54,6 +47,21 @@ static SplayTreeNode* merge_node(SplayTreeNode* l, SplayTreeNode* r); (A)->rchd = NULL;\ }while(0) +/* +static void dump_node(SplayTreeNode* now){ + if(now == NULL) return ; + printf("now = (%5.1f, %2d) ", *(double*)now->key, *(int*)now->val); + if(now->lchd == NULL) printf("lchd = ( NULL) "); + else printf("lchd = (%5.1f, %2d) ", *(double*)now->lchd->key, *(int*)now->lchd->val); + if(now->rchd == NULL) printf("rchd = ( NULL) "); + else printf("rchd = (%5.1f, %2d) ", *(double*)now->rchd->key, *(int*)now->rchd->val); + if(now->fath == NULL) printf("fath = ( NULL) "); + else printf("fath = (%5.1f, %2d) ", *(double*)now->fath->key, *(int*)now->fath->val); + printf("\n"); + dump_node(now->lchd); + dump_node(now->rchd); +} +// */ /*************** constructure / destructure ***************/ pvoid ctl_sptree_initX(ppvoid sp, uint k_size, uint v_size, key_cmp f){ SplayTree* head = (SplayTree*)ctl_malloc(sizeof(SplayTree)); @@ -102,7 +110,9 @@ pvoid ctl_sptree_addX(ppvoid sp, pcvoid key, pvoid val){ head->root = merge_node(lft, rgh); }else{ mid = (SplayTreeNode*)ctl_malloc(getSize(head)); - memcpy(mid->key, key, head->key_size); + mid->key = pcVoid(pChar(mid) + sizeof(SplayTreeNode)); + mid->val = pVoid(pChar(mid) + sizeof(SplayTreeNode) + head->key_size); + memcpy(pChar(mid) + sizeof(SplayTreeNode), key, head->key_size); memcpy(mid->val, val, head->val_size); mid->fath = NULL; mid->lchd = NULL; @@ -157,6 +167,14 @@ void ctl_sptree_clearX(ppvoid sp){ /****************** static functions **********************/ +static inline void connectL(SplayTreeNode* fath, SplayTreeNode* lchd){ + if(fath != NULL) fath->lchd = lchd; + if(lchd != NULL) lchd->fath = fath; +} +static inline void connectR(SplayTreeNode* fath, SplayTreeNode* righ){ + if(fath != NULL) fath->rchd = righ; + if(righ != NULL) righ->fath = fath; +} static void delete_dfs(SplayTreeNode* nd){ if(nd->lchd != NULL) delete_dfs(nd->lchd); if(nd->rchd != NULL) delete_dfs(nd->rchd); @@ -166,27 +184,29 @@ static SplayTreeNode* access_node(key_cmp f, SplayTreeNode* nd, pcvoid k){ if(nd == NULL) return NULL; if(f(k, nd->key) == 0) return nd; if(f(k, nd->key) < 0){ - SplayTreeNode* ret = access_node(f, nd->lchd, k); + SplayTreeNode* ret = access_node(f, nd->lchd, k), *tmp; if(nd->lchd != NULL){ if(nd->fath != NULL) if(nd->fath->lchd == nd) connectL(nd->fath, nd->lchd); else connectR(nd->fath, nd->lchd); else nd->lchd->fath = NULL; - connectL(nd, nd->lchd->rchd); - connectR(nd->lchd, nd); + tmp = nd->lchd; + connectL(nd, tmp->rchd); + connectR(tmp, nd); } return ret; }else{ - SplayTreeNode* ret = access_node(f, nd->rchd, k); + SplayTreeNode* ret = access_node(f, nd->rchd, k), *tmp; if(nd->rchd != NULL){ if(nd->fath != NULL) if(nd->fath->lchd == nd) connectL(nd->fath, nd->rchd); else connectR(nd->fath, nd->rchd); else nd->rchd->fath = NULL; - connectR(nd, nd->rchd->lchd); - connectL(nd->rchd, nd); + tmp = nd->rchd; + connectR(nd, tmp->lchd); + connectL(tmp, nd); } return ret; } @@ -219,14 +239,15 @@ static SplayTreeNode* split_node(key_cmp f, SplayTreeNode* nd, pcvoid k, static SplayTreeNode* access_max(SplayTreeNode *nd){ if(nd == NULL) return NULL; if(nd->rchd != NULL){ - SplayTreeNode* ret = nd->rchd; + SplayTreeNode* ret = nd->rchd, *tmp; if(nd->fath != NULL) if(nd->fath->lchd == nd) connectL(nd->fath, nd->rchd); else connectR(nd->fath, nd->rchd); else nd->rchd->fath = NULL; - connectR(nd, nd->rchd->lchd); - connectL(nd->rchd, nd); + tmp = nd->rchd; + connectR(nd, tmp->lchd); + connectL(tmp, nd); return ret; }else{ return nd; |