Source code of Windows XP (NT5)
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.

219 lines
5.0 KiB

  1. /*
  2. ** Copyright 1994, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. **
  17. ** Author: Eric Veach, July 1994.
  18. */
  19. #include <stddef.h>
  20. #include <assert.h>
  21. #include "memalloc.h"
  22. /* Include all the code for the regular heap-based queue here. */
  23. #ifdef NT
  24. #include "prq-heap.c"
  25. #else
  26. #include "priorityq-heap.c"
  27. #endif
  28. /* Now redefine all the function names to map to their "Sort" versions. */
  29. #ifdef NT
  30. #include "prq-sort.h"
  31. #else
  32. #include "priorityq-sort.h"
  33. #endif
  34. PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
  35. {
  36. PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
  37. pq->heap = __gl_pqHeapNewPriorityQ( leq );
  38. pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE * sizeof(pq->keys[0]) );
  39. pq->size = 0;
  40. pq->max = INIT_SIZE;
  41. pq->initialized = FALSE;
  42. pq->leq = leq;
  43. return pq;
  44. }
  45. void pqDeletePriorityQ( PriorityQ *pq )
  46. {
  47. __gl_pqHeapDeletePriorityQ( pq->heap );
  48. #ifdef NT
  49. memFree( pq->order );
  50. #endif
  51. memFree( pq->keys );
  52. memFree( pq );
  53. }
  54. #define LT(x,y) (! LEQ(y,x))
  55. #define GT(x,y) (! LEQ(x,y))
  56. #define Swap(a,b) if(1){PQkey *tmp = *a; *a = *b; *b = tmp;}else
  57. void pqInit( PriorityQ *pq )
  58. {
  59. PQkey **p, **r, **i, **j, *piv;
  60. struct { PQkey **p, **r; } Stack[50], *top = Stack;
  61. unsigned long seed = 2016473283;
  62. /* Create an array of indirect pointers to the keys, so that we
  63. * the handles we have returned are still valid.
  64. */
  65. pq->order = (PQHeapKey **)memAlloc( (size_t)
  66. (pq->size * sizeof(pq->order[0])) );
  67. p = pq->order;
  68. r = p + pq->size - 1;
  69. for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) {
  70. *i = piv;
  71. }
  72. /* Sort the indirect pointers in descending order,
  73. * using randomized Quicksort
  74. */
  75. top->p = p; top->r = r; ++top;
  76. while( --top >= Stack ) {
  77. p = top->p;
  78. r = top->r;
  79. while( r > p + 10 ) {
  80. seed = seed * 1539415821 + 1;
  81. i = p + seed % (r - p + 1);
  82. piv = *i;
  83. *i = *p;
  84. *p = piv;
  85. i = p - 1;
  86. j = r + 1;
  87. do {
  88. do { ++i; } while( GT( **i, *piv ));
  89. do { --j; } while( LT( **j, *piv ));
  90. Swap( i, j );
  91. } while( i < j );
  92. Swap( i, j ); /* Undo last swap */
  93. if( i - p < r - j ) {
  94. top->p = j+1; top->r = r; ++top;
  95. r = i-1;
  96. } else {
  97. top->p = p; top->r = i-1; ++top;
  98. p = j+1;
  99. }
  100. }
  101. /* Insertion sort small lists */
  102. for( i = p+1; i <= r; ++i ) {
  103. piv = *i;
  104. for( j = i; j > p && LT( **(j-1), *piv ); --j ) {
  105. *j = *(j-1);
  106. }
  107. *j = piv;
  108. }
  109. }
  110. pq->max = pq->size;
  111. pq->initialized = TRUE;
  112. __gl_pqHeapInit( pq->heap );
  113. #ifndef NDEBUG
  114. p = pq->order;
  115. r = p + pq->size - 1;
  116. for( i = p; i < r; ++i ) {
  117. assert( LEQ( **(i+1), **i ));
  118. }
  119. #endif
  120. }
  121. PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
  122. {
  123. long curr;
  124. if( pq->initialized ) {
  125. return __gl_pqHeapInsert( pq->heap, keyNew );
  126. }
  127. curr = pq->size;
  128. if( ++ pq->size >= pq->max ) {
  129. /* If the heap overflows, double its size. */
  130. pq->max <<= 1;
  131. pq->keys = (PQHeapKey *)memRealloc( pq->keys,
  132. (size_t)
  133. (pq->max * sizeof( pq->keys[0] )));
  134. }
  135. pq->keys[curr] = keyNew;
  136. /* Negative handles index the sorted array. */
  137. return -(curr+1);
  138. }
  139. PQkey pqExtractMin( PriorityQ *pq )
  140. {
  141. PQkey sortMin, heapMin;
  142. if( pq->size == 0 ) {
  143. return __gl_pqHeapExtractMin( pq->heap );
  144. }
  145. sortMin = *(pq->order[pq->size-1]);
  146. if( ! __gl_pqHeapIsEmpty( pq->heap )) {
  147. heapMin = __gl_pqHeapMinimum( pq->heap );
  148. if( LEQ( heapMin, sortMin )) {
  149. return __gl_pqHeapExtractMin( pq->heap );
  150. }
  151. }
  152. do {
  153. -- pq->size;
  154. } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL );
  155. return sortMin;
  156. }
  157. PQkey pqMinimum( PriorityQ *pq )
  158. {
  159. PQkey sortMin, heapMin;
  160. if( pq->size == 0 ) {
  161. return __gl_pqHeapMinimum( pq->heap );
  162. }
  163. sortMin = *(pq->order[pq->size-1]);
  164. if( ! __gl_pqHeapIsEmpty( pq->heap )) {
  165. heapMin = __gl_pqHeapMinimum( pq->heap );
  166. if( LEQ( heapMin, sortMin )) {
  167. return heapMin;
  168. }
  169. }
  170. return sortMin;
  171. }
  172. int pqIsEmpty( PriorityQ *pq )
  173. {
  174. return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
  175. }
  176. void pqDelete( PriorityQ *pq, PQhandle curr )
  177. {
  178. if( curr >= 0 ) {
  179. __gl_pqHeapDelete( pq->heap, curr );
  180. return;
  181. }
  182. curr = -(curr+1);
  183. assert( curr < pq->max && pq->keys[curr] != NULL );
  184. pq->keys[curr] = NULL;
  185. while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
  186. -- pq->size;
  187. }
  188. }