priorityq-heap.c 6.4 KB

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