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.

191 lines
6.2 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // InsertionSort.h
  7. //
  8. // Description:
  9. // This file contains templates to perform an insertion sort on an array.
  10. //
  11. // Documentation:
  12. //
  13. // Implementation Files:
  14. // None.
  15. //
  16. // Maintained By:
  17. // John Franco (jfranco) 1-JUN-2001
  18. //
  19. //////////////////////////////////////////////////////////////////////////////
  20. // Make sure that this file is included only once per compile path.
  21. #pragma once
  22. //////////////////////////////////////////////////////////////////////////////
  23. // Include Files
  24. //////////////////////////////////////////////////////////////////////////////
  25. //////////////////////////////////////////////////////////////////////////////
  26. // Constant Declarations
  27. //////////////////////////////////////////////////////////////////////////////
  28. //////////////////////////////////////////////////////////////////////////////
  29. // Template Declarations
  30. //////////////////////////////////////////////////////////////////////////////
  31. //////////////////////////////////////////////////////////////////////////////
  32. //++
  33. //
  34. // InsertionSort< Item, PredecessorFunction > template
  35. //
  36. // Description:
  37. // InsertionSort(Start, Size, Precedes)
  38. // reorders the array of Size elements beginning at Start so that
  39. // for all p and q in [Start, Start + Size),
  40. // Precedes(*p, *q) implies p < q.
  41. //
  42. // The notation [begin, end) refers to a half-open interval, so that
  43. // for ( Item* pItem = begin; pItem < end; ++pItem )
  44. // iterates through all elements in the array, and
  45. // end - begin
  46. // gives the number of elements in the array.
  47. //
  48. // Template Arguments:
  49. // Item - the type of the elements in the array.
  50. // requirements for Item:
  51. // - copy constructor
  52. // - assignment operator
  53. //
  54. // PredecessorFunction
  55. // The function that determines whether one element should precede
  56. // another in the sort order
  57. //
  58. // requirements for PredecessorFunction:
  59. // - bool operator()( const Item &, const Item & ) const;
  60. // or, PredecessorFunction can be a pointer to a function
  61. // taking two Items and returning a bool.
  62. //
  63. // Function Arguments:
  64. // pArrayStartIn
  65. // A pointer to the first element of the array.
  66. //
  67. // cArraySizeIn
  68. // The number of elements in the array.
  69. //
  70. // rPrecedesIn
  71. // An instance of the predecessor function class, or a pointer to
  72. // a function with the appropriate signature.
  73. //
  74. // Return Values:
  75. // None.
  76. //
  77. //--
  78. //////////////////////////////////////////////////////////////////////////////
  79. template < class Item, class PredecessorFunction >
  80. void
  81. InsertionSort(
  82. Item * pArrayStartIn
  83. , size_t cArraySizeIn
  84. , const PredecessorFunction & rPrecedesIn )
  85. {
  86. Assert( pArrayStartIn != NULL );
  87. Item * pCurrentItem;
  88. Item * pSortedLocation;
  89. Item * pNextToCopy;
  90. Item * pArrayEnd = pArrayStartIn + cArraySizeIn;
  91. //
  92. // Insertion sort algorithm; for a good description, see "Introduction to Algorithms"
  93. // by Cormen, Leiserson, and Rivest.
  94. //
  95. //
  96. // Loop invariant: all items to the left of pCurrentItem are in sorted order.
  97. // Arrays of size zero or one are considered to be already sorted, so start
  98. // pCurrentItem at second element in the array (if more than one element exists).
  99. //
  100. for ( pCurrentItem = pArrayStartIn + 1; pCurrentItem < pArrayEnd; ++pCurrentItem )
  101. {
  102. //
  103. // Find where the current item needs to go to make the loop invariant true for pCurrentItem + 1.
  104. // This could be a binary search, but collections of large enough size that it matters
  105. // should use a quick sort instead.
  106. //
  107. pSortedLocation = pCurrentItem;
  108. while ( ( pSortedLocation > pArrayStartIn )
  109. && rPrecedesIn( *pCurrentItem, *( pSortedLocation - 1 ) )
  110. )
  111. {
  112. --pSortedLocation;
  113. }
  114. // Does the current item need to move?
  115. if ( pSortedLocation != pCurrentItem )
  116. {
  117. // Store the current item.
  118. Item tempItem( *pCurrentItem ); // declared inline to avoid requiring default constructor for Item
  119. // Move all items in [pSortedLocation, pCurrentItem) to the right by one.
  120. for ( pNextToCopy = pCurrentItem ; pNextToCopy > pSortedLocation; --pNextToCopy )
  121. {
  122. *pNextToCopy = *( pNextToCopy - 1 );
  123. }
  124. // Copy the current item into its proper location
  125. *pSortedLocation = tempItem;
  126. } // if item not in sorted location
  127. } // for each item in array
  128. } //*** InsertionSort
  129. //////////////////////////////////////////////////////////////////////////////
  130. //++
  131. //
  132. // LessThan<Item> and InsertionSort<Item> templates
  133. //
  134. // Description:
  135. // These templates overload InsertionSort<Item, PredecessorFunction>
  136. // to make PredecessorFunction use the less-than operator by default when
  137. // the user provides no explicit predecessor function.
  138. //
  139. // Passing an instance of a function object allows the compiler to inline
  140. // the comparison operation, which is impossible with a function pointer.
  141. //
  142. // Template parameters:
  143. //
  144. // Item - the type of the elements in the array.
  145. // requirements for Item:
  146. // - bool operator < ( const Item & ) const
  147. // alternatively, Item can be a built-in type, like int
  148. // - those from InsertionSort<Item, PredecessorFunction>
  149. //
  150. //--
  151. //////////////////////////////////////////////////////////////////////////////
  152. template < class Item >
  153. struct LessThan
  154. {
  155. bool operator()( const Item & rLeftIn, const Item & rRightIn ) const
  156. {
  157. return ( rLeftIn < rRightIn );
  158. }
  159. };
  160. template < class Item >
  161. inline void
  162. InsertionSort( Item * pBeginIn, size_t cArraySizeIn )
  163. {
  164. InsertionSort( pBeginIn, cArraySizeIn, LessThan< Item >() );
  165. }