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.

218 lines
4.1 KiB

  1. #ifndef _BasicTree_H
  2. #define _BasicTree_H
  3. #include "PssException.h"
  4. #include "Allocator.h"
  5. #include "Algorithms.h"
  6. #include "BasicTree.h"
  7. template <class WmiKey,class WmiElement>
  8. class WmiBasicTree
  9. {
  10. protected:
  11. class WmiBasicNode
  12. {
  13. public:
  14. WmiElement m_Element ;
  15. WmiKey m_Key ;
  16. WmiBasicNode *m_Left ;
  17. WmiBasicNode *m_Right ;
  18. WmiBasicNode *m_Parent ;
  19. WmiBasicNode ()
  20. {
  21. m_Left = m_Right = m_Parent = NULL ;
  22. } ;
  23. } ;
  24. public:
  25. class Iterator ;
  26. typedef WmiStatusCode ( * IteratorFunction ) ( void *a_Void , typename WmiBasicTree :: Iterator &a_Iterator ) ;
  27. class Iterator
  28. {
  29. friend WmiBasicTree <WmiKey,WmiElement>;
  30. private:
  31. WmiBasicNode *m_Node ;
  32. WmiBasicNode *LeftMost ( WmiBasicNode *a_Node )
  33. {
  34. WmiBasicNode *t_Node = a_Node ;
  35. if ( t_Node )
  36. {
  37. while ( t_Node->m_Left )
  38. {
  39. t_Node = t_Node->m_Left ;
  40. }
  41. }
  42. return t_Node ;
  43. }
  44. WmiBasicNode *RightMost ( WmiBasicNode *a_Node )
  45. {
  46. WmiBasicNode *t_Node = a_Node ;
  47. if ( t_Node )
  48. {
  49. while ( t_Node->m_Right )
  50. {
  51. t_Node = t_Node->m_Right ;
  52. }
  53. }
  54. return t_Node ;
  55. }
  56. public:
  57. Iterator () : m_Node ( NULL ) { ; }
  58. Iterator ( WmiBasicNode *a_Node ) { m_Node = a_Node ; }
  59. Iterator ( const Iterator &a_Iterator ) { m_Node = a_Iterator.m_Node ; }
  60. Iterator &Left () { m_Node = m_Node ? m_Node->m_Left : NULL ; return *this ; }
  61. Iterator &Right () { m_Node = m_Node ? m_Node->m_Right : NULL ; return *this ; }
  62. Iterator &Parent () { m_Node = m_Node ? m_Node->m_Parent : NULL ; return *this ; }
  63. Iterator &LeftMost ()
  64. {
  65. m_Node = LeftMost ( m_Node ) ;
  66. return *this ;
  67. }
  68. Iterator &RightMost ()
  69. {
  70. m_Node = RightMost ( m_Node ) ;
  71. return *this ;
  72. }
  73. Iterator &Decrement ()
  74. {
  75. WmiBasicNode *t_Node = m_Node ;
  76. if ( t_Node )
  77. {
  78. if ( t_Node->m_Left )
  79. {
  80. t_Node = RightMost ( t_Node->m_Left ) ;
  81. }
  82. else
  83. {
  84. WmiBasicNode *t_Parent ;
  85. while ( ( t_Parent = t_Node->m_Parent ) && ( t_Node->m_Parent->m_Left == t_Node ) )
  86. {
  87. t_Node = t_Parent ;
  88. }
  89. t_Node = t_Parent ;
  90. }
  91. }
  92. m_Node = t_Node ;
  93. return *this ;
  94. }
  95. Iterator &Increment ()
  96. {
  97. WmiBasicNode *t_Node = m_Node ;
  98. if ( t_Node )
  99. {
  100. if ( t_Node->m_Right )
  101. {
  102. t_Node = LeftMost ( t_Node->m_Right ) ;
  103. }
  104. else
  105. {
  106. WmiBasicNode *t_Parent ;
  107. while ( ( t_Parent = t_Node->m_Parent ) && ( t_Node->m_Parent->m_Right == t_Node ) )
  108. {
  109. t_Node = t_Parent ;
  110. }
  111. t_Node = t_Parent ;
  112. }
  113. }
  114. m_Node = t_Node ;
  115. return *this ;
  116. }
  117. bool Null () { return m_Node == NULL ; }
  118. WmiKey &GetKey () { return m_Node->m_Key ; }
  119. WmiElement &GetElement () { return m_Node->m_Element ; }
  120. WmiStatusCode PreOrder ( void *a_Void , IteratorFunction a_Function ) ;
  121. WmiStatusCode InOrder ( void *a_Void , IteratorFunction a_Function ) ;
  122. WmiStatusCode PostOrder ( void *a_Void , IteratorFunction a_Function ) ;
  123. } ;
  124. protected:
  125. WmiBasicNode *m_Root ;
  126. ULONG m_Size ;
  127. WmiAllocator &m_Allocator ;
  128. WmiStatusCode DeleteFixup ( WmiBasicNode *a_Node ) ;
  129. WmiStatusCode RecursiveUnInitialize ( WmiBasicNode *a_Node ) ;
  130. public:
  131. WmiBasicTree (
  132. WmiAllocator &a_Allocator
  133. ) ;
  134. ~WmiBasicTree () ;
  135. WmiStatusCode Initialize () ;
  136. WmiStatusCode UnInitialize () ;
  137. WmiStatusCode Insert (
  138. const WmiKey &a_Key ,
  139. const WmiElement &a_Element ,
  140. Iterator &a_Iterator
  141. ) ;
  142. WmiStatusCode Delete (
  143. const WmiKey &a_Key
  144. ) ;
  145. WmiStatusCode Find (
  146. const WmiKey &a_Key ,
  147. Iterator &a_Iterator
  148. ) ;
  149. WmiStatusCode FindNext (
  150. const WmiKey &a_Key ,
  151. Iterator &a_Iterator
  152. ) ;
  153. WmiStatusCode Merge (
  154. WmiBasicTree <WmiKey,WmiElement> &a_Tree
  155. ) ;
  156. ULONG Size () { return m_Size ; } ;
  157. Iterator Begin () { return Iterator ( m_Root ).LeftMost () ; };
  158. Iterator End () { return Iterator ( m_Root ).RightMost () ; }
  159. Iterator Root () { return Iterator ( m_Root ) ; }
  160. } ;
  161. #include <BasicTree.cpp>
  162. #endif _BasicTree_H