1 /*-------------------------------------------------------------------------
4 * A simple binary heap implementaion
6 * Portions Copyright (c) 2012-2013, PostgreSQL Global Development Group
9 * src/backend/lib/binaryheap.c
11 *-------------------------------------------------------------------------
18 #include "lib/binaryheap.h"
20 static void sift_down(binaryheap *heap, int node_off);
21 static void sift_up(binaryheap *heap, int node_off);
22 static inline void swap_nodes(binaryheap *heap, int a, int b);
27 * Returns a pointer to a newly-allocated heap that has the capacity to
28 * store the given number of nodes, with the heap property defined by
29 * the given comparator function, which will be invoked with the additional
30 * argument specified by 'arg'.
33 binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
38 sz = offsetof(binaryheap, bh_nodes) +sizeof(Datum) * capacity;
39 heap = (binaryheap *) palloc(sz);
40 heap->bh_space = capacity;
41 heap->bh_compare = compare;
45 heap->bh_has_heap_property = true;
53 * Resets the heap to an empty state, losing its data content but not the
54 * parameters passed at allocation.
57 binaryheap_reset(binaryheap *heap)
60 heap->bh_has_heap_property = true;
66 * Releases memory used by the given binaryheap.
69 binaryheap_free(binaryheap *heap)
75 * These utility functions return the offset of the left child, right
76 * child, and parent of the node at the given index, respectively.
78 * The heap is represented as an array of nodes, with the root node
79 * stored at index 0. The left child of node i is at index 2*i+1, and
80 * the right child at 2*i+2. The parent of node i is at index (i-1)/2.
102 * binaryheap_add_unordered
104 * Adds the given datum to the end of the heap's list of nodes in O(1) without
105 * preserving the heap property. This is a convenience to add elements quickly
106 * to a new heap. To obtain a valid heap, one must call binaryheap_build()
110 binaryheap_add_unordered(binaryheap *heap, Datum d)
112 if (heap->bh_size >= heap->bh_space)
113 elog(ERROR, "out of binary heap slots");
114 heap->bh_has_heap_property = false;
115 heap->bh_nodes[heap->bh_size] = d;
122 * Assembles a valid heap in O(n) from the nodes added by
123 * binaryheap_add_unordered(). Not needed otherwise.
126 binaryheap_build(binaryheap *heap)
130 for (i = parent_offset(heap->bh_size - 1); i >= 0; i--)
132 heap->bh_has_heap_property = true;
138 * Adds the given datum to the heap in O(log n) time, while preserving
142 binaryheap_add(binaryheap *heap, Datum d)
144 if (heap->bh_size >= heap->bh_space)
145 elog(ERROR, "out of binary heap slots");
146 heap->bh_nodes[heap->bh_size] = d;
148 sift_up(heap, heap->bh_size - 1);
154 * Returns a pointer to the first (root, topmost) node in the heap
155 * without modifying the heap. The caller must ensure that this
156 * routine is not used on an empty heap. Always O(1).
159 binaryheap_first(binaryheap *heap)
161 Assert(!binaryheap_empty(heap) && heap->bh_has_heap_property);
162 return heap->bh_nodes[0];
166 * binaryheap_remove_first
168 * Removes the first (root, topmost) node in the heap and returns a
169 * pointer to it after rebalancing the heap. The caller must ensure
170 * that this routine is not used on an empty heap. O(log n) worst
174 binaryheap_remove_first(binaryheap *heap)
176 Assert(!binaryheap_empty(heap) && heap->bh_has_heap_property);
178 if (heap->bh_size == 1)
181 return heap->bh_nodes[0];
185 * Swap the root and last nodes, decrease the size of the heap (i.e.
186 * remove the former root node) and sift the new root node down to its
189 swap_nodes(heap, 0, heap->bh_size - 1);
193 return heap->bh_nodes[heap->bh_size];
197 * binaryheap_replace_first
199 * Replace the topmost element of a non-empty heap, preserving the heap
200 * property. O(1) in the best case, or O(log n) if it must fall back to
201 * sifting the new node down.
204 binaryheap_replace_first(binaryheap *heap, Datum d)
206 Assert(!binaryheap_empty(heap) && heap->bh_has_heap_property);
208 heap->bh_nodes[0] = d;
210 if (heap->bh_size > 1)
215 * Swap the contents of two nodes.
218 swap_nodes(binaryheap *heap, int a, int b)
222 swap = heap->bh_nodes[a];
223 heap->bh_nodes[a] = heap->bh_nodes[b];
224 heap->bh_nodes[b] = swap;
228 * Sift a node up to the highest position it can hold according to the
232 sift_up(binaryheap *heap, int node_off)
234 while (node_off != 0)
240 * If this node is smaller than its parent, the heap condition is
241 * satisfied, and we're done.
243 parent_off = parent_offset(node_off);
244 cmp = heap->bh_compare(heap->bh_nodes[node_off],
245 heap->bh_nodes[parent_off],
251 * Otherwise, swap the node and its parent and go on to check the
254 swap_nodes(heap, node_off, parent_off);
255 node_off = parent_off;
260 * Sift a node down from its current position to satisfy the heap
264 sift_down(binaryheap *heap, int node_off)
268 int left_off = left_offset(node_off);
269 int right_off = right_offset(node_off);
272 /* Is the left child larger than the parent? */
273 if (left_off < heap->bh_size &&
274 heap->bh_compare(heap->bh_nodes[node_off],
275 heap->bh_nodes[left_off],
279 /* Is the right child larger than the parent? */
280 if (right_off < heap->bh_size &&
281 heap->bh_compare(heap->bh_nodes[node_off],
282 heap->bh_nodes[right_off],
285 /* swap with the larger child */
287 heap->bh_compare(heap->bh_nodes[left_off],
288 heap->bh_nodes[right_off],
290 swap_off = right_off;
294 * If we didn't find anything to swap, the heap condition is
295 * satisfied, and we're done.
301 * Otherwise, swap the node with the child that violates the heap
302 * property; then go on to check its children.
304 swap_nodes(heap, swap_off, node_off);