2 * UCW Library -- Universal Heap Macros
4 * (c) 2001 Martin Mares <mj@ucw.cz>
5 * (c) 2005 Tomas Valla <tom@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
16 * Binary heap is a simple data structure, which for example supports efficient insertions, deletions
17 * and access to the minimal inserted item. We define several macros for such operations.
18 * Note that because of simplicity of heaps, we have decided to define direct macros instead
19 * of a <<generic:,macro generator>> as for several other data structures in the Libucw.
21 * A heap is represented by a number of elements and by an array of values. Beware that we
22 * index this array from one, not from zero as do the standard C arrays.
24 * Most macros use these parameters:
26 * - @type - the type of elements
27 * - @num - a variable (signed or unsigned integer) with the number of elements
28 * - @heap - a C array of type @type; the heap is stored in `heap[1] .. heap[num]`; `heap[0]` is unused
29 * - @less - a callback to compare two element values; `less(x, y)` shall return a non-zero value iff @x is lower than @y
30 * - @swap - a callback to swap two array elements; `swap(heap, i, j, t)` must swap `heap[i]` with `heap[j]` with possible help of temporary variable @t (type @type).
32 * A valid heap must follow these rules:
35 * - `heap[i] >= heap[i / 2]` for each `i` in `[2, num]`
37 * The first element `heap[1]` is always lower or equal to all other elements.
44 /* For internal usage. */
45 #define HEAP_BUBBLE_DOWN_J(heap,num,less,swap) \
51 if (less(heap[_j],heap[_l]) && (_l == num || less(heap[_j],heap[_l+1]))) \
53 if (_l != num && less(heap[_l+1],heap[_l])) \
59 /* For internal usage. */
60 #define HEAP_BUBBLE_UP_J(heap,num,less,swap) \
64 if (less(heap[_u], heap[_j])) \
71 * Shuffle the unordered array @heap of @num elements to become a valid heap. The time complexity is linear.
73 #define HEAP_INIT(type,heap,num,less,swap) \
81 HEAP_BUBBLE_DOWN_J(heap,num,less,swap) \
87 * Delete the minimum element `heap[1]` in `O(log(n))` time.
88 * The removed value is moved just after the resulting heap (`heap[num + 1]`).
90 #define HEAP_DELMIN(type,heap,num,less,swap) \
97 HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \
101 * Insert `heap[num]` in `O(log(n))` time. The value of @num must be increased before.
103 #define HEAP_INSERT(type,heap,num,less,swap) \
108 HEAP_BUBBLE_UP_J(heap,num,less,swap); \
112 * If you need to increase the value of `heap[pos]`, just do it and then call this macro to rebuild the heap.
113 * Only `heap[pos]` can be changed, the rest of the array must form a valid heap.
114 * The time complexity is `O(log(n))`.
116 #define HEAP_INCREASE(type,heap,num,less,swap,pos) \
121 HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \
125 * If you need to decrease the value of `heap[pos]`, just do it and then call this macro to rebuild the heap.
126 * Only `heap[pos]` can be changed, the rest of the array must form a valid heap.
127 * The time complexity is `O(log(n))`.
129 #define HEAP_DECREASE(type,heap,num,less,swap,pos) \
134 HEAP_BUBBLE_UP_J(heap,num,less,swap); \
138 * Delete `heap[pos]` in `O(log(n))` time.
140 #define HEAP_DELETE(type,heap,num,less,swap,pos) \
145 swap(heap,_j,num,x); \
147 if (less(heap[_j], heap[num+1])) \
148 HEAP_BUBBLE_UP_J(heap,num,less,swap) \
150 HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \
154 * Default swapping macro.
156 #define HEAP_SWAP(heap,a,b,t) (t=heap[a], heap[a]=heap[b], heap[b]=t)