Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.2 KiB

  1. /* --------------------------------------------------------------------
  2. Microsoft OS/2 LAN Manager
  3. Copyright(c) Microsoft Corp., 1990
  4. RPC - Written by Dov Harel
  5. This file contains the definition for splay tree self
  6. adjusting binary trees
  7. -------------------------------------------------------------------- */
  8. #ifndef __DICT_HXX__
  9. #define __DICT_HXX__
  10. #if 0
  11. #include "objidl.h"
  12. #include "common.h"
  13. #endif // 0
  14. extern "C"
  15. {
  16. #include "string.h"
  17. }
  18. #ifndef Nil
  19. #define Nil 0
  20. #endif
  21. #ifndef DICT_MYSPLAY
  22. #define VIRT_SPLAY
  23. #else
  24. #define VIRT_SPLAY virtual
  25. #endif
  26. typedef void * pUserType;
  27. class TreeNode {
  28. public:
  29. TreeNode *left; /* left child pointer */
  30. TreeNode *right; /* right child pointer */
  31. pUserType item; /* pointer to some structure */
  32. TreeNode(pUserType itemI)
  33. {
  34. left = right = Nil;
  35. item = itemI;
  36. }
  37. };
  38. typedef int (* CompareFN)(pUserType, pUserType);
  39. typedef void (* PrintFN)(pUserType);
  40. typedef enum {
  41. SUCCESS,
  42. ITEM_ALREADY_PRESENT,
  43. ITEM_NOT_FOUND,
  44. FIRST_ITEM,
  45. LAST_ITEM,
  46. EMPTY_DICTIONARY,
  47. NULL_ITEM
  48. } Dict_Status;
  49. class Dictionary {
  50. TreeNode * root; // pointer to the root of a SAT
  51. long fCompare; // value of last compare
  52. pUserType itemCur; // the current item
  53. long size; // number of records in dictionary/
  54. public:
  55. Dictionary()
  56. {
  57. root = Nil;
  58. size = 0;
  59. }
  60. long SplayUserType(pUserType);
  61. // default comparison is (signed) comparison of pointers to entries
  62. virtual
  63. int Compare (pUserType p1, pUserType p2)
  64. {
  65. long l1 = (long)p1;
  66. long l2 = (long)p2;
  67. return l1 - l2;
  68. }
  69. virtual
  70. void Print(pUserType pItem)
  71. {
  72. }
  73. pUserType Dict_Curr_Item ()
  74. { // return the top of the tree
  75. return ((root)? root->item: Nil);
  76. }
  77. pUserType Dict_Item ()
  78. { // return item from Find/Next/Prev methods
  79. return (itemCur);
  80. }
  81. long Dict_Empty ()
  82. { // Is the tree empty
  83. return (root == Nil);
  84. }
  85. // internal print routine
  86. void PrinTree( int lmargin, TreeNode *np );
  87. // printout the tree, requires print function
  88. void Dict_Print(long indent = 1);
  89. Dict_Status Dict_Find(pUserType); // Item searched for
  90. Dict_Status Dict_Init() // First item of a Type
  91. {
  92. return Dict_Next( (pUserType) 0 );
  93. }
  94. Dict_Status Dict_Next(pUserType = Nil); // Next item of a Type
  95. Dict_Status Dict_Prev(pUserType = Nil); // Previous item of a Type
  96. Dict_Status Dict_Insert(pUserType); // Add a new item to the tree
  97. Dict_Status Dict_Delete(pUserType *); // Delete an item form the tree
  98. // returns the item just deleted
  99. pUserType Dict_Delete_One(); // Delete any convenient node from the tree
  100. // and return the deleted node
  101. Dict_Status Dict_Discard() // Delete the dictionary ( but not the user items )
  102. {
  103. while ( Dict_Delete_One() )
  104. ;
  105. return EMPTY_DICTIONARY;
  106. }
  107. };
  108. // dictionary of strings ( compare function compares strings )
  109. class STRING_DICT : public Dictionary
  110. {
  111. public:
  112. virtual
  113. int Compare (pUserType p1, pUserType p2)
  114. {
  115. return strcmp( (char *)p1, (char *)p2 );
  116. }
  117. };
  118. #endif // __DICT_HXX__