--- mstools/samples/rpc/dict/dict0.c 2018/08/09 18:20:58 1.1.1.2 +++ mstools/samples/rpc/dict/dict0.c 2018/08/09 18:24:22 1.1.1.3 @@ -6,58 +6,46 @@ /** **/ /*************************************************************/ -#include -#include #include -#include +#include #include -#ifdef NTENV -#include -// #define printf DbgPrint -#endif // NTENV - #include -// #include -// #include -// #include - #include "dict0.h" /************************************************************************/ TreeNode Dumbo; // volatile -TreeNode *Dummy = &Dumbo; // a global dummy node +TreeNode *Dummy = &Dumbo; // a global dummy node -#define ROTATELEFT tmp=t->right; t->right=tmp->left; tmp->left=t; t=tmp +#define ROTATELEFT tmp=t->right; t->right=tmp->left; tmp->left=t; t=tmp #define ROTATERIGHT tmp=t->left; t->left=tmp->right; tmp->right=t; t=tmp -#define LINKLEFT tmp=t; t=t->right; l=l->right=tmp -#define LINKRIGHT tmp=t; t=t->left; r=r->left=tmp -#define ASSEMBLE r->left=t->right; l->right=t->left; \ - t->left=Dummy->right; t->right=Dummy->left +#define LINKLEFT tmp=t; t=t->right; l=l->right=tmp +#define LINKRIGHT tmp=t; t=t->left; r=r->left=tmp +#define ASSEMBLE r->left=t->right; l->right=t->left; \ + t->left=Dummy->right; t->right=Dummy->left /************************************************************************/ /************************************************************************ Basic structure declarations from dict0.h: typedef struct tnode { - struct tnode *left; // left child pointer - struct tnode *right; // right child pointer - void *item; // pointer to some structure + struct tnode *left; // left child pointer + struct tnode *right; // right child pointer + void *item; // pointer to some structure } TreeNode; typedef struct dictnode { TreeNode *root; // pointer to the root of a SAT long size; // number of records in dictionary void * state; // reserved for state info - // int (*cmp_rec)(void *, void *); comparison function pointer - cmp_rec_func cmp_rec; + cmp_rec_func cmp_rec; // pointer to a comparison function TreeNode* (*splay)(TreeNode *, void *, cmp_rec_func); // pointer to a splay function void (*print_rec)(void *); // one line print function } Dictionary; -#define DICT_CURR_ITEM(pDict) pDict->root->item +#define DICT_CURR_ITEM(pDict) pDict->root->item typedef enum { SUCCESS, @@ -67,8 +55,7 @@ typedef enum { LAST_ITEM, EMPTY_DICTIONARY, NULL_ITEM -} -Dict_Status; +} Dict_Status; **************************************************************************/ @@ -76,11 +63,11 @@ Dict_Status; /*** MIDL_user_allocate / MIDL_user_free ***/ /*************************************************************************/ -void * +void __RPC_FAR * __RPC_API MIDL_user_allocate(unsigned long count); -void -MIDL_user_free(void * p); +void __RPC_API +MIDL_user_free(void __RPC_FAR * p); /*************************************************************************/ /*** Minimal Dictionary Operations: ***/ @@ -110,7 +97,8 @@ Dict_New( // returns (void *) ) { Dictionary* dp; - dp = (Dictionary*)MIDL_user_allocate(sizeof(Dictionary)); + + dp = (Dictionary*) MIDL_user_allocate(sizeof(Dictionary)); dp->root = NULL; dp->size = 0; dp->state = NULL; @@ -120,7 +108,6 @@ Dict_New( // returns return(dp); } - Dict_Status Dict_Find( Dictionary* dp, // Dictionary to be searched. @@ -129,8 +116,10 @@ Dict_Find( int keycmp; TreeNode* t; - if (dp->root == NULL) return (EMPTY_DICTIONARY); - if (item == NULL) return(NULL_ITEM); + if (dp->root == NULL) + return (EMPTY_DICTIONARY); + if (item == NULL) + return(NULL_ITEM); t = dp->root = dp->splay(dp->root, item, dp->cmp_rec); keycmp = (dp->cmp_rec)( t->item, item ); if (keycmp != 0) @@ -143,24 +132,28 @@ Dict_Next( Dictionary* dp, // Dictionary to be searched. void* item) // A Key item. Advance to successor of item in dp. { - TreeNode* t, *r; + TreeNode* t; int keycmp; - if (dp->root == NULL) return (EMPTY_DICTIONARY); - if (item == NULL) { dp->root = tdSplayLeft(dp->root); + if (dp->root == NULL) + return (EMPTY_DICTIONARY); + if (item == NULL) { + dp->root = tdSplayLeft(dp->root); return(SUCCESS); } if (item == DICT_CURR_ITEM(dp)) { t = dp->root; - keycmp = 0; } + keycmp = 0; + } else { dp->root = t = dp->splay(dp->root, item, dp->cmp_rec); - keycmp = (dp->cmp_rec)( item, t->item ); + keycmp = (dp->cmp_rec) (item, t->item); } if (keycmp < 0) return(SUCCESS); else if (t->right == NULL) { - return(LAST_ITEM); } + return(LAST_ITEM); + } else { t = dp->root; dp->root = tdSplayLeft(t->right); @@ -175,24 +168,28 @@ Dict_Prev( Dictionary* dp, // Dictionary to be searched. void* item) // A Key item. Retreat to predecessor of item in dp. { - TreeNode* t, s; + TreeNode* t; int keycmp; - if (dp->root == NULL) return (EMPTY_DICTIONARY); - if (item == NULL) { dp->root = tdSplayRight(dp->root); + if (dp->root == NULL) + return (EMPTY_DICTIONARY); + if (item == NULL) { + dp->root = tdSplayRight(dp->root); return(SUCCESS); } if (item == DICT_CURR_ITEM(dp)) { t = dp->root; - keycmp = 0; } + keycmp = 0; + } else { dp->root = t = dp->splay(dp->root, item, dp->cmp_rec); - keycmp = (dp->cmp_rec)( item, t->item ); + keycmp = (dp->cmp_rec) (item, t->item); } if (keycmp > 0) return(SUCCESS); else if (t->left == NULL) { - return(FIRST_ITEM); } + return(FIRST_ITEM); + } else { t = dp->root; dp->root = tdSplayRight(t->left); @@ -210,7 +207,8 @@ Dict_Insert( // insert the gi int keycmp; TreeNode *t, *newNode; - if (item == NULL) return(NULL_ITEM); + if (item == NULL) + return(NULL_ITEM); if (dp->root == NULL) { TreeNode_New(newNode, item); dp->root = newNode; @@ -226,7 +224,8 @@ Dict_Insert( // insert the gi if (keycmp < 0) { // t->item < item newNode->right = t->right; t->right = NULL; - newNode->left = t; } + newNode->left = t; + } else { newNode->left = t->left; t->left = NULL; @@ -249,8 +248,10 @@ Dict_Delete( // delete the gi void* item = *pItem; t = dp->root; - if (item == NULL) return(NULL_ITEM); - if (dp->root == NULL) return (EMPTY_DICTIONARY); + if (item == NULL) + return(NULL_ITEM); + if (dp->root == NULL) + return (EMPTY_DICTIONARY); if (item == DICT_CURR_ITEM(dp)) keycmp = 0; else { @@ -262,9 +263,11 @@ Dict_Delete( // delete the gi *pItem = DICT_CURR_ITEM(dp); if (t->left == NULL) { - dp->root = t->right; } - else if ( (r = t->right) == NULL) { - dp->root = t->left; } + dp->root = t->right; + } + else if ((r = t->right) == NULL) { + dp->root = t->left; + } else { r = tdSplayLeft(r); // at this point r->left == NULL @@ -302,39 +305,52 @@ tdSplay( // general top d r = Dummy; /***/ - if ( (root == NULL) || ((*cmp)(keyItem, root->item) == 0) ) return (root); - Dummy->left=Dummy->right=NULL; + if ( (root == NULL) || ((*cmp)(keyItem, root->item) == 0) ) + return(root); + Dummy->left = Dummy->right = NULL; while ( (kcmpt = (*cmp)(keyItem, t->item)) != 0 ) { if ( kcmpt < 0 ) { - if ( t->left == NULL ) break; + if ( t->left == NULL ) + break; if ( (kcmpleft = (*cmp)(keyItem, t->left->item)) == 0 ) { - LINKRIGHT; } + LINKRIGHT; + } else if ( kcmpleft < 0 ) { ROTATERIGHT; if ( t->left != NULL ) { - LINKRIGHT; } } + LINKRIGHT; + } + } else { // keyItem > t->left->item LINKRIGHT; if ( t->right != NULL ) { - LINKLEFT; } } } + LINKLEFT; + } + } + } else { // keyItem > t->item - if ( t->right == NULL ) break; + if ( t->right == NULL ) + break; if ( (kcmpright = (*cmp)(keyItem, t->right->item)) == 0 ) { - LINKLEFT; } + LINKLEFT; + } else if ( kcmpright > 0 ) { ROTATELEFT; if ( t->right != NULL ) { - LINKLEFT; } } + LINKLEFT; + } + } else { // keyItem < t->right->item LINKLEFT; if ( t->left != NULL ) { - LINKRIGHT; } } + LINKRIGHT; + } + } } } - // assemble: - r->left=t->right; l->right=t->left; - t->left=Dummy->right; t->right=Dummy->left; - return(t); + + ASSEMBLE; + return(t); } TreeNode* @@ -345,17 +361,21 @@ tdSplayLeft(TreeNode* root) TreeNode* r=Dummy; // root of "right subtree" > keyItem TreeNode* tmp; - if ((t == NULL) || (t->left == NULL)) return(t); - if (t->left->left == NULL) { - ROTATERIGHT; return(t); } - Dummy->left=Dummy->right=NULL; + if ( (t == NULL) || (t->left == NULL) ) + return(t); + if ( t->left->left == NULL ) { + ROTATERIGHT; + return(t); + } + Dummy->left = Dummy->right = NULL; while ( t->left != NULL ) { ROTATERIGHT; if ( t->left != NULL ) { - LINKRIGHT; } + LINKRIGHT; + } } - r->left=t->right; l->right=t->left; - t->left=Dummy->right; t->right=Dummy->left; + + ASSEMBLE; return(t); } @@ -367,14 +387,16 @@ tdSplayRight(TreeNode* root) TreeNode* r=Dummy; // root of "right subtree" > keyItem TreeNode* tmp; - if ((t == NULL) || (t->right == NULL)) return(t); - Dummy->left=Dummy->right=NULL; + if ( (t == NULL) || (t->right == NULL) ) + return(t); + Dummy->left = Dummy->right = NULL; while ( t->right != NULL ) { ROTATELEFT; if ( t->right != NULL ) { - LINKLEFT; } + LINKLEFT; + } } - r->left=t->right; l->right=t->left; - t->left=Dummy->right; t->right=Dummy->left; + + ASSEMBLE; return(t); }