priorityq-heap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <stddef.h>
  35. #include <assert.h>
  36. #include <limits.h>
  37. #include "priorityq-heap.h"
  38. #include "memalloc.h"
  39. #define INIT_SIZE 32
  40. #define TRUE 1
  41. #define FALSE 0
  42. #ifdef FOR_TRITE_TEST_PROGRAM
  43. #define LEQ(x,y) (*pq->leq)(x,y)
  44. #else
  45. /* Violates modularity, but a little faster */
  46. #include "geom.h"
  47. #define LEQ(x,y) VertLeq((GLUvertex *)x, (GLUvertex *)y)
  48. #endif
  49. /* really __gl_pqHeapNewPriorityQ */
  50. PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
  51. {
  52. PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
  53. if (pq == NULL) return NULL;
  54. pq->size = 0;
  55. pq->max = INIT_SIZE;
  56. pq->nodes = (PQnode *)memAlloc( (INIT_SIZE + 1) * sizeof(pq->nodes[0]) );
  57. if (pq->nodes == NULL) {
  58. memFree(pq);
  59. return NULL;
  60. }
  61. pq->handles = (PQhandleElem *)memAlloc( (INIT_SIZE + 1) * sizeof(pq->handles[0]) );
  62. if (pq->handles == NULL) {
  63. memFree(pq->nodes);
  64. memFree(pq);
  65. return NULL;
  66. }
  67. pq->initialized = FALSE;
  68. pq->freeList = 0;
  69. pq->leq = leq;
  70. pq->nodes[1].handle = 1; /* so that Minimum() returns NULL */
  71. pq->handles[1].key = NULL;
  72. return pq;
  73. }
  74. /* really __gl_pqHeapDeletePriorityQ */
  75. void pqDeletePriorityQ( PriorityQ *pq )
  76. {
  77. memFree( pq->handles );
  78. memFree( pq->nodes );
  79. memFree( pq );
  80. }
  81. static void FloatDown( PriorityQ *pq, long curr )
  82. {
  83. PQnode *n = pq->nodes;
  84. PQhandleElem *h = pq->handles;
  85. PQhandle hCurr, hChild;
  86. long child;
  87. hCurr = n[curr].handle;
  88. for( ;; ) {
  89. child = curr << 1;
  90. if( child < pq->size && LEQ( h[n[child+1].handle].key,
  91. h[n[child].handle].key )) {
  92. ++child;
  93. }
  94. assert(child <= pq->max);
  95. hChild = n[child].handle;
  96. if( child > pq->size || LEQ( h[hCurr].key, h[hChild].key )) {
  97. n[curr].handle = hCurr;
  98. h[hCurr].node = curr;
  99. break;
  100. }
  101. n[curr].handle = hChild;
  102. h[hChild].node = curr;
  103. curr = child;
  104. }
  105. }
  106. static void FloatUp( PriorityQ *pq, long curr )
  107. {
  108. PQnode *n = pq->nodes;
  109. PQhandleElem *h = pq->handles;
  110. PQhandle hCurr, hParent;
  111. long parent;
  112. hCurr = n[curr].handle;
  113. for( ;; ) {
  114. parent = curr >> 1;
  115. hParent = n[parent].handle;
  116. if( parent == 0 || LEQ( h[hParent].key, h[hCurr].key )) {
  117. n[curr].handle = hCurr;
  118. h[hCurr].node = curr;
  119. break;
  120. }
  121. n[curr].handle = hParent;
  122. h[hParent].node = curr;
  123. curr = parent;
  124. }
  125. }
  126. /* really __gl_pqHeapInit */
  127. void pqInit( PriorityQ *pq )
  128. {
  129. long i;
  130. /* This method of building a heap is O(n), rather than O(n lg n). */
  131. for( i = pq->size; i >= 1; --i ) {
  132. FloatDown( pq, i );
  133. }
  134. pq->initialized = TRUE;
  135. }
  136. /* really __gl_pqHeapInsert */
  137. /* returns LONG_MAX iff out of memory */
  138. PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
  139. {
  140. long curr;
  141. PQhandle free;
  142. curr = ++ pq->size;
  143. if( (curr*2) > pq->max ) {
  144. PQnode *saveNodes= pq->nodes;
  145. PQhandleElem *saveHandles= pq->handles;
  146. /* If the heap overflows, double its size. */
  147. pq->max <<= 1;
  148. pq->nodes = (PQnode *)memRealloc( pq->nodes,
  149. (size_t)
  150. ((pq->max + 1) * sizeof( pq->nodes[0] )));
  151. if (pq->nodes == NULL) {
  152. pq->nodes = saveNodes; /* restore ptr to free upon return */
  153. return LONG_MAX;
  154. }
  155. pq->handles = (PQhandleElem *)memRealloc( pq->handles,
  156. (size_t)
  157. ((pq->max + 1) *
  158. sizeof( pq->handles[0] )));
  159. if (pq->handles == NULL) {
  160. pq->handles = saveHandles; /* restore ptr to free upon return */
  161. return LONG_MAX;
  162. }
  163. }
  164. if( pq->freeList == 0 ) {
  165. free = curr;
  166. } else {
  167. free = pq->freeList;
  168. pq->freeList = pq->handles[free].node;
  169. }
  170. pq->nodes[curr].handle = free;
  171. pq->handles[free].node = curr;
  172. pq->handles[free].key = keyNew;
  173. if( pq->initialized ) {
  174. FloatUp( pq, curr );
  175. }
  176. assert(free != LONG_MAX);
  177. return free;
  178. }
  179. /* really __gl_pqHeapExtractMin */
  180. PQkey pqExtractMin( PriorityQ *pq )
  181. {
  182. PQnode *n = pq->nodes;
  183. PQhandleElem *h = pq->handles;
  184. PQhandle hMin = n[1].handle;
  185. PQkey min = h[hMin].key;
  186. if( pq->size > 0 ) {
  187. n[1].handle = n[pq->size].handle;
  188. h[n[1].handle].node = 1;
  189. h[hMin].key = NULL;
  190. h[hMin].node = pq->freeList;
  191. pq->freeList = hMin;
  192. if( -- pq->size > 0 ) {
  193. FloatDown( pq, 1 );
  194. }
  195. }
  196. return min;
  197. }
  198. /* really __gl_pqHeapDelete */
  199. void pqDelete( PriorityQ *pq, PQhandle hCurr )
  200. {
  201. PQnode *n = pq->nodes;
  202. PQhandleElem *h = pq->handles;
  203. long curr;
  204. assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
  205. curr = h[hCurr].node;
  206. n[curr].handle = n[pq->size].handle;
  207. h[n[curr].handle].node = curr;
  208. if( curr <= -- pq->size ) {
  209. if( curr <= 1 || LEQ( h[n[curr>>1].handle].key, h[n[curr].handle].key )) {
  210. FloatDown( pq, curr );
  211. } else {
  212. FloatUp( pq, curr );
  213. }
  214. }
  215. h[hCurr].key = NULL;
  216. h[hCurr].node = pq->freeList;
  217. pq->freeList = hCurr;
  218. }