/contrib/bind9/lib/isc/heap.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 265 lines · 179 code · 45 blank · 41 comment · 43 complexity · 44023fe387f45b5eaa744071664263a9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2007, 2010-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1997-2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file
  19. * Heap implementation of priority queues adapted from the following:
  20. *
  21. * \li "Introduction to Algorithms," Cormen, Leiserson, and Rivest,
  22. * MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
  23. *
  24. * \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
  25. * ISBN 0-201-06673-4, chapter 11.
  26. */
  27. #include <config.h>
  28. #include <isc/heap.h>
  29. #include <isc/magic.h>
  30. #include <isc/mem.h>
  31. #include <isc/string.h> /* Required for memcpy. */
  32. #include <isc/util.h>
  33. /*@{*/
  34. /*%
  35. * Note: to make heap_parent and heap_left easy to compute, the first
  36. * element of the heap array is not used; i.e. heap subscripts are 1-based,
  37. * not 0-based. The parent is index/2, and the left-child is index*2.
  38. * The right child is index*2+1.
  39. */
  40. #define heap_parent(i) ((i) >> 1)
  41. #define heap_left(i) ((i) << 1)
  42. /*@}*/
  43. #define SIZE_INCREMENT 1024
  44. #define HEAP_MAGIC ISC_MAGIC('H', 'E', 'A', 'P')
  45. #define VALID_HEAP(h) ISC_MAGIC_VALID(h, HEAP_MAGIC)
  46. /*%
  47. * When the heap is in a consistent state, the following invariant
  48. * holds true: for every element i > 1, heap_parent(i) has a priority
  49. * higher than or equal to that of i.
  50. */
  51. #define HEAPCONDITION(i) ((i) == 1 || \
  52. ! heap->compare(heap->array[(i)], \
  53. heap->array[heap_parent(i)]))
  54. /*% ISC heap structure. */
  55. struct isc_heap {
  56. unsigned int magic;
  57. isc_mem_t * mctx;
  58. unsigned int size;
  59. unsigned int size_increment;
  60. unsigned int last;
  61. void **array;
  62. isc_heapcompare_t compare;
  63. isc_heapindex_t index;
  64. };
  65. isc_result_t
  66. isc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare,
  67. isc_heapindex_t index, unsigned int size_increment,
  68. isc_heap_t **heapp)
  69. {
  70. isc_heap_t *heap;
  71. REQUIRE(heapp != NULL && *heapp == NULL);
  72. REQUIRE(compare != NULL);
  73. heap = isc_mem_get(mctx, sizeof(*heap));
  74. if (heap == NULL)
  75. return (ISC_R_NOMEMORY);
  76. heap->magic = HEAP_MAGIC;
  77. heap->size = 0;
  78. heap->mctx = NULL;
  79. isc_mem_attach(mctx, &heap->mctx);
  80. if (size_increment == 0)
  81. heap->size_increment = SIZE_INCREMENT;
  82. else
  83. heap->size_increment = size_increment;
  84. heap->last = 0;
  85. heap->array = NULL;
  86. heap->compare = compare;
  87. heap->index = index;
  88. *heapp = heap;
  89. return (ISC_R_SUCCESS);
  90. }
  91. void
  92. isc_heap_destroy(isc_heap_t **heapp) {
  93. isc_heap_t *heap;
  94. REQUIRE(heapp != NULL);
  95. heap = *heapp;
  96. REQUIRE(VALID_HEAP(heap));
  97. if (heap->array != NULL)
  98. isc_mem_put(heap->mctx, heap->array,
  99. heap->size * sizeof(void *));
  100. heap->magic = 0;
  101. isc_mem_putanddetach(&heap->mctx, heap, sizeof(*heap));
  102. *heapp = NULL;
  103. }
  104. static isc_boolean_t
  105. resize(isc_heap_t *heap) {
  106. void **new_array;
  107. size_t new_size;
  108. REQUIRE(VALID_HEAP(heap));
  109. new_size = heap->size + heap->size_increment;
  110. new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *));
  111. if (new_array == NULL)
  112. return (ISC_FALSE);
  113. if (heap->array != NULL) {
  114. memcpy(new_array, heap->array, heap->size * sizeof(void *));
  115. isc_mem_put(heap->mctx, heap->array,
  116. heap->size * sizeof(void *));
  117. }
  118. heap->size = new_size;
  119. heap->array = new_array;
  120. return (ISC_TRUE);
  121. }
  122. static void
  123. float_up(isc_heap_t *heap, unsigned int i, void *elt) {
  124. unsigned int p;
  125. for (p = heap_parent(i) ;
  126. i > 1 && heap->compare(elt, heap->array[p]) ;
  127. i = p, p = heap_parent(i)) {
  128. heap->array[i] = heap->array[p];
  129. if (heap->index != NULL)
  130. (heap->index)(heap->array[i], i);
  131. }
  132. heap->array[i] = elt;
  133. if (heap->index != NULL)
  134. (heap->index)(heap->array[i], i);
  135. INSIST(HEAPCONDITION(i));
  136. }
  137. static void
  138. sink_down(isc_heap_t *heap, unsigned int i, void *elt) {
  139. unsigned int j, size, half_size;
  140. size = heap->last;
  141. half_size = size / 2;
  142. while (i <= half_size) {
  143. /* Find the smallest of the (at most) two children. */
  144. j = heap_left(i);
  145. if (j < size && heap->compare(heap->array[j+1],
  146. heap->array[j]))
  147. j++;
  148. if (heap->compare(elt, heap->array[j]))
  149. break;
  150. heap->array[i] = heap->array[j];
  151. if (heap->index != NULL)
  152. (heap->index)(heap->array[i], i);
  153. i = j;
  154. }
  155. heap->array[i] = elt;
  156. if (heap->index != NULL)
  157. (heap->index)(heap->array[i], i);
  158. INSIST(HEAPCONDITION(i));
  159. }
  160. isc_result_t
  161. isc_heap_insert(isc_heap_t *heap, void *elt) {
  162. unsigned int new_last;
  163. REQUIRE(VALID_HEAP(heap));
  164. new_last = heap->last + 1;
  165. RUNTIME_CHECK(new_last > 0); /* overflow check */
  166. if (new_last >= heap->size && !resize(heap))
  167. return (ISC_R_NOMEMORY);
  168. heap->last = new_last;
  169. float_up(heap, new_last, elt);
  170. return (ISC_R_SUCCESS);
  171. }
  172. void
  173. isc_heap_delete(isc_heap_t *heap, unsigned int index) {
  174. void *elt;
  175. isc_boolean_t less;
  176. REQUIRE(VALID_HEAP(heap));
  177. REQUIRE(index >= 1 && index <= heap->last);
  178. if (index == heap->last) {
  179. heap->array[heap->last] = NULL;
  180. heap->last--;
  181. } else {
  182. elt = heap->array[heap->last];
  183. heap->array[heap->last] = NULL;
  184. heap->last--;
  185. less = heap->compare(elt, heap->array[index]);
  186. heap->array[index] = elt;
  187. if (less)
  188. float_up(heap, index, heap->array[index]);
  189. else
  190. sink_down(heap, index, heap->array[index]);
  191. }
  192. }
  193. void
  194. isc_heap_increased(isc_heap_t *heap, unsigned int index) {
  195. REQUIRE(VALID_HEAP(heap));
  196. REQUIRE(index >= 1 && index <= heap->last);
  197. float_up(heap, index, heap->array[index]);
  198. }
  199. void
  200. isc_heap_decreased(isc_heap_t *heap, unsigned int index) {
  201. REQUIRE(VALID_HEAP(heap));
  202. REQUIRE(index >= 1 && index <= heap->last);
  203. sink_down(heap, index, heap->array[index]);
  204. }
  205. void *
  206. isc_heap_element(isc_heap_t *heap, unsigned int index) {
  207. REQUIRE(VALID_HEAP(heap));
  208. REQUIRE(index >= 1);
  209. if (index <= heap->last)
  210. return (heap->array[index]);
  211. return (NULL);
  212. }
  213. void
  214. isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap) {
  215. unsigned int i;
  216. REQUIRE(VALID_HEAP(heap));
  217. REQUIRE(action != NULL);
  218. for (i = 1 ; i <= heap->last ; i++)
  219. (action)(heap->array[i], uap);
  220. }