1 // util/heap.h -- A heap-based priority queue implementation
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
29 #include <lowlevel/bitops.h>
32 // T must support operator < for key ordering, and
33 // have a Heap<T>::Node member called heap_node.
39 Node *parent, *left, *right;
41 // A pointer to the parent's left or right pointer
42 // (whichever side this node is on). This is also
43 // used for sanity checks (it is NULL for nodes not
50 parent = left = right = NULL;
56 return parentlink != NULL;
64 T *node_to_type(Node *n)
66 return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(n) -
67 offsetof(T, heap_node));
70 // Swap n with its parent
76 n->parent = p->parent;
79 Node **plink = p->parentlink;
81 if (n->parentlink == &p->left) {
91 p->parentlink = &n->left;
95 n->right->parentlink = &n->right;
98 assert(n->parentlink == &p->right);
99 assert(p->right == n);
108 p->parentlink = &n->right;
112 n->left->parentlink = &n->left;
118 p->left->parentlink = &p->left;
121 p->right->parent = p;
122 p->right->parentlink = &p->right;
128 n->parentlink = plink;
131 // Restore heap condition by moving n towards the root,
136 T *n_type = node_to_type(n);
138 assert(!n->left || !(*node_to_type(n->left) < *n_type));
139 assert(!n->right || !(*node_to_type(n->right) < *n_type));
141 while (n->parent && *n_type < *node_to_type(n->parent)) {
142 assert(!n->left || !(*node_to_type(n->left) < *n_type));
143 assert(!n->right || !(*node_to_type(n->right) < *n_type));
148 assert(!n->left || !(*node_to_type(n->left) < *n_type));
149 assert(!n->right || !(*node_to_type(n->right) < *n_type));
152 // Restore heap condition by moving n towards the leaves,
157 T *n_type = node_to_type(n);
159 assert(!n->parent || !(*n_type < *node_to_type(n->parent)));
161 // If n has a right, it'll have a left.
163 assert(!n->parent || !(*n_type < *node_to_type(n->parent)));
164 Node *child = n->left;
167 *node_to_type(n->right) < *node_to_type(n->left))
170 if (*node_to_type(child) < *n_type)
176 assert(!n->parent || !(*n_type < *node_to_type(n->parent)));
179 Node *get_by_index(size_t num, Node **parent, Node ***link)
181 assert(num_nodes == 0 || top);
182 assert(num <= num_nodes);
184 // It's easier to use 1-based indices, so that the high set bit
185 // perfectly indicates which level the node is on.
190 size_t bit = 1 << ll_get_order_round_down(num);
192 Node **nextlink = ⊤
202 nextlink = &n->right;
219 return num_nodes == 0;
227 return node_to_type(top);
232 Node *n = &t->heap_node;
233 assert(!n->is_on_heap());
235 Node *ret = get_by_index(num_nodes, &n->parent, &n->parentlink);
238 assert(num_nodes == 0 || n->parent);
242 n->left = n->right = NULL;
249 Node *n = &t->heap_node;
250 assert(*n->parentlink == n);
252 if (--num_nodes == 0) {
254 n->parentlink = NULL;
258 Node *parent, **link;
259 Node *last = get_by_index(num_nodes, &parent, &link);
261 assert(last->parent);
262 assert(last->is_on_heap());
264 assert(!last->right);
267 *n->parentlink = last;
269 n->parentlink = NULL;
272 last->left->parent = last;
273 last->left->parentlink = &last->left;
277 last->right->parent = last;
278 last->right->parentlink = &last->right;
285 Node *n = &t->heap_node;
286 assert(*n->parentlink == n);
288 if (!n->parent || *node_to_type(n->parent) < *t)