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.

267 lines
5.0 KiB

  1. #ifndef _AVLTREE_H
  2. #define _AVLTREE_H
  3. #include "PssException.h"
  4. #include "Allocator.h"
  5. #include "Algorithms.h"
  6. #include "AvlTree.h"
  7. template <class WmiKey,class WmiElement>
  8. class WmiAvlTree
  9. {
  10. public:
  11. class WmiAvlNode
  12. {
  13. public:
  14. enum WmAvlState
  15. {
  16. e_Equal,
  17. e_LeftHigher ,
  18. e_RightHigher
  19. } ;
  20. WmiKey m_Key ;
  21. WmiAvlNode *m_Left ;
  22. WmiAvlNode *m_Right ;
  23. WmiAvlNode *m_Parent ;
  24. WmAvlState m_State ;
  25. WmiElement m_Element ;
  26. WmiAvlNode ()
  27. {
  28. m_Left = m_Right = m_Parent = NULL ;
  29. m_State = e_Equal ;
  30. } ;
  31. } ;
  32. public:
  33. class Iterator ;
  34. typedef WmiStatusCode ( * IteratorFunction ) ( void *a_Void , typename WmiAvlTree :: Iterator &a_Iterator ) ;
  35. class Iterator
  36. {
  37. friend WmiAvlTree <WmiKey,WmiElement>;
  38. private:
  39. WmiAvlNode *m_Node ;
  40. WmiAvlNode *LeftMost ( WmiAvlNode *a_Node )
  41. {
  42. WmiAvlNode *t_Node = a_Node ;
  43. if ( t_Node )
  44. {
  45. while ( t_Node->m_Left )
  46. {
  47. t_Node = t_Node->m_Left ;
  48. }
  49. }
  50. return t_Node ;
  51. }
  52. WmiAvlNode *RightMost ( WmiAvlNode *a_Node )
  53. {
  54. WmiAvlNode *t_Node = a_Node ;
  55. if ( t_Node )
  56. {
  57. while ( t_Node->m_Right )
  58. {
  59. t_Node = t_Node->m_Right ;
  60. }
  61. }
  62. return t_Node ;
  63. }
  64. public:
  65. Iterator () : m_Node ( NULL ) { ; }
  66. Iterator ( WmiAvlNode *a_Node ) { m_Node = a_Node ; }
  67. Iterator ( const Iterator &a_Iterator ) { m_Node = a_Iterator.m_Node ; }
  68. Iterator &Left () { m_Node = m_Node ? m_Node->m_Left : NULL ; return *this ; }
  69. Iterator &Right () { m_Node = m_Node ? m_Node->m_Right : NULL ; return *this ; }
  70. Iterator &Parent () { m_Node = m_Node ? m_Node->m_Parent : NULL ; return *this ; }
  71. Iterator &LeftMost ()
  72. {
  73. m_Node = LeftMost ( m_Node ) ;
  74. return *this ;
  75. }
  76. Iterator &RightMost ()
  77. {
  78. m_Node = RightMost ( m_Node ) ;
  79. return *this ;
  80. }
  81. Iterator &Decrement ()
  82. {
  83. WmiAvlNode *t_Node = m_Node ;
  84. if ( t_Node )
  85. {
  86. if ( t_Node->m_Left )
  87. {
  88. t_Node = RightMost ( t_Node->m_Left ) ;
  89. }
  90. else
  91. {
  92. WmiAvlNode *t_Parent ;
  93. while ( ( t_Parent = t_Node->m_Parent ) && ( t_Node->m_Parent->m_Left == t_Node ) )
  94. {
  95. t_Node = t_Parent ;
  96. }
  97. t_Node = t_Parent ;
  98. }
  99. }
  100. m_Node = t_Node ;
  101. return *this ;
  102. }
  103. Iterator &Increment ()
  104. {
  105. WmiAvlNode *t_Node = m_Node ;
  106. if ( t_Node )
  107. {
  108. if ( t_Node->m_Right )
  109. {
  110. t_Node = LeftMost ( t_Node->m_Right ) ;
  111. }
  112. else
  113. {
  114. WmiAvlNode *t_Parent ;
  115. while ( ( t_Parent = t_Node->m_Parent ) && ( t_Node->m_Parent->m_Right == t_Node ) )
  116. {
  117. t_Node = t_Parent ;
  118. }
  119. t_Node = t_Parent ;
  120. }
  121. }
  122. m_Node = t_Node ;
  123. return *this ;
  124. }
  125. bool Null () { return m_Node == NULL ; }
  126. WmiKey &GetKey () { return m_Node->m_Key ; }
  127. WmiElement &GetElement () { return m_Node->m_Element ; }
  128. WmiStatusCode PreOrder ( void *a_Void , IteratorFunction a_Function ) ;
  129. WmiStatusCode InOrder ( void *a_Void , IteratorFunction a_Function ) ;
  130. WmiStatusCode PostOrder ( void *a_Void , IteratorFunction a_Function ) ;
  131. } ;
  132. protected:
  133. WmiAvlNode *m_Root ;
  134. ULONG m_Size ;
  135. WmiAllocator &m_Allocator ;
  136. WmiStatusCode DeleteFixup ( WmiAvlNode *a_Node , bool &a_Decreased ) ;
  137. WmiStatusCode RecursiveCheck ( WmiAvlNode *a_Root , ULONG &a_Count , ULONG a_Height , ULONG &a_MaxHeight ) ;
  138. WmiStatusCode RecursiveUnInitialize ( WmiAvlNode *a_Node ) ;
  139. WmiStatusCode Insert_LeftBalance (
  140. WmiAvlNode *&a_Node ,
  141. WmiAvlNode *a_Left ,
  142. bool &a_Increased
  143. ) ;
  144. WmiStatusCode Insert_RightBalance (
  145. WmiAvlNode *&a_Node ,
  146. WmiAvlNode *a_Right ,
  147. bool &a_Increased
  148. ) ;
  149. WmiStatusCode Delete_LeftBalance (
  150. WmiAvlNode *&a_Node ,
  151. WmiAvlNode *a_Left ,
  152. bool &a_Increased
  153. ) ;
  154. WmiStatusCode Delete_RightBalance (
  155. WmiAvlNode *&a_Node ,
  156. WmiAvlNode *a_Right ,
  157. bool &a_Increased
  158. ) ;
  159. #if 0
  160. WmiStatusCode RecursiveDelete ( WmiAvlNode *a_Root , const WmiKey &a_Key , bool &a_Decreased ) ;
  161. WmiStatusCode RecursiveInsert ( WmiAvlNode *a_Root , WmiAvlNode *a_Node , bool &a_Increased ) ;
  162. #endif
  163. public:
  164. WmiAvlTree (
  165. WmiAllocator &a_Allocator
  166. ) ;
  167. ~WmiAvlTree () ;
  168. WmiStatusCode Initialize () ;
  169. WmiStatusCode UnInitialize () ;
  170. WmiStatusCode Insert (
  171. const WmiKey &a_Key ,
  172. const WmiElement &a_Element ,
  173. Iterator &a_Iterator
  174. ) ;
  175. WmiStatusCode Delete (
  176. const WmiKey &a_Key
  177. ) ;
  178. WmiStatusCode Find (
  179. const WmiKey &a_Key ,
  180. Iterator &a_Iterator
  181. ) ;
  182. WmiStatusCode FindNext (
  183. const WmiKey &a_Key ,
  184. Iterator &a_Iterator
  185. ) ;
  186. WmiStatusCode Merge (
  187. WmiAvlTree <WmiKey,WmiElement> &a_Tree
  188. ) ;
  189. WmiStatusCode Check ( ULONG &a_MaxHeight ) ;
  190. ULONG Size () { return m_Size ; } ;
  191. Iterator Begin () { return Iterator ( m_Root ).LeftMost () ; };
  192. Iterator End () { return Iterator ( m_Root ).RightMost () ; }
  193. Iterator Root () { return Iterator ( m_Root ) ; }
  194. } ;
  195. #include <AvlTree.cpp>
  196. #endif _AVLTREE_H