ntrHeap.c 9.0 KB

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