heap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*!
  2. Temelia - Heap implementation source file.
  3. Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include "include/heap.h"
  18. #include "include/vector.h"
  19. #include "include/common.h"
  20. #include <stdlib.h>
  21. struct _heap_t
  22. {
  23. /*! array of keys */
  24. vector_t data;
  25. };
  26. //Private functions
  27. // Bubbles down key, in heap, from position p.
  28. static void _heap_down(heap_t heap, int position, int(*compare)(void *x, void *y,
  29. void *context), void *context);
  30. // Bubbles up key, in heap, from position p.
  31. static void _heap_up(heap_t heap, int position, int(*compare)(void *x, void *y,
  32. void *context), void *context);
  33. // Returns the index of key in heap.
  34. static int _heap_find(heap_t heap, void *key, int(*compare)(void *x, void *y,
  35. void *context), void *context);
  36. heap_t heap_new(int capacity)
  37. {
  38. heap_t heap;
  39. _ASSERT(capacity, <=, 0, INVALID_INPUT, NULL);
  40. heap = (struct _heap_t *) _new(sizeof(struct _heap_t));
  41. heap->data = vector_new(capacity);
  42. return heap;
  43. }
  44. void heap_delete(heap_t heap)
  45. {
  46. _ASSERT(heap, ==, NULL, NULL_POINTER,);
  47. vector_delete(heap->data);
  48. _delete(heap);
  49. }
  50. int heap_is_empty(heap_t heap)
  51. {
  52. _ASSERT(heap, ==, NULL, NULL_POINTER, -1);
  53. return vector_get_size(heap->data) == 0;
  54. }
  55. int heap_is_full(heap_t heap)
  56. {
  57. _ASSERT(heap, ==, NULL, NULL_POINTER, -1);
  58. return vector_get_capacity(heap->data) == vector_get_size(heap->data);
  59. }
  60. void *heap_get_min_key(heap_t heap)
  61. {
  62. _ASSERT(heap, ==, NULL, NULL_POINTER, NULL);
  63. _ASSERT(vector_get_size(heap->data), <=, 0, INVALID_INPUT, NULL);
  64. return vector_get_key_at(heap->data, 0);
  65. }
  66. void heap_remove_min_key(heap_t heap,
  67. int compare(void *x, void *y, void *context), void *context)
  68. {
  69. void *x;
  70. _ASSERT(heap, ==, NULL, NULL_POINTER,);
  71. _ASSERT(vector_get_size(heap->data), <=, 0, EMPTY,);
  72. _ASSERT(compare, ==, NULL, NULL_POINTER,);
  73. x = vector_get_key_at(heap->data, 0);
  74. vector_set_key_at(heap->data, 0, vector_get_key_at(heap->data,
  75. vector_get_size(heap->data) - 1));
  76. vector_set_size(heap->data, vector_get_size(heap->data) - 1);
  77. _heap_down(heap, 0, compare, context);
  78. }
  79. void heap_insert(heap_t heap, void *key, int compare(void *x, void *y,
  80. void *context), void *context)
  81. {
  82. _ASSERT(heap, ==, NULL, NULL_POINTER,);
  83. vector_push_back(heap->data, key);
  84. _heap_up(heap, vector_get_size(heap->data) - 1, compare, context);
  85. }
  86. void heap_change_key_by_position(heap_t heap, int position, void *new_key_value,
  87. int compare(void *x, void *y, void *context), void *context)
  88. {
  89. void *tmp;
  90. _ASSERT(heap, ==, NULL, NULL_POINTER,);
  91. _ASSERT(position, <, 0, INVALID_INPUT,);
  92. _ASSERT(compare, ==, NULL, NULL_POINTER,);
  93. _ASSERT(vector_get_size(heap->data), <=, position, INVALID_INPUT,);
  94. tmp = vector_get_key_at(heap->data, position);
  95. vector_set_key_at(heap->data, position, new_key_value);
  96. if (compare(new_key_value, tmp, context) < 0)
  97. _heap_down(heap, position, compare, context);
  98. else
  99. _heap_up(heap, position, compare, context);
  100. }
  101. void heap_change_key_by_value(heap_t heap, void *key_value, void *new_key_value,
  102. int compare(void *x, void *y, void *context), void *context)
  103. {
  104. heap_change_key_by_position(heap, _heap_find(heap, key_value,
  105. compare, context), new_key_value, compare, context);
  106. }
  107. void heap_iterate(heap_t heap, void key_handler(void *x, void *context),
  108. void *context)
  109. {
  110. int i;
  111. _ASSERT(heap, ==, NULL, NULL_POINTER,);
  112. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  113. for (i = 0; i < vector_get_size(heap->data); i++)
  114. key_handler(vector_get_key_at(heap->data, i), context);
  115. }
  116. int heap_get_size(heap_t heap)
  117. {
  118. _ASSERT(heap, ==, NULL, NULL_POINTER, -1);
  119. return vector_get_size(heap->data);
  120. }
  121. int heap_get_capacity(heap_t heap)
  122. {
  123. _ASSERT(heap, ==, NULL, NULL_POINTER, -1);
  124. return vector_get_capacity(heap->data);
  125. }
  126. void heap_set_capacity_increment(heap_t heap, int capacity_increment)
  127. {
  128. _ASSERT(heap, ==, NULL, NULL_POINTER,);
  129. _ASSERT(capacity_increment, <, 0, INVALID_INPUT,);
  130. vector_set_capacity_increment(heap->data, capacity_increment);
  131. }
  132. int heap_get_capacity_increment(heap_t heap)
  133. {
  134. _ASSERT(heap, ==, NULL, NULL_POINTER, -1);
  135. return vector_get_capacity_increment(heap->data);
  136. }
  137. void *heap_get_data(heap_t heap)
  138. {
  139. _ASSERT(heap, ==, NULL, NULL_POINTER, NULL);
  140. return heap->data;
  141. }
  142. heap_t heap_create_heap(void **data, int size, int compare(void *x, void *y,
  143. void *context), void *context)
  144. {
  145. int i;
  146. heap_t heap;
  147. _ASSERT(data, ==, NULL, NULL_POINTER, NULL);
  148. _ASSERT(size, <=, 0, INVALID_INPUT, NULL);
  149. _ASSERT(compare, ==, NULL, NULL_POINTER, NULL);
  150. heap = heap_new(size);
  151. _ASSERT(heap, ==, NULL, NULL_POINTER, NULL);
  152. for (i = 0; i < size; i++)
  153. heap_insert(heap, data[i], compare, context);
  154. return heap;
  155. }
  156. static void _heap_down(heap_t heap, int position, int (*compare)(void *x, void *y,
  157. void *context), void *context)
  158. {
  159. int child;
  160. void *aux;
  161. while (2 * position + 1 < vector_get_size(heap->data))
  162. {
  163. child = 2 * position + 1;
  164. if (child + 1 < vector_get_size(heap->data) && compare(
  165. vector_get_key_at(heap->data, child), vector_get_key_at(
  166. heap->data, child + 1), context) > 0)
  167. child++;
  168. if (compare(vector_get_key_at(heap->data, child), vector_get_key_at(
  169. heap->data, position), context) < 0)
  170. {
  171. aux = vector_get_key_at(heap->data, position);
  172. vector_set_key_at(heap->data, position, vector_get_key_at(heap->data,
  173. child));
  174. vector_set_key_at(heap->data, child, aux);
  175. position = child;
  176. }
  177. else
  178. break;
  179. }
  180. }
  181. static void _heap_up(heap_t heap, int position, int (*compare)(void *x, void *y,
  182. void *context), void *context)
  183. {
  184. void *aux;
  185. if (position == 0)
  186. return;
  187. while (position > 0 && (compare(vector_get_key_at(heap->data, position),
  188. vector_get_key_at(heap->data, (position - 1) / 2), context) < 0))
  189. {
  190. aux = vector_get_key_at(heap->data, position);
  191. vector_set_key_at(heap->data, position, vector_get_key_at(heap->data,
  192. (position - 1) / 2));
  193. vector_set_key_at(heap->data, (position - 1) / 2, aux);
  194. position = (position - 1) / 2;
  195. }
  196. }
  197. static int _heap_find(heap_t heap, void *key, int (*compare)(void *x, void *y,
  198. void *context), void *context)
  199. {
  200. int i;
  201. for (i = 0; i < vector_get_size(heap->data); i++)
  202. if (!compare(vector_get_key_at(heap->data, i), key, context))
  203. return i;
  204. return -1;
  205. }