DICT0.H
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*************************************************************/
  2. /**                                                         **/
  3. /**                 Microsoft RPC Examples                  **/
  4. /**                 Dictionary Application                  **/
  5. /**             Copyright(c) Microsoft Corp. 1992-1996      **/
  6. /**                                                         **/
  7. /*************************************************************/
  8. /**************************************************************/
  9. /*  user interface header file for top down splay package     */
  10. /*  based on Sleator & Tarjan's Self Adjusting Trees          */
  11. /**************************************************************/
  12. typedef int (*cmp_rec_func)(void *, void *);
  13.                             /* type of a comparison function */
  14. typedef struct tnode {
  15.     struct tnode *left;     /* left child pointer */
  16.     struct tnode *right;    /* right child pointer */
  17.     void *item;             /* pointer to some structure */
  18. } TreeNode;
  19. typedef struct dictnode {
  20.     TreeNode *root;                 /* pointer to the root of a SAT     */
  21.     long size;                      /* number of records in dictionary  */
  22.     void * state;                   /* reserved for state info          */
  23.     cmp_rec_func cmp_rec;           /* pointer to a comparison function */
  24.     TreeNode* (*splay)(TreeNode *, void *, cmp_rec_func);
  25.                                     /* pointer to a splay function      */
  26.     void (*print_rec)(void *);      /* one line print function          */
  27. } Dictionary;
  28. /*************************************************************************/
  29. /*****              Core functions (internal)                        *****/
  30. /*************************************************************************/
  31. TreeNode *              /* returns the new root                 */
  32. tdSplay(                /* general top down splay               */
  33.     TreeNode *root,     /* the current root of the tree         */
  34.     void *keyItem,      /* pointer to a "key item" searched for */
  35.     int (*cmp)(void *, void *) );
  36.                         /* pointer to a comparison function     */
  37. TreeNode*
  38. tdSplayLeft(TreeNode* root);
  39. TreeNode*
  40. tdSplayRight(TreeNode* root);
  41. void
  42. print_tree_sat(         /* prints the binary tree (indented right subtree,
  43.                            followed by the root, followed by the indented
  44.                            right dubtree) */
  45.     Dictionary * dp,
  46.     int indent);        /* number of blanks to indent subtrees */
  47. /*************************************************************************/
  48. /***    Minimal Dictionary Operations:                                 ***/
  49. /***                                                                   ***/
  50. /***    Dictionary *Dict_New(Cmp_rec*, Splay*, print_rec*)             ***/
  51. /***                                                                   ***/
  52. /***    Dict_Status Dict_Find(Dictionary*, Item*)                      ***/
  53. /***    Dict_Status Dict_Next(Dictionary*, Item*)                      ***/
  54. /***    Dict_Status Dict_Prev(Dictionary*, Item*)                      ***/
  55. /***    Dict_Status Dict_Insert(Dictionary*, Item*)                    ***/
  56. /***    Dict_Status Dict_Delete(Dictionary*, Item**)                   ***/
  57. /***                                                                   ***/
  58. /***    Item* DICT_CURR_ITEM(Dict*)                                    ***/
  59. /*************************************************************************/
  60. typedef enum {
  61.     SUCCESS,
  62.     ITEM_ALREADY_PRESENT,
  63.     ITEM_NOT_FOUND,
  64.     FIRST_ITEM,
  65.     LAST_ITEM,
  66.     EMPTY_DICTIONARY,
  67.     NULL_ITEM
  68. } Dict_Status;
  69. #define DICT_CURR_ITEM(pDict)  ( (pDict)->root->item )
  70. #define DICT_EMPTY(pDict)      ( (pDict)->root == NULL )
  71. Dictionary*
  72. Dict_New(                    /* returns a new dictionary node          */
  73.     int (*cmp_rec)           /* pointer to item comparison function    */
  74.         (void *, void *),
  75.     TreeNode* (*splay)       /* pointer to a splay function            */
  76.         (TreeNode *, void *, cmp_rec_func),
  77.     void (*print_rec)        /* pointer to one line item print routine */
  78.         (void *) );
  79. Dict_Status
  80. Dict_Find(
  81.     Dictionary* dp,     /* Dictionary to be searched. */
  82.     void* item);        /* Item searched for. */
  83. Dict_Status
  84. Dict_Next(
  85.     Dictionary* dp,     /* Dictionary to be searched. */
  86.     void* item);        /* A Key item.  Advance to successor of item in dp. */
  87. Dict_Status
  88. Dict_Prev(
  89.     Dictionary* dp,     /* Dictionary to be searched. */
  90.     void* item);        /* A Key item.  Retreat to predecessor of item in dp. */
  91. Dict_Status
  92. Dict_Insert(            /* insert the given item into the tree */
  93.     Dictionary* dp,     /* the modified dictionary */
  94.     void* item);        /* the item to be inserted */
  95. Dict_Status
  96. Dict_Delete(            /* delete the given item into from the tree */
  97.     Dictionary* dp,     /* the modified dictionary */
  98.     void** pItem);      /* IN: points to the (key) item to be deleted */
  99.                         /* OUT: points to the item just deleted */