priorityq-heap.c 6.3 KB

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