priorityq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. typedef double GLdouble;
  35. typedef unsigned char GLboolean;
  36. //#include "gluos.h"
  37. #include <stddef.h>
  38. #include <assert.h>
  39. #include <limits.h> /* LONG_MAX */
  40. #include "memalloc.h"
  41. /* Include all the code for the regular heap-based queue here. */
  42. #include "priorityq-heap.c"
  43. /* Now redefine all the function names to map to their "Sort" versions. */
  44. #include "priorityq-sort.h"
  45. /* really __gl_pqSortNewPriorityQ */
  46. PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
  47. {
  48. PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
  49. if (pq == NULL) return NULL;
  50. pq->heap = __gl_pqHeapNewPriorityQ( leq );
  51. if (pq->heap == NULL) {
  52. memFree(pq);
  53. return NULL;
  54. }
  55. pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE * sizeof(pq->keys[0]) );
  56. if (pq->keys == NULL) {
  57. __gl_pqHeapDeletePriorityQ(pq->heap);
  58. memFree(pq);
  59. return NULL;
  60. }
  61. pq->size = 0;
  62. pq->max = INIT_SIZE;
  63. pq->initialized = FALSE;
  64. pq->leq = leq;
  65. return pq;
  66. }
  67. /* really __gl_pqSortDeletePriorityQ */
  68. void pqDeletePriorityQ( PriorityQ *pq )
  69. {
  70. assert(pq != NULL);
  71. if (pq->heap != NULL) __gl_pqHeapDeletePriorityQ( pq->heap );
  72. if (pq->order != NULL) memFree( pq->order );
  73. if (pq->keys != NULL) memFree( pq->keys );
  74. memFree( pq );
  75. }
  76. #define LT(x,y) (! LEQ(y,x))
  77. #define GT(x,y) (! LEQ(x,y))
  78. #define Swap(a,b) if(1){PQkey *tmp = *a; *a = *b; *b = tmp;}else
  79. /* really __gl_pqSortInit */
  80. int pqInit( PriorityQ *pq )
  81. {
  82. PQkey **p, **r, **i, **j, *piv;
  83. struct { PQkey **p, **r; } Stack[50], *top = Stack;
  84. unsigned long seed = 2016473283;
  85. /* Create an array of indirect pointers to the keys, so that we
  86. * the handles we have returned are still valid.
  87. */
  88. /*
  89. pq->order = (PQHeapKey **)memAlloc( (size_t)
  90. (pq->size * sizeof(pq->order[0])) );
  91. */
  92. pq->order = (PQHeapKey **)memAlloc( (size_t)
  93. ((pq->size+1) * sizeof(pq->order[0])) );
  94. /* the previous line is a patch to compensate for the fact that IBM */
  95. /* machines return a null on a malloc of zero bytes (unlike SGI), */
  96. /* so we have to put in this defense to guard against a memory */
  97. /* fault four lines down. from fossum@austin.ibm.com. */
  98. if (pq->order == NULL) return 0;
  99. p = pq->order;
  100. r = p + pq->size - 1;
  101. for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) {
  102. *i = piv;
  103. }
  104. /* Sort the indirect pointers in descending order,
  105. * using randomized Quicksort
  106. */
  107. top->p = p; top->r = r; ++top;
  108. while( --top >= Stack ) {
  109. p = top->p;
  110. r = top->r;
  111. while( r > p + 10 ) {
  112. seed = seed * 1539415821 + 1;
  113. i = p + seed % (r - p + 1);
  114. piv = *i;
  115. *i = *p;
  116. *p = piv;
  117. i = p - 1;
  118. j = r + 1;
  119. do {
  120. do { ++i; } while( GT( **i, *piv ));
  121. do { --j; } while( LT( **j, *piv ));
  122. Swap( i, j );
  123. } while( i < j );
  124. Swap( i, j ); /* Undo last swap */
  125. if( i - p < r - j ) {
  126. top->p = j+1; top->r = r; ++top;
  127. r = i-1;
  128. } else {
  129. top->p = p; top->r = i-1; ++top;
  130. p = j+1;
  131. }
  132. }
  133. /* Insertion sort small lists */
  134. for( i = p+1; i <= r; ++i ) {
  135. piv = *i;
  136. for( j = i; j > p && LT( **(j-1), *piv ); --j ) {
  137. *j = *(j-1);
  138. }
  139. *j = piv;
  140. }
  141. }
  142. pq->max = pq->size;
  143. pq->initialized = TRUE;
  144. __gl_pqHeapInit( pq->heap ); /* always succeeds */
  145. #ifndef NDEBUG
  146. p = pq->order;
  147. r = p + pq->size - 1;
  148. for( i = p; i < r; ++i ) {
  149. assert( LEQ( **(i+1), **i ));
  150. }
  151. #endif
  152. return 1;
  153. }
  154. /* really __gl_pqSortInsert */
  155. /* returns LONG_MAX iff out of memory */
  156. PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
  157. {
  158. long curr;
  159. if( pq->initialized ) {
  160. return __gl_pqHeapInsert( pq->heap, keyNew );
  161. }
  162. curr = pq->size;
  163. if( ++ pq->size >= pq->max ) {
  164. PQkey *saveKey= pq->keys;
  165. /* If the heap overflows, double its size. */
  166. pq->max <<= 1;
  167. pq->keys = (PQHeapKey *)memRealloc( pq->keys,
  168. (size_t)
  169. (pq->max * sizeof( pq->keys[0] )));
  170. if (pq->keys == NULL) {
  171. pq->keys = saveKey; /* restore ptr to free upon return */
  172. return LONG_MAX;
  173. }
  174. }
  175. assert(curr != LONG_MAX);
  176. pq->keys[curr] = keyNew;
  177. /* Negative handles index the sorted array. */
  178. return -(curr+1);
  179. }
  180. /* really __gl_pqSortExtractMin */
  181. PQkey pqExtractMin( PriorityQ *pq )
  182. {
  183. PQkey sortMin, heapMin;
  184. if( pq->size == 0 ) {
  185. return __gl_pqHeapExtractMin( pq->heap );
  186. }
  187. sortMin = *(pq->order[pq->size-1]);
  188. if( ! __gl_pqHeapIsEmpty( pq->heap )) {
  189. heapMin = __gl_pqHeapMinimum( pq->heap );
  190. if( LEQ( heapMin, sortMin )) {
  191. return __gl_pqHeapExtractMin( pq->heap );
  192. }
  193. }
  194. do {
  195. -- pq->size;
  196. } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL );
  197. return sortMin;
  198. }
  199. /* really __gl_pqSortMinimum */
  200. PQkey pqMinimum( PriorityQ *pq )
  201. {
  202. PQkey sortMin, heapMin;
  203. if( pq->size == 0 ) {
  204. return __gl_pqHeapMinimum( pq->heap );
  205. }
  206. sortMin = *(pq->order[pq->size-1]);
  207. if( ! __gl_pqHeapIsEmpty( pq->heap )) {
  208. heapMin = __gl_pqHeapMinimum( pq->heap );
  209. if( LEQ( heapMin, sortMin )) {
  210. return heapMin;
  211. }
  212. }
  213. return sortMin;
  214. }
  215. /* really __gl_pqSortIsEmpty */
  216. int pqIsEmpty( PriorityQ *pq )
  217. {
  218. return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
  219. }
  220. /* really __gl_pqSortDelete */
  221. void pqDelete( PriorityQ *pq, PQhandle curr )
  222. {
  223. if( curr >= 0 ) {
  224. __gl_pqHeapDelete( pq->heap, curr );
  225. return;
  226. }
  227. curr = -(curr+1);
  228. assert( curr < pq->max && pq->keys[curr] != NULL );
  229. pq->keys[curr] = NULL;
  230. while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
  231. -- pq->size;
  232. }
  233. }