Endpoint.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ** Copyright (C) 1996, 1997 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: Endpoint.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. **
  10. ** History:
  11. */
  12. #include "pch.h"
  13. #define CUTOFF 16 //Subarrays this size or lower are sorted using shortSort
  14. //Make it easy to reuses this routine for other specialized sorts
  15. #define DataType Endpoint*
  16. //swap two array elements
  17. static inline void swap(DataType* a,
  18. DataType* b)
  19. {
  20. assert (a != b);
  21. DataType tmp = *a;
  22. *a = *b;
  23. *b = tmp;
  24. }
  25. //compare two array elements
  26. static inline int comp(DataType const * a,
  27. DataType const * b)
  28. {
  29. return ((*a)->value > (*b)->value)
  30. ? 1
  31. : (((*a)->value < (*b)->value)
  32. ? -1
  33. : (int)((*b)->highF - (*a)->highF)); //In the event of a tie, high endpoints go before low endpoints
  34. }
  35. //sort the array between lo and hi (inclusive)
  36. void Endpoint::longSort(DataType* lo,
  37. DataType* hi)
  38. {
  39. assert (lo);
  40. assert (hi);
  41. if (lo != hi)
  42. {
  43. int stkptr = 0; /* stack for saving sub-array to be processed */
  44. DataType* lostk[30];
  45. DataType* histk[30];
  46. /* Note: the number of stack entries required is no more than
  47. 1 + log2(size), so 30 is sufficient for any array */
  48. assert (lo < hi);
  49. /* this entry point is for pseudo-recursion calling: setting
  50. lo and hi and jumping to here is like recursion, but stkptr is
  51. prserved, locals aren't, so we preserve stuff on the stack */
  52. recurse:
  53. unsigned size = (hi - lo) + 1; /* number of el's to sort */
  54. /* below a certain size, it is faster to use a O(n^2) sorting method */
  55. if (size <= CUTOFF)
  56. {
  57. shortSort(lo, hi);
  58. }
  59. else
  60. {
  61. /* First we pick a partititioning element. The efficiency of the
  62. algorithm demands that we find one that is approximately the
  63. median of the values, but also that we select one fast. Using
  64. the first one produces bad performace if the array is already
  65. sorted, so we use the middle one, which would require a very
  66. wierdly arranged array for worst case performance. Testing shows
  67. that a median-of-three algorithm does not, in general, increase
  68. performance. */
  69. DataType* mid = &lo[size >> 1]; /* find middle element */
  70. swap(mid, lo); /* swap it to beginning of array */
  71. /* We now wish to partition the array into three pieces, one
  72. consisiting of elements <= partition element, one of elements
  73. equal to the parition element, and one of element >= to it. This
  74. is done below; comments indicate conditions established at every
  75. step. */
  76. DataType* loguy = lo;
  77. DataType* higuy = hi + 1;
  78. /* Note that higuy decreases and loguy increases on every iteration,
  79. so loop must terminate. */
  80. for (;;)
  81. {
  82. /* lo <= loguy < hi, lo < higuy <= hi + 1,
  83. A[i] <= A[lo] for lo <= i <= loguy,
  84. A[i] >= A[lo] for higuy <= i <= hi */
  85. do
  86. {
  87. loguy += 1;
  88. } while (loguy <= hi && comp(loguy, lo) <= 0);
  89. /* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
  90. either loguy > hi or A[loguy] > A[lo] */
  91. do
  92. {
  93. higuy -= 1;
  94. } while (higuy > lo && comp(higuy, lo) >= 0);
  95. /* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
  96. either higuy <= lo or A[higuy] < A[lo] */
  97. if (higuy < loguy)
  98. break;
  99. /* if loguy > hi or higuy <= lo, then we would have exited, so
  100. A[loguy] > A[lo], A[higuy] < A[lo],
  101. loguy < hi, highy > lo */
  102. swap(loguy, higuy);
  103. /* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
  104. of loop is re-established */
  105. }
  106. /* A[i] >= A[lo] for higuy < i <= hi,
  107. A[i] <= A[lo] for lo <= i < loguy,
  108. higuy < loguy, lo <= higuy <= hi
  109. implying:
  110. A[i] >= A[lo] for loguy <= i <= hi,
  111. A[i] <= A[lo] for lo <= i <= higuy,
  112. A[i] = A[lo] for higuy < i < loguy */
  113. if (lo != higuy)
  114. swap(lo, higuy); /* put partition element in place */
  115. /* OK, now we have the following:
  116. A[i] >= A[higuy] for loguy <= i <= hi,
  117. A[i] <= A[higuy] for lo <= i < higuy
  118. A[i] = A[lo] for higuy <= i < loguy */
  119. /* We've finished the partition, now we want to sort the subarrays
  120. [lo, higuy-1] and [loguy, hi].
  121. We do the smaller one first to minimize stack usage.
  122. We only sort arrays of length 2 or more.*/
  123. if ( higuy - lo > hi - loguy ) //was this: if ( higuy - 1 - lo >= hi - loguy )
  124. {
  125. if (lo + 1 < higuy)
  126. {
  127. lostk[stkptr] = lo;
  128. histk[stkptr] = higuy - 1;
  129. ++stkptr;
  130. } /* save big recursion for later */
  131. if (loguy < hi) {
  132. lo = loguy;
  133. goto recurse; /* do small recursion */
  134. }
  135. }
  136. else
  137. {
  138. if (loguy < hi)
  139. {
  140. lostk[stkptr] = loguy;
  141. histk[stkptr] = hi;
  142. ++stkptr; /* save big recursion for later */
  143. }
  144. if (lo + 1 < higuy) {
  145. hi = higuy - 1;
  146. goto recurse; /* do small recursion */
  147. }
  148. }
  149. }
  150. /* We have sorted the array, except for any pending sorts on the stack.
  151. Check if there are any, and do them. */
  152. --stkptr;
  153. if (stkptr >= 0)
  154. {
  155. lo = lostk[stkptr];
  156. hi = histk[stkptr];
  157. goto recurse; /* pop subarray from stack */
  158. }
  159. }
  160. }
  161. //Bubble-sort the array between lo and hi (inclusive)
  162. void Endpoint::shortSort(DataType* lo,
  163. DataType* hi)
  164. {
  165. assert (lo < hi);
  166. //Use a bubble sort since that works well when the list is almost sorted (since the
  167. //sort will terminate early and most things will not have to bubble far).
  168. DataType* p = lo; //[lo, p] is sorted
  169. do
  170. {
  171. DataType* next = p + 1;
  172. if (comp(p, next) > 0)
  173. {
  174. //*p > *next (& therefore either p or next is out of order)
  175. //Assume it is next and move it to the correct location in the list.
  176. //Note: we know the list is sorted from lo to p so we could use a binary
  177. //search. But since the original list is supposed to be "almost sorted"
  178. //a linear search is probably faster.
  179. DataType* back = p - 1;
  180. while ((back >= lo) && (comp(back, next) > 0))
  181. back--;
  182. //Either at the start of the list or *back <= *next
  183. //In either case, insert next just after back
  184. DataType tmp = *next;
  185. DataType* bp1 = back + 1;
  186. DataType* t = next;
  187. assert (t > bp1);
  188. assert (bp1 >= lo);
  189. do
  190. {
  191. //This is a safer way of doing: *t = *(--t);
  192. DataType& scratch = *t;
  193. scratch = *(--t);
  194. }
  195. while (t > bp1);
  196. *bp1 = tmp;
  197. }
  198. p = next;
  199. }
  200. while (p < hi);
  201. }