Leaked source code of windows server 2003
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.

177 lines
5.4 KiB

  1. /* --------------------------------------------------------------------
  2. Microsoft OS/2 LAN Manager
  3. Copyright(c) Microsoft Corp., 1990-1999
  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. #include <basetsd.h>
  11. #include "common.hxx"
  12. #include "freelist.hxx"
  13. #include "listhndl.hxx"
  14. extern "C"
  15. {
  16. #include "string.h"
  17. }
  18. #ifndef Nil
  19. #define Nil 0
  20. #endif
  21. typedef void * pUserType;
  22. class TreeNode
  23. {
  24. public:
  25. TreeNode * left; /* left child pointer */
  26. TreeNode * right; /* right child pointer */
  27. pUserType item; /* pointer to some structure */
  28. TreeNode( pUserType itemI = Nil)
  29. : left ( Nil ),
  30. right( Nil ),
  31. item ( itemI )
  32. {
  33. }
  34. private:
  35. static
  36. FreeListMgr MyFreeList;
  37. public:
  38. void * operator new( size_t size )
  39. {
  40. return MyFreeList.Get( size );
  41. }
  42. void operator delete( void * pX )
  43. {
  44. MyFreeList.Put( pX );
  45. }
  46. };
  47. typedef int (* CompareFN)(pUserType, pUserType);
  48. typedef void (* PrintFN)(pUserType);
  49. typedef enum {
  50. SUCCESS,
  51. ITEM_ALREADY_PRESENT,
  52. ITEM_NOT_FOUND,
  53. FIRST_ITEM,
  54. LAST_ITEM,
  55. EMPTY_DICTIONARY,
  56. NULL_ITEM
  57. } Dict_Status;
  58. class Dictionary
  59. {
  60. private:
  61. TreeNode * root; // pointer to the root of a SAT
  62. SSIZE_T fCompare; // value of last compare
  63. pUserType itemCur; // the current item
  64. long size; // number of records in dictionary/
  65. TreeNode Dumbo;
  66. TreeNode * Dummy; // a "global" dummy node
  67. TreeNode * SplayLeft ( TreeNode * t );
  68. TreeNode * SplayRight( TreeNode * t );
  69. public:
  70. Dictionary()
  71. : root ( Nil ),
  72. fCompare( 0 ),
  73. itemCur ( Nil ),
  74. size ( 0 ),
  75. Dummy ( &Dumbo )
  76. {
  77. }
  78. SSIZE_T SplayUserType( pUserType );
  79. // default comparison is (signed) comparison of pointers to entries
  80. virtual
  81. SSIZE_T Compare (pUserType p1, pUserType p2)
  82. {
  83. SSIZE_T l1 = (SSIZE_T) p1;
  84. SSIZE_T l2 = (SSIZE_T) p2;
  85. return l1 - l2;
  86. }
  87. virtual
  88. void Print(pUserType )
  89. {
  90. }
  91. pUserType Dict_Curr_Item ()
  92. { // return the top of the tree
  93. return ((root)? root->item: Nil);
  94. }
  95. pUserType Dict_Item ()
  96. { // return item from Find/Next/Prev methods
  97. return (itemCur);
  98. }
  99. long Dict_Empty ()
  100. { // Is the tree empty
  101. return (root == Nil);
  102. }
  103. // internal print routine
  104. void PrinTree( int lmargin, TreeNode *np );
  105. // printout the tree, requires print function
  106. void Dict_Print(long indent = 1);
  107. Dict_Status Dict_Find(pUserType); // Item searched for
  108. Dict_Status Dict_Init() // First item of a Type
  109. {
  110. return Dict_Next( (pUserType) 0 );
  111. }
  112. Dict_Status Dict_Next(pUserType = Nil); // Next item of a Type
  113. Dict_Status Dict_Prev(pUserType = Nil); // Previous item of a Type
  114. Dict_Status Dict_Insert(pUserType); // Add a new item to the tree
  115. Dict_Status Dict_Delete(pUserType *); // Delete an item form the tree
  116. // returns the item just deleted
  117. pUserType Dict_Delete_One(); // Delete any convenient node from the tree
  118. // and return the deleted node
  119. Dict_Status Dict_Discard() // Delete the dictionary ( but not the user items )
  120. {
  121. while ( Dict_Delete_One() )
  122. ;
  123. return EMPTY_DICTIONARY;
  124. }
  125. short Dict_GetList( gplistmgr & List ); // make a list of the nodes in the dictionary
  126. };
  127. // dictionary of strings ( compare function compares strings )
  128. class STRING_DICT : public Dictionary
  129. {
  130. public:
  131. virtual
  132. SSIZE_T Compare (pUserType p1, pUserType p2)
  133. {
  134. return strcmp( (char *)p1, (char *)p2 );
  135. }
  136. };
  137. #endif // __DICT_HXX__