You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
11 KiB

  1. /**CFile***********************************************************************
  2. FileName [ntrHeap.c]
  3. PackageName [ntr]
  4. Synopsis [Functions for heap-based priority queue.]
  5. Description [The functions in this file manage a priority queue implemented
  6. as a heap. The first element of the heap is the one with the smallest key.
  7. Refer to Chapter 7 of Cormen, Leiserson, and Rivest for the theory.]
  8. SeeAlso []
  9. Author [Fabio Somenzi]
  10. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions
  14. are met:
  15. Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. Neither the name of the University of Colorado nor the names of its
  21. contributors may be used to endorse or promote products derived from
  22. this software without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. POSSIBILITY OF SUCH DAMAGE.]
  35. ******************************************************************************/
  36. #include "ntr.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. /*---------------------------------------------------------------------------*/
  41. /* Stucture declarations */
  42. /*---------------------------------------------------------------------------*/
  43. /*---------------------------------------------------------------------------*/
  44. /* Type declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Variable declarations */
  48. /*---------------------------------------------------------------------------*/
  49. #ifndef lint
  50. static char rcsid[] UTIL_UNUSED = "$Id: ntrHeap.c,v 1.6 2012/02/05 01:53:01 fabio Exp fabio $";
  51. #endif
  52. /*---------------------------------------------------------------------------*/
  53. /* Macro declarations */
  54. /*---------------------------------------------------------------------------*/
  55. #define PARENT(i) (((i)-1)>>1)
  56. #define RIGHT(i) (((i)+1)<<1)
  57. #define LEFT(i) (((i)<<1)|1)
  58. #define ITEM(p,i) ((p)[i].item)
  59. #define KEY(p,i) ((p)[i].key)
  60. /**AutomaticStart*************************************************************/
  61. /*---------------------------------------------------------------------------*/
  62. /* Static function prototypes */
  63. /*---------------------------------------------------------------------------*/
  64. static void ntrHeapify (NtrHeap *heap, int i);
  65. static int ntrHeapResize (NtrHeap *heap);
  66. /**AutomaticEnd***************************************************************/
  67. /*---------------------------------------------------------------------------*/
  68. /* Definition of exported functions */
  69. /*---------------------------------------------------------------------------*/
  70. /**Function********************************************************************
  71. Synopsis [Initializes a priority queue.]
  72. Description [Initializes a priority queue. Returns a pointer to the heap
  73. if successful; NULL otherwise.]
  74. SideEffects [None]
  75. SeeAlso [Ntr_FreeHeap]
  76. ******************************************************************************/
  77. NtrHeap *
  78. Ntr_InitHeap(
  79. int size)
  80. {
  81. NtrHeap *heap;
  82. heap = ALLOC(NtrHeap,1);
  83. if (heap == NULL) return(NULL);
  84. heap->size = size;
  85. heap->nslots = 0;
  86. heap->slots = ALLOC(NtrHeapSlot,size);
  87. if (heap->slots == NULL) {
  88. FREE(heap);
  89. return(NULL);
  90. }
  91. return(heap);
  92. } /* end of Ntr_InitHeap */
  93. /**Function********************************************************************
  94. Synopsis [Frees a priority queue.]
  95. Description []
  96. SideEffects [None]
  97. SeeAlso [Ntr_InitHeap]
  98. ******************************************************************************/
  99. void
  100. Ntr_FreeHeap(
  101. NtrHeap *heap)
  102. {
  103. FREE(heap->slots);
  104. FREE(heap);
  105. return;
  106. } /* end of Ntr_FreeHeap */
  107. /**Function********************************************************************
  108. Synopsis [Inserts an item in a priority queue.]
  109. Description [Inserts an item in a priority queue. Returns 1 if successful;
  110. 0 otherwise.]
  111. SideEffects [None]
  112. SeeAlso [Ntr_HeapExtractMin]
  113. ******************************************************************************/
  114. int
  115. Ntr_HeapInsert(
  116. NtrHeap *heap,
  117. void *item,
  118. int key)
  119. {
  120. NtrHeapSlot *slots;
  121. int i = heap->nslots;
  122. if (i == heap->size && !ntrHeapResize(heap)) return(0);
  123. slots = heap->slots;
  124. heap->nslots++;
  125. while (i > 0 && KEY(slots,PARENT(i)) > key) {
  126. ITEM(slots,i) = ITEM(slots,PARENT(i));
  127. KEY(slots,i) = KEY(slots,PARENT(i));
  128. i = PARENT(i);
  129. }
  130. ITEM(slots,i) = item;
  131. KEY(slots,i) = key;
  132. return(1);
  133. } /* end of Ntr_HeapInsert */
  134. /**Function********************************************************************
  135. Synopsis [Extracts the element with the minimum key from a priority
  136. queue.]
  137. Description [Extracts the element with the minimum key from a priority
  138. queue. Returns 1 if successful; 0 otherwise.]
  139. SideEffects [The minimum key and the associated item are returned as
  140. side effects.]
  141. SeeAlso [Ntr_HeapInsert]
  142. ******************************************************************************/
  143. int
  144. Ntr_HeapExtractMin(
  145. NtrHeap *heap,
  146. void *item,
  147. int *key)
  148. {
  149. NtrHeapSlot *slots = heap->slots;
  150. if (heap->nslots == 0) return(0);
  151. *(void **)item = ITEM(slots,0);
  152. *key = KEY(slots,0);
  153. heap->nslots--;
  154. ITEM(slots,0) = ITEM(slots,heap->nslots);
  155. KEY(slots,0) = KEY(slots,heap->nslots);
  156. ntrHeapify(heap,0);
  157. return(1);
  158. } /* end of Ntr_HeapExtractMin */
  159. /**Function********************************************************************
  160. Synopsis [Returns the number of items in a priority queue.]
  161. Description []
  162. SideEffects [None]
  163. SeeAlso []
  164. ******************************************************************************/
  165. int
  166. Ntr_HeapCount(
  167. NtrHeap *heap)
  168. {
  169. return(heap->nslots);
  170. } /* end of Ntr_HeapCount */
  171. /**Function********************************************************************
  172. Synopsis [Clones a priority queue.]
  173. Description []
  174. SideEffects [None]
  175. SeeAlso [Ntr_InitHeap]
  176. ******************************************************************************/
  177. NtrHeap *
  178. Ntr_HeapClone(
  179. NtrHeap *source)
  180. {
  181. NtrHeap *dest;
  182. int i;
  183. int nslots = source->nslots;
  184. NtrHeapSlot *sslots = source->slots;
  185. NtrHeapSlot *dslots;
  186. dest = Ntr_InitHeap(source->size);
  187. if (dest == NULL) return(NULL);
  188. dest->nslots = nslots;
  189. dslots = dest->slots;
  190. for (i = 0; i < nslots; i++) {
  191. KEY(dslots,i) = KEY(sslots,i);
  192. ITEM(dslots,i) = ITEM(sslots,i);
  193. }
  194. return(dest);
  195. } /* end of Ntr_HeapClone */
  196. /**Function********************************************************************
  197. Synopsis [Tests the heap property of a priority queue.]
  198. Description [Tests the heap property of a priority queue. Returns 1 if
  199. Successful; 0 otherwise.]
  200. SideEffects [None]
  201. SeeAlso []
  202. ******************************************************************************/
  203. int
  204. Ntr_TestHeap(
  205. NtrHeap *heap,
  206. int i)
  207. {
  208. NtrHeapSlot *slots = heap->slots;
  209. int nslots = heap->nslots;
  210. if (i > 0 && KEY(slots,i) < KEY(slots,PARENT(i)))
  211. return(0);
  212. if (LEFT(i) < nslots) {
  213. if (!Ntr_TestHeap(heap,LEFT(i)))
  214. return(0);
  215. }
  216. if (RIGHT(i) < nslots) {
  217. if (!Ntr_TestHeap(heap,RIGHT(i)))
  218. return(0);
  219. }
  220. return(1);
  221. } /* end of Ntr_TestHeap */
  222. /*---------------------------------------------------------------------------*/
  223. /* Definition of static functions */
  224. /*---------------------------------------------------------------------------*/
  225. /**Function********************************************************************
  226. Synopsis [Maintains the heap property of a priority queue.]
  227. Description []
  228. SideEffects [None]
  229. SeeAlso [Ntr_HeapExtractMin]
  230. ******************************************************************************/
  231. static void
  232. ntrHeapify(
  233. NtrHeap *heap,
  234. int i)
  235. {
  236. int smallest;
  237. int left = LEFT(i);
  238. int right = RIGHT(i);
  239. int nslots = heap->nslots;
  240. NtrHeapSlot *slots = heap->slots;
  241. int key = KEY(slots,i);
  242. if (left < nslots && KEY(slots,left) < key) {
  243. smallest = left;
  244. } else {
  245. smallest = i;
  246. }
  247. if (right < nslots && KEY(slots,right) < KEY(slots,smallest)) {
  248. smallest = right;
  249. }
  250. if (smallest != i) {
  251. void *item = ITEM(slots,i);
  252. KEY(slots,i) = KEY(slots,smallest);
  253. ITEM(slots,i) = ITEM(slots,smallest);
  254. KEY(slots,smallest) = key;
  255. ITEM(slots,smallest) = item;
  256. ntrHeapify(heap,smallest);
  257. }
  258. return;
  259. } /* end of ntrHeapify */
  260. /**Function********************************************************************
  261. Synopsis [Resizes a priority queue.]
  262. Description [Resizes a priority queue by doubling the number of
  263. available slots. Returns 1 if successful; 0 otherwise.]
  264. SideEffects [None]
  265. SeeAlso [Ntr_HeapInsert]
  266. ******************************************************************************/
  267. static int
  268. ntrHeapResize(
  269. NtrHeap *heap)
  270. {
  271. int oldlength = heap->size;
  272. int newlength = 2 * oldlength;
  273. NtrHeapSlot *oldslots = heap->slots;
  274. NtrHeapSlot *newslots = REALLOC(NtrHeapSlot, oldslots, newlength);
  275. if (newslots == NULL) return 0;
  276. heap->size = newlength;
  277. heap->slots = newslots;
  278. assert(Ntr_TestHeap(heap, 0));
  279. return 1;
  280. } /* end of ntrHeapResize */