2 * lib/prio_tree.c - priority search tree
4 * Copyright (C) 2004, Rajesh Venkatasubramanian <vrajesh@umich.edu>
6 * This file is released under the GPL v2.
8 * Based on the radix priority search tree proposed by Edward M. McCreight
9 * SIAM Journal of Computing, vol. 14, no.2, pages 257-276, May 1985
11 * 02Feb2004 Initial version
18 #include "../compiler/compiler.h"
19 #include "prio_tree.h"
21 #define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0])))
24 * A clever mix of heap and radix trees forms a radix priority search tree (PST)
25 * which is useful for storing intervals, e.g, we can consider a vma as a closed
26 * interval of file pages [offset_begin, offset_end], and store all vmas that
27 * map a file in a PST. Then, using the PST, we can answer a stabbing query,
28 * i.e., selecting a set of stored intervals (vmas) that overlap with (map) a
29 * given input interval X (a set of consecutive file pages), in "O(log n + m)"
30 * time where 'log n' is the height of the PST, and 'm' is the number of stored
31 * intervals (vmas) that overlap (map) with the input interval X (the set of
32 * consecutive file pages).
34 * In our implementation, we store closed intervals of the form [radix_index,
35 * heap_index]. We assume that always radix_index <= heap_index. McCreight's PST
36 * is designed for storing intervals with unique radix indices, i.e., each
37 * interval have different radix_index. However, this limitation can be easily
38 * overcome by using the size, i.e., heap_index - radix_index, as part of the
39 * index, so we index the tree using [(radix_index,size), heap_index].
41 * When the above-mentioned indexing scheme is used, theoretically, in a 32 bit
42 * machine, the maximum height of a PST can be 64. We can use a balanced version
43 * of the priority search tree to optimize the tree height, but the balanced
44 * tree proposed by McCreight is too complex and memory-hungry for our purpose.
47 static void get_index(const struct prio_tree_node *node,
48 unsigned long *radix, unsigned long *heap)
54 static unsigned long index_bits_to_maxindex[BITS_PER_LONG];
56 static void fio_init prio_tree_init(void)
60 for (i = 0; i < ARRAY_SIZE(index_bits_to_maxindex) - 1; i++)
61 index_bits_to_maxindex[i] = (1UL << (i + 1)) - 1;
62 index_bits_to_maxindex[ARRAY_SIZE(index_bits_to_maxindex) - 1] = ~0UL;
66 * Maximum heap_index that can be stored in a PST with index_bits bits
68 static inline unsigned long prio_tree_maxindex(unsigned int bits)
70 return index_bits_to_maxindex[bits - 1];
74 * Extend a priority search tree so that it can store a node with heap_index
75 * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
76 * However, this function is used rarely and the common case performance is
79 static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
80 struct prio_tree_node *node, unsigned long max_heap_index)
82 struct prio_tree_node *first = NULL, *prev, *last = NULL;
84 if (max_heap_index > prio_tree_maxindex(root->index_bits))
87 while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
90 if (prio_tree_empty(root))
94 first = root->prio_tree_node;
95 prio_tree_remove(root, root->prio_tree_node);
96 INIT_PRIO_TREE_NODE(first);
100 last = root->prio_tree_node;
101 prio_tree_remove(root, root->prio_tree_node);
102 INIT_PRIO_TREE_NODE(last);
108 INIT_PRIO_TREE_NODE(node);
112 first->parent = node;
116 if (!prio_tree_empty(root)) {
117 last->left = root->prio_tree_node;
118 last->left->parent = last;
121 root->prio_tree_node = node;
126 * Replace a prio_tree_node with a new node and return the old node
128 struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
129 struct prio_tree_node *old, struct prio_tree_node *node)
131 INIT_PRIO_TREE_NODE(node);
133 if (prio_tree_root(old)) {
134 assert(root->prio_tree_node == old);
136 * We can reduce root->index_bits here. However, it is complex
137 * and does not help much to improve performance (IMO).
140 root->prio_tree_node = node;
142 node->parent = old->parent;
143 if (old->parent->left == old)
144 old->parent->left = node;
146 old->parent->right = node;
149 if (!prio_tree_left_empty(old)) {
150 node->left = old->left;
151 old->left->parent = node;
154 if (!prio_tree_right_empty(old)) {
155 node->right = old->right;
156 old->right->parent = node;
163 * Insert a prio_tree_node @node into a radix priority search tree @root. The
164 * algorithm typically takes O(log n) time where 'log n' is the number of bits
165 * required to represent the maximum heap_index. In the worst case, the algo
166 * can take O((log n)^2) - check prio_tree_expand.
168 * If a prior node with same radix_index and heap_index is already found in
169 * the tree, then returns the address of the prior node. Otherwise, inserts
170 * @node into the tree and returns @node.
172 struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
173 struct prio_tree_node *node)
175 struct prio_tree_node *cur, *res = node;
176 unsigned long radix_index, heap_index;
177 unsigned long r_index, h_index, index, mask;
180 get_index(node, &radix_index, &heap_index);
182 if (prio_tree_empty(root) ||
183 heap_index > prio_tree_maxindex(root->index_bits))
184 return prio_tree_expand(root, node, heap_index);
186 cur = root->prio_tree_node;
187 mask = 1UL << (root->index_bits - 1);
190 get_index(cur, &r_index, &h_index);
192 if (r_index == radix_index && h_index == heap_index)
195 if (h_index < heap_index ||
196 (h_index == heap_index && r_index > radix_index)) {
197 struct prio_tree_node *tmp = node;
198 node = prio_tree_replace(root, cur, node);
202 r_index = radix_index;
205 h_index = heap_index;
210 index = heap_index - radix_index;
215 if (prio_tree_right_empty(cur)) {
216 INIT_PRIO_TREE_NODE(node);
223 if (prio_tree_left_empty(cur)) {
224 INIT_PRIO_TREE_NODE(node);
235 mask = 1UL << (BITS_PER_LONG - 1);
239 /* Should not reach here */
245 * Remove a prio_tree_node @node from a radix priority search tree @root. The
246 * algorithm takes O(log n) time where 'log n' is the number of bits required
247 * to represent the maximum heap_index.
249 void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
251 struct prio_tree_node *cur;
252 unsigned long r_index, h_index_right, h_index_left;
256 while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
257 if (!prio_tree_left_empty(cur))
258 get_index(cur->left, &r_index, &h_index_left);
264 if (!prio_tree_right_empty(cur))
265 get_index(cur->right, &r_index, &h_index_right);
271 /* both h_index_left and h_index_right cannot be 0 */
272 if (h_index_left >= h_index_right)
278 if (prio_tree_root(cur)) {
279 assert(root->prio_tree_node == cur);
280 INIT_PRIO_TREE_ROOT(root);
284 if (cur->parent->right == cur)
285 cur->parent->right = cur->parent;
287 cur->parent->left = cur->parent;
290 cur = prio_tree_replace(root, cur->parent, cur);
294 * Following functions help to enumerate all prio_tree_nodes in the tree that
295 * overlap with the input interval X [radix_index, heap_index]. The enumeration
296 * takes O(log n + m) time where 'log n' is the height of the tree (which is
297 * proportional to # of bits required to represent the maximum heap_index) and
298 * 'm' is the number of prio_tree_nodes that overlap the interval X.
301 static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
302 unsigned long *r_index, unsigned long *h_index)
304 if (prio_tree_left_empty(iter->cur))
307 get_index(iter->cur->left, r_index, h_index);
309 if (iter->r_index <= *h_index) {
310 iter->cur = iter->cur->left;
313 if (iter->size_level)
316 if (iter->size_level) {
317 assert(prio_tree_left_empty(iter->cur));
318 assert(prio_tree_right_empty(iter->cur));
320 iter->mask = ULONG_MAX;
322 iter->size_level = 1;
323 iter->mask = 1UL << (BITS_PER_LONG - 1);
332 static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
333 unsigned long *r_index, unsigned long *h_index)
337 if (prio_tree_right_empty(iter->cur))
340 if (iter->size_level)
343 value = iter->value | iter->mask;
345 if (iter->h_index < value)
348 get_index(iter->cur->right, r_index, h_index);
350 if (iter->r_index <= *h_index) {
351 iter->cur = iter->cur->right;
355 if (iter->size_level)
358 if (iter->size_level) {
359 assert(prio_tree_left_empty(iter->cur));
360 assert(prio_tree_right_empty(iter->cur));
362 iter->mask = ULONG_MAX;
364 iter->size_level = 1;
365 iter->mask = 1UL << (BITS_PER_LONG - 1);
374 static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
376 iter->cur = iter->cur->parent;
377 if (iter->mask == ULONG_MAX)
379 else if (iter->size_level == 1)
383 if (iter->size_level)
385 if (!iter->size_level && (iter->value & iter->mask))
386 iter->value ^= iter->mask;
390 static inline int overlap(struct prio_tree_iter *iter,
391 unsigned long r_index, unsigned long h_index)
393 return iter->h_index >= r_index && iter->r_index <= h_index;
399 * Get the first prio_tree_node that overlaps with the interval [radix_index,
400 * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
401 * traversal of the tree.
403 static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
405 struct prio_tree_root *root;
406 unsigned long r_index, h_index;
408 INIT_PRIO_TREE_ITER(iter);
411 if (prio_tree_empty(root))
414 get_index(root->prio_tree_node, &r_index, &h_index);
416 if (iter->r_index > h_index)
419 iter->mask = 1UL << (root->index_bits - 1);
420 iter->cur = root->prio_tree_node;
423 if (overlap(iter, r_index, h_index))
426 if (prio_tree_left(iter, &r_index, &h_index))
429 if (prio_tree_right(iter, &r_index, &h_index))
440 * Get the next prio_tree_node that overlaps with the input interval in iter
442 struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
444 unsigned long r_index, h_index;
446 if (iter->cur == NULL)
447 return prio_tree_first(iter);
450 while (prio_tree_left(iter, &r_index, &h_index))
451 if (overlap(iter, r_index, h_index))
454 while (!prio_tree_right(iter, &r_index, &h_index)) {
455 while (!prio_tree_root(iter->cur) &&
456 iter->cur->parent->right == iter->cur)
457 prio_tree_parent(iter);
459 if (prio_tree_root(iter->cur))
462 prio_tree_parent(iter);
465 if (overlap(iter, r_index, h_index))