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.

253 lines
5.4 KiB

  1. #ifndef __ALGORITHMS_CPP
  2. #define __ALGORITHMS_CPP
  3. /*
  4. * Class:
  5. *
  6. * WmiAllocator
  7. *
  8. * Description:
  9. *
  10. * Provides abstraction above heap allocation functions
  11. *
  12. * Version:
  13. *
  14. * Initial
  15. *
  16. * Last Changed:
  17. *
  18. * See Source Depot for change history
  19. *
  20. */
  21. #include <Stack.h>
  22. /******************************************************************************
  23. *
  24. * Name:
  25. *
  26. *
  27. * Description:
  28. *
  29. *
  30. *****************************************************************************/
  31. #ifdef WMI_CONTAINER_PERFORMANCE_TESTING
  32. extern ULONG g_Compare ;
  33. #endif
  34. template <class WmiElement>
  35. LONG CompareElement ( const WmiElement &a_Arg1 , const WmiElement &a_Arg2 )
  36. {
  37. #ifdef WMI_CONTAINER_PERFORMANCE_TESTING
  38. g_Compare ++ ;
  39. #endif
  40. if ( a_Arg1 == a_Arg2 )
  41. {
  42. return 0 ;
  43. }
  44. else if ( a_Arg1 < a_Arg2 )
  45. {
  46. return -1 ;
  47. }
  48. return 1 ;
  49. }
  50. /******************************************************************************
  51. *
  52. * Name:
  53. *
  54. *
  55. * Description:
  56. *
  57. *
  58. *****************************************************************************/
  59. class StackElement
  60. {
  61. public:
  62. ULONG m_Lower ;
  63. ULONG m_Upper ;
  64. StackElement () {;}
  65. } ;
  66. /******************************************************************************
  67. *
  68. * Name:
  69. *
  70. *
  71. * Description:
  72. *
  73. *
  74. *****************************************************************************/
  75. template <class WmiElement>
  76. WmiStatusCode Flat_QuickSort ( WmiElement *a_Array , ULONG a_Size )
  77. {
  78. if ( a_Size )
  79. {
  80. WmiAllocator t_Allocator ;
  81. WmiStatusCode t_StatusCode = t_Allocator.Initialize () ;
  82. if ( t_StatusCode == e_StatusCode_Success )
  83. {
  84. WmiStack <StackElement,8> t_Stack ( t_Allocator ) ;
  85. StackElement t_Element ;
  86. t_Element.m_Lower = 1 ;
  87. t_Element.m_Upper = a_Size - 1 ;
  88. t_Stack.Push ( t_Element ) ;
  89. while ( t_Stack.Size () )
  90. {
  91. StackElement t_Top ;
  92. WmiStatusCode t_StatusCode = t_Stack.Top ( t_Top ) ;
  93. t_StatusCode = t_Stack.Pop () ;
  94. if ( t_StatusCode == e_StatusCode_Success )
  95. {
  96. if ( t_Top.m_Lower <= t_Top.m_Upper )
  97. {
  98. ULONG t_LeftIndex = t_Top.m_Lower ;
  99. ULONG t_RightIndex = t_Top.m_Upper ;
  100. while ( true )
  101. {
  102. while ( ( t_LeftIndex < t_RightIndex ) && ( CompareElement ( a_Array [ t_LeftIndex ] , a_Array [ t_Top.m_Lower - 1 ] ) <= 0 ) )
  103. {
  104. t_LeftIndex ++ ;
  105. }
  106. while ( ( t_LeftIndex < t_RightIndex ) && ( CompareElement ( a_Array [ t_Top.m_Lower - 1 ] , a_Array [ t_RightIndex ] ) <= 0 ) )
  107. {
  108. t_RightIndex -- ;
  109. }
  110. if ( t_LeftIndex < t_RightIndex )
  111. {
  112. WmiElement t_Temp = a_Array [ t_LeftIndex ] ;
  113. a_Array [ t_LeftIndex ] = a_Array [ t_RightIndex ] ;
  114. a_Array [ t_RightIndex ] = t_Temp ;
  115. }
  116. else
  117. {
  118. break ;
  119. }
  120. }
  121. LONG t_Compare = CompareElement ( a_Array [ t_LeftIndex ] , a_Array [ t_Top.m_Lower - 1 ] ) ;
  122. if ( t_Compare < 0 )
  123. {
  124. WmiElement t_Temp = a_Array [ t_LeftIndex ] ;
  125. a_Array [ t_LeftIndex ] = a_Array [ t_Top.m_Lower - 1 ] ;
  126. a_Array [ t_Top.m_Lower - 1 ] = t_Temp ;
  127. }
  128. StackElement t_Element ;
  129. t_Element.m_Lower = t_Top.m_Lower ;
  130. t_Element.m_Upper = t_LeftIndex - 1 ;
  131. t_StatusCode = t_Stack.Push ( t_Element ) ;
  132. t_Element.m_Lower = t_LeftIndex + 1 ;
  133. t_Element.m_Upper = t_Top.m_Upper ;
  134. t_StatusCode = t_Stack.Push ( t_Element ) ;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. return e_StatusCode_Success ;
  141. }
  142. /******************************************************************************
  143. *
  144. * Name:
  145. *
  146. *
  147. * Description:
  148. *
  149. *
  150. *****************************************************************************/
  151. template <class WmiElement>
  152. void RecursiveQuickSort ( WmiElement *a_Array , ULONG a_Lower , ULONG a_Upper )
  153. {
  154. if ( a_Lower <= a_Upper )
  155. {
  156. ULONG t_LeftIndex = a_Lower ;
  157. ULONG t_RightIndex = a_Upper ;
  158. while ( true )
  159. {
  160. while ( ( t_LeftIndex < t_RightIndex ) && ( CompareElement ( a_Array [ t_LeftIndex ] , a_Array [ a_Lower - 1 ] ) <= 0 ) )
  161. {
  162. t_LeftIndex ++ ;
  163. }
  164. while ( ( t_LeftIndex < t_RightIndex ) && ( CompareElement ( a_Array [ a_Lower - 1 ] , a_Array [ t_RightIndex ] ) <= 0 ) )
  165. {
  166. t_RightIndex -- ;
  167. }
  168. if ( t_LeftIndex < t_RightIndex )
  169. {
  170. WmiElement t_Temp = a_Array [ t_LeftIndex ] ;
  171. a_Array [ t_LeftIndex ] = a_Array [ t_RightIndex ] ;
  172. a_Array [ t_RightIndex ] = t_Temp ;
  173. }
  174. else
  175. {
  176. break ;
  177. }
  178. }
  179. LONG t_Compare = CompareElement ( a_Array [ t_LeftIndex ] , a_Array [ a_Lower - 1 ] ) ;
  180. if ( t_Compare < 0 )
  181. {
  182. WmiElement t_Temp = a_Array [ t_LeftIndex ] ;
  183. a_Array [ t_LeftIndex ] = a_Array [ a_Lower - 1 ] ;
  184. a_Array [ a_Lower - 1 ] = t_Temp ;
  185. }
  186. RecursiveQuickSort (
  187. a_Array ,
  188. a_Lower ,
  189. t_LeftIndex - 1
  190. ) ;
  191. RecursiveQuickSort (
  192. a_Array ,
  193. t_LeftIndex + 1 ,
  194. a_Upper
  195. ) ;
  196. }
  197. }
  198. /******************************************************************************
  199. *
  200. * Name:
  201. *
  202. *
  203. * Description:
  204. *
  205. *
  206. *****************************************************************************/
  207. template <class WmiElement>
  208. WmiStatusCode QuickSort ( WmiElement *a_Array , ULONG a_Size )
  209. {
  210. RecursiveQuickSort ( a_Array , 1 , a_Size - 1 ) ;
  211. return e_StatusCode_Success ;
  212. }
  213. #endif __ALGORITHMS_CPP