11 #include "cgds/types.h"
12 #include "cgds/Vector.h"
13 #include "cgds/safe_alloc.h"
16 * @brief Generic d-ary heap.
19 size_t dataSize
; ///< Size of a heap item in bytes.
20 OrderType hType
; ///< Type of heap: max first (MAX_T) or min first (MIN_T).
21 UInt arity
; ///< Arity of the underlying tree.
22 Vector
* array
; ///< Vector of ItemValue* (internal representation).
26 * @brief Return an allocated and initialized heap.
29 size_t dataSize
, ///< Size in bytes of a heap element.
30 OrderType hType
, ///< Type of heap: max first (MAX_T) or min first (MIN_T).
31 UInt arity
///< Arity of the underlying tree.
35 * @brief Return an allocated and initialized heap.
36 * @param type Type of a buffer item (int, char*, ...).
37 * @param hType Type of heap: max first (MAX_T) or min first (MIN_T).
38 * @param arity Arity of the underlying tree.
40 * Usage: Heap* heap_new(<Type> type, OrderType hType, UInt arity)
42 #define heap_new(type, hType, arity) \
44 _heap_new(sizeof(type), hType, arity); \
48 * @brief Copy constructor (shallow copy, ok for basic types).
51 Heap
* heap
///< "this" pointer.
55 * @brief Check if the heap is empty.
58 Heap
* heap
///< "this" pointer.
62 * @brief Return the size of current heap.
65 Heap
* heap
///< "this" pointer.
69 * @brief "Bubble up" an item-value inserted somewhere in the tree
70 * in O(log(n)) operations.
73 Heap
* heap
, ///< "this" pointer.
74 UInt startIndex
///< Index to bubble up.
78 * @brief "Bubble down" an item-value inserted somewhere in the tree
79 * in O(log(n)) operations.
81 void _heap_bubble_down(
82 Heap
* heap
, ///< "this" pointer.
83 UInt startIndex
///< Index to bubble down.
87 * @brief Insert a pair (item,value) inside the heap.
90 Heap
* heap
, ///< "this" pointer.
91 void* item
, ///< Pointer to an item of type as defined in the constructor.
92 Real value
///< Value associated with the item.
96 * @brief Insert a pair (item,value) inside the heap.
97 * @param heap "this" pointer.
98 * @param item Item of type as defined in the constructor.
99 * @param value Value associated with the item.
101 * Usage: void heap_insert(Heap* heap, void item, Real value)
103 #define heap_insert(heap, item, value) \
105 typeof((item)) tmp = item; \
106 _heap_insert(heap, &tmp, value); \
110 * @brief Change the value of an item at a given index.
113 Heap
* heap
, ///< "this" pointer.
114 UInt index
, ///< Index of the item to modify.
115 Real newValue
///< New value for the item.
119 * @brief Change the value of an item inside the heap.
120 * @param heap "this" pointer.
121 * @param item_ Item to modify.
122 * @param newValue New value for the item.
123 * @note If several similar items are present, only the first is affected.
125 * Usage: void heap_modify(Heap* heap, void item_, Real newValue)
127 * WARNING: does not work if items are not basic type nor pointers.
129 #define heap_modify(heap, item_, newValue) \
132 typeof((item_)) item__ = item_; \
133 for (; index<heap->array->size; index++) \
135 void* pItem = ((ItemValue*)(heap->array->datas[index]))->item; \
136 if (*((typeof(&item__))pItem) == item__) break; \
138 _heap_modify(heap, index, newValue); \
142 * @brief Remove an item-value at a given index.
145 Heap
* heap
, ///< "this" pointer.
146 UInt index
///< Index of the item to remove.
150 * @brief Remove an item-value inside the heap.
151 * @param heap "this" pointer.
152 * @param item_ Item to remove.
153 * @note If several similar items are present, only the first is deleted.
155 * Usage: void heap_remove(Heap* heap, void item_)
157 * WARNING: does not work if items are not basic type nor pointers.
159 #define heap_remove(heap, item_) \
162 typeof((item_)) item__ = item_; \
163 for (; index<heap->array->size; index++) \
165 void* pItem = ((ItemValue*)(heap->array->datas[index]))->item; \
166 if (*((typeof(&item__))pItem) == item__) break; \
168 _heap_remove(heap, index); \
172 * @brief Return what is at the beginning of the heap.
174 ItemValue
* heap_top_raw(
175 Heap
* heap
///< "this" pointer.
179 * @brief Return what is at the beginning of the heap.
180 * @param heap "this" pointer.
181 * @param item_ Item to be assigned.
183 * Usage: void heap_top(Heap* heap, void item_)
185 #define heap_top(heap, item_) \
187 void* pItem = heap_top_raw(heap)->item; \
188 item_ = *((typeof(&item_))pItem); \
192 * @brief Remove the top of the heap.
195 Heap
* heap
///< "this" pointer.
199 * @brief Clear the entire heap.
202 Heap
* heap
///< "this" pointer.
206 * @brief Destroy the heap: clear it, and free 'heap' pointer.
209 Heap
* heap
///< "this" pointer.