heap.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*!
  2. Temelia - Heap interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.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. #ifndef HEAP_H_
  18. #define HEAP_H_
  19. #include "platform.h"
  20. struct _heap_t;
  21. typedef struct _heap_t *heap_t;
  22. /*!
  23. * @brief Returns an empty heap.
  24. * Complexity O(1)
  25. *
  26. * @param Heap's size
  27. */
  28. DECLSPEC heap_t heap_new(int size);
  29. /*!
  30. * @brief Frees the memory occupied by a heap.
  31. * Complexity O(1)
  32. *
  33. * @param Heap
  34. */
  35. DECLSPEC void heap_delete(heap_t heap);
  36. /*!
  37. * @brief Tests if a heap is empty.
  38. * Complexity O(1)
  39. *
  40. * @param Heap
  41. */
  42. DECLSPEC int heap_is_empty(heap_t heap);
  43. /*!
  44. * @brief Tests if a heap is full.
  45. * Complexity O(1)
  46. *
  47. * @param Heap
  48. */
  49. DECLSPEC int heap_is_full(heap_t heap);
  50. /*!
  51. * @brief Return the minimum key, stored in the heap's root.
  52. * Complexity O(1)
  53. *
  54. * @param Heap
  55. */
  56. DECLSPEC void *heap_get_min_key(heap_t heap);
  57. /*!
  58. * @brief Removes minimum key.
  59. * Complexity O(log(n))
  60. *
  61. * @param Heap
  62. * @param Pointer to comparison function
  63. * @param Context
  64. */
  65. DECLSPEC void heap_remove_min_key(heap_t heap, int compare(void *x, void *y,
  66. void *context), void *context);
  67. /*!
  68. * @brief Inserts a new key in heap.
  69. * Complexity O(log(n))
  70. *
  71. * @param Heap
  72. * @param Key
  73. * @param Pointer to comparison function
  74. * @param Context
  75. */
  76. DECLSPEC void heap_insert(heap_t heap, void *key, int compare(void *x, void *y,
  77. void *context), void *context);
  78. /*!
  79. * @brief Changes the key from position in heap.
  80. * Complexity O(1)
  81. *
  82. * @param Heap
  83. * @param Key's position
  84. * @param New key reference
  85. * @param Pointer to comparison function
  86. */
  87. DECLSPEC void heap_change_key_by_position(heap_t heap, int position,
  88. void *new_key_value, int compare(void *x, void *y, void *context),
  89. void *context);
  90. /*!
  91. * @brief Changes a key value from heap.
  92. * Complexity O(n)
  93. *
  94. * @param Heap
  95. * @param Key
  96. * @param New key
  97. * @param Pointer to comparison function
  98. */
  99. DECLSPEC void heap_change_key_by_value(heap_t heap, void *key_value,
  100. void *new_key_value, int compare(void *x, void *y, void *context),
  101. void *context);
  102. /*!
  103. * @brief Breadth traverses a heap and handles the key from each node.
  104. * Complexity O(1)
  105. *
  106. * @param Heap
  107. * @param Pointer to print function.
  108. * @param Pointer to stream.
  109. */
  110. DECLSPEC void heap_iterate(heap_t heap, void key_handler(void *x, void *context),
  111. void *context);
  112. /*!
  113. * @brief Returns the number of keys from heap.
  114. * Complexity O(1)
  115. *
  116. * @param Heap
  117. */
  118. DECLSPEC int heap_get_size(heap_t heap);
  119. /*!
  120. * @brief Returns heap's size.
  121. * Complexity O(1)
  122. *
  123. * @param Heap
  124. */
  125. DECLSPEC int heap_get_capacity(heap_t heap);
  126. /*!
  127. * @brief Sets heap's capacity increment. If you don't want size auto-adjusting when
  128. * the heap is full, then set the capacity increment to 0.
  129. * Complexity O(1)
  130. *
  131. * @param Heap
  132. * @param New capacity increment value
  133. */
  134. DECLSPEC void heap_set_capacity_increment(heap_t heap, int new_capacity_increment);
  135. /*!
  136. * @brief Returns heap's capacity increment.
  137. * Complexity O(1)
  138. *
  139. * @param Heap
  140. */
  141. DECLSPEC int heap_get_capacity_increment(heap_t heap);
  142. /*!
  143. * @brief Returns the internal representation of the current heap;
  144. * cast it to vector_t.
  145. * Complexity O(1)
  146. *
  147. * @param Heap
  148. */
  149. DECLSPEC void *heap_get_data(heap_t heap);
  150. /*!
  151. * @brief Creates a heap from an array.
  152. * Complexity O(n * log(n))
  153. *
  154. * @param Array data
  155. * @param Size
  156. * @param Pointer to comparison function
  157. */
  158. DECLSPEC heap_t heap_create_heap(void **data, int size, int compare(void *x, void *y,
  159. void *context), void *context);
  160. #endif /* HEAP_H_ */