priorityq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  3. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice including the dates of first publication and
  13. * either this permission notice or a reference to
  14. * http://oss.sgi.com/projects/FreeB/
  15. * shall be included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization from
  28. * Silicon Graphics, Inc.
  29. */
  30. /*
  31. ** Author: Eric Veach, July 1994.
  32. **
  33. */
  34. #include "gluos.h"
  35. #include <stddef.h>
  36. #include <assert.h>
  37. #include <limits.h> /* LONG_MAX */
  38. #include "memalloc.h"
  39. /* Include all the code for the regular heap-based queue here. */
  40. #include "priorityq-heap.c"
  41. /* Now redefine all the function names to map to their "Sort" versions. */
  42. #include "priorityq-sort.h"
  43. /* really __gl_pqSortNewPriorityQ */
  44. PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
  45. {
  46. PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
  47. if (pq == NULL) return NULL;
  48. pq->heap = __gl_pqHeapNewPriorityQ( leq );
  49. if (pq->heap == NULL) {
  50. memFree(pq);
  51. return NULL;
  52. }
  53. pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE * sizeof(pq->keys[0]) );
  54. if (pq->keys == NULL) {
  55. __gl_pqHeapDeletePriorityQ(pq->heap);
  56. memFree(pq);
  57. return NULL;
  58. }
  59. pq->size = 0;
  60. pq->max = INIT_SIZE;
  61. pq->initialized = FALSE;
  62. pq->leq = leq;
  63. return pq;
  64. }
  65. /* really __gl_pqSortDeletePriorityQ */
  66. void pqDeletePriorityQ( PriorityQ *pq )
  67. {
  68. assert(pq != NULL);
  69. if (pq->heap != NULL) __gl_pqHeapDeletePriorityQ( pq->heap );
  70. if (pq->order != NULL) memFree( pq->order );
  71. if (pq->keys != NULL) memFree( pq->keys );
  72. memFree( pq );
  73. }
  74. #define LT(x,y) (! LEQ(y,x))
  75. #define GT(x,y) (! LEQ(x,y))
  76. #define Swap(a,b) do{PQkey *tmp = *a; *a = *b; *b = tmp;}while(0)
  77. /* really __gl_pqSortInit */
  78. int pqInit( PriorityQ *pq )
  79. {
  80. PQkey **p, **r, **i, **j, *piv;
  81. struct { PQkey **p, **r; } Stack[50], *top = Stack;
  82. unsigned long seed = 2016473283;
  83. /* Create an array of indirect pointers to the keys, so that we
  84. * the handles we have returned are still valid.
  85. */
  86. /*
  87. pq->order = (PQHeapKey **)memAlloc( (size_t)
  88. (pq->size * sizeof(pq->order[0])) );
  89. */
  90. pq->order = (PQHeapKey **)memAlloc( (size_t)
  91. ((pq->size+1) * sizeof(pq->order[0])) );
  92. /* the previous line is a patch to compensate for the fact that IBM */
  93. /* machines return a null on a malloc of zero bytes (unlike SGI), */
  94. /* so we have to put in this defense to guard against a memory */
  95. /* fault four lines down. from fossum@austin.ibm.com. */
  96. if (pq->order == NULL) return 0;
  97. p = pq->order;
  98. r = p + pq->size - 1;
  99. for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) {
  100. *i = piv;
  101. }
  102. /* Sort the indirect pointers in descending order,
  103. * using randomized Quicksort
  104. */
  105. top->p = p; top->r = r; ++top;
  106. while( --top >= Stack ) {
  107. p = top->p;
  108. r = top->r;
  109. while( r > p + 10 ) {
  110. seed = seed * 1539415821 + 1;
  111. i = p + seed % (r - p + 1);
  112. piv = *i;
  113. *i = *p;
  114. *p = piv;
  115. i = p - 1;
  116. j = r + 1;
  117. do {
  118. do { ++i; } while( GT( **i, *piv ));
  119. do { --j; } while( LT( **j, *piv ));
  120. Swap( i, j );
  121. } while( i < j );
  122. Swap( i, j ); /* Undo last swap */
  123. if( i - p < r - j ) {
  124. top->p = j+1; top->r = r; ++top;
  125. r = i-1;
  126. } else {
  127. top->p = p; top->r = i-1; ++top;
  128. p = j+1;
  129. }
  130. }
  131. /* Insertion sort small lists */
  132. for( i = p+1; i <= r; ++i ) {
  133. piv = *i;
  134. for( j = i; j > p && LT( **(j-1), *piv ); --j ) {
  135. *j = *(j-1);
  136. }
  137. *j = piv;
  138. }
  139. }
  140. pq->max = pq->size;
  141. pq->initialized = TRUE;
  142. __gl_pqHeapInit( pq->heap ); /* always succeeds */
  143. #ifndef NDEBUG
  144. p = pq->order;
  145. r = p + pq->size - 1;
  146. for( i = p; i < r; ++i ) {
  147. assert( LEQ( **(i+1), **i ));
  148. }
  149. #endif
  150. return 1;
  151. }
  152. /* really __gl_pqSortInsert */
  153. /* returns LONG_MAX iff out of memory */
  154. PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
  155. {
  156. long curr;
  157. if( pq->initialized ) {
  158. return __gl_pqHeapInsert( pq->heap, keyNew );
  159. }
  160. curr = pq->size;
  161. if( ++ pq->size >= pq->max ) {
  162. PQkey *saveKey= pq->keys;
  163. /* If the heap overflows, double its size. */
  164. pq->max <<= 1;
  165. pq->keys = (PQHeapKey *)memRealloc( pq->keys,
  166. (size_t)
  167. (pq->max * sizeof( pq->keys[0] )));
  168. if (pq->keys == NULL) {
  169. pq->keys = saveKey; /* restore ptr to free upon return */
  170. return LONG_MAX;
  171. }
  172. }
  173. assert(curr != LONG_MAX);
  174. pq->keys[curr] = keyNew;
  175. /* Negative handles index the sorted array. */
  176. return -(curr+1);
  177. }
  178. /* really __gl_pqSortExtractMin */
  179. PQkey pqExtractMin( PriorityQ *pq )
  180. {
  181. PQkey sortMin, heapMin;
  182. if( pq->size == 0 ) {
  183. return __gl_pqHeapExtractMin( pq->heap );
  184. }
  185. sortMin = *(pq->order[pq->size-1]);
  186. if( ! __gl_pqHeapIsEmpty( pq->heap )) {
  187. heapMin = __gl_pqHeapMinimum( pq->heap );
  188. if( LEQ( heapMin, sortMin )) {
  189. return __gl_pqHeapExtractMin( pq->heap );
  190. }
  191. }
  192. do {
  193. -- pq->size;
  194. } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL );
  195. return sortMin;
  196. }
  197. /* really __gl_pqSortMinimum */
  198. PQkey pqMinimum( PriorityQ *pq )
  199. {
  200. PQkey sortMin, heapMin;
  201. if( pq->size == 0 ) {
  202. return __gl_pqHeapMinimum( pq->heap );
  203. }
  204. sortMin = *(pq->order[pq->size-1]);
  205. if( ! __gl_pqHeapIsEmpty( pq->heap )) {
  206. heapMin = __gl_pqHeapMinimum( pq->heap );
  207. if( LEQ( heapMin, sortMin )) {
  208. return heapMin;
  209. }
  210. }
  211. return sortMin;
  212. }
  213. /* really __gl_pqSortIsEmpty */
  214. int pqIsEmpty( PriorityQ *pq )
  215. {
  216. return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
  217. }
  218. /* really __gl_pqSortDelete */
  219. void pqDelete( PriorityQ *pq, PQhandle curr )
  220. {
  221. if( curr >= 0 ) {
  222. __gl_pqHeapDelete( pq->heap, curr );
  223. return;
  224. }
  225. curr = -(curr+1);
  226. assert( curr < pq->max && pq->keys[curr] != NULL );
  227. pq->keys[curr] = NULL;
  228. while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
  229. -- pq->size;
  230. }
  231. }