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
17 #include "prio_tree.h"
20 * A clever mix of heap and radix trees forms a radix priority search tree (PST)
21 * which is useful for storing intervals, e.g, we can consider a vma as a closed
22 * interval of file pages [offset_begin, offset_end], and store all vmas that
23 * map a file in a PST. Then, using the PST, we can answer a stabbing query,
24 * i.e., selecting a set of stored intervals (vmas) that overlap with (map) a
25 * given input interval X (a set of consecutive file pages), in "O(log n + m)"
26 * time where 'log n' is the height of the PST, and 'm' is the number of stored
27 * intervals (vmas) that overlap (map) with the input interval X (the set of
28 * consecutive file pages).
30 * In our implementation, we store closed intervals of the form [radix_index,
31 * heap_index]. We assume that always radix_index <= heap_index. McCreight's PST
32 * is designed for storing intervals with unique radix indices, i.e., each
33 * interval have different radix_index. However, this limitation can be easily
34 * overcome by using the size, i.e., heap_index - radix_index, as part of the
35 * index, so we index the tree using [(radix_index,size), heap_index].
37 * When the above-mentioned indexing scheme is used, theoretically, in a 32 bit
38 * machine, the maximum height of a PST can be 64. We can use a balanced version
39 * of the priority search tree to optimize the tree height, but the balanced
40 * tree proposed by McCreight is too complex and memory-hungry for our purpose.
43 static void get_index(const struct prio_tree_node *node,
44 unsigned long *radix, unsigned long *heap)
50 static unsigned long index_bits_to_maxindex[BITS_PER_LONG];
52 static void fio_init prio_tree_init(void)
56 for (i = 0; i < ARRAY_SIZE(index_bits_to_maxindex) - 1; i++)
57 index_bits_to_maxindex[i] = (1UL << (i + 1)) - 1;
58 index_bits_to_maxindex[ARRAY_SIZE(index_bits_to_maxindex) - 1] = ~0UL;
62 * Maximum heap_index that can be stored in a PST with index_bits bits
64 static inline unsigned long prio_tree_maxindex(unsigned int bits)
66 return index_bits_to_maxindex[bits - 1];
70 * Extend a priority search tree so that it can store a node with heap_index
71 * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
72 * However, this function is used rarely and the common case performance is
75 static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
76 struct prio_tree_node *node, unsigned long max_heap_index)
78 struct prio_tree_node *first = NULL, *prev, *last = NULL;
80 if (max_heap_index > prio_tree_maxindex(root->index_bits))
83 while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
86 if (prio_tree_empty(root))
90 first = root->prio_tree_node;
91 prio_tree_remove(root, root->prio_tree_node);
92 INIT_PRIO_TREE_NODE(first);
96 last = root->prio_tree_node;
97 prio_tree_remove(root, root->prio_tree_node);
98 INIT_PRIO_TREE_NODE(last);
104 INIT_PRIO_TREE_NODE(node);
108 first->parent = node;
112 if (!prio_tree_empty(root)) {
113 last->left = root->prio_tree_node;
114 last->left->parent = last;
117 root->prio_tree_node = node;
122 * Replace a prio_tree_node with a new node and return the old node
124 struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
125 struct prio_tree_node *old, struct prio_tree_node *node)
127 INIT_PRIO_TREE_NODE(node);
129 if (prio_tree_root(old)) {
130 assert(root->prio_tree_node == old);
132 * We can reduce root->index_bits here. However, it is complex
133 * and does not help much to improve performance (IMO).
136 root->prio_tree_node = node;
138 node->parent = old->parent;
139 if (old->parent->left == old)
140 old->parent->left = node;
142 old->parent->right = node;
145 if (!prio_tree_left_empty(old)) {
146 node->left = old->left;
147 old->left->parent = node;
150 if (!prio_tree_right_empty(old)) {
151 node->right = old->right;
152 old->right->parent = node;
159 * Insert a prio_tree_node @node into a radix priority search tree @root. The
160 * algorithm typically takes O(log n) time where 'log n' is the number of bits
161 * required to represent the maximum heap_index. In the worst case, the algo
162 * can take O((log n)^2) - check prio_tree_expand.
164 * If a prior node with same radix_index and heap_index is already found in
165 * the tree, then returns the address of the prior node. Otherwise, inserts
166 * @node into the tree and returns @node.
168 struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
169 struct prio_tree_node *node)
171 struct prio_tree_node *cur, *res = node;
172 unsigned long radix_index, heap_index;
173 unsigned long r_index, h_index, index, mask;
176 get_index(node, &radix_index, &heap_index);
178 if (prio_tree_empty(root) ||
179 heap_index > prio_tree_maxindex(root->index_bits))
180 return prio_tree_expand(root, node, heap_index);
182 cur = root->prio_tree_node;
183 mask = 1UL << (root->index_bits - 1);
186 get_index(cur, &r_index, &h_index);
188 if (r_index == radix_index && h_index == heap_index)
191 if (h_index < heap_index ||
192 (h_index == heap_index && r_index > radix_index)) {
193 struct prio_tree_node *tmp = node;
194 node = prio_tree_replace(root, cur, node);
198 r_index = radix_index;
201 h_index = heap_index;
206 index = heap_index - radix_index;
211 if (prio_tree_right_empty(cur)) {
212 INIT_PRIO_TREE_NODE(node);
219 if (prio_tree_left_empty(cur)) {
220 INIT_PRIO_TREE_NODE(node);
231 mask = 1UL << (BITS_PER_LONG - 1);
235 /* Should not reach here */
241 * Remove a prio_tree_node @node from a radix priority search tree @root. The
242 * algorithm takes O(log n) time where 'log n' is the number of bits required
243 * to represent the maximum heap_index.
245 void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
247 struct prio_tree_node *cur;
248 unsigned long r_index, h_index_right, h_index_left;
252 while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
253 if (!prio_tree_left_empty(cur))
254 get_index(cur->left, &r_index, &h_index_left);
260 if (!prio_tree_right_empty(cur))
261 get_index(cur->right, &r_index, &h_index_right);
267 /* both h_index_left and h_index_right cannot be 0 */
268 if (h_index_left >= h_index_right)
274 if (prio_tree_root(cur)) {
275 assert(root->prio_tree_node == cur);
276 INIT_PRIO_TREE_ROOT(root);
280 if (cur->parent->right == cur)
281 cur->parent->right = cur->parent;
283 cur->parent->left = cur->parent;
286 cur = prio_tree_replace(root, cur->parent, cur);
290 * Following functions help to enumerate all prio_tree_nodes in the tree that
291 * overlap with the input interval X [radix_index, heap_index]. The enumeration
292 * takes O(log n + m) time where 'log n' is the height of the tree (which is
293 * proportional to # of bits required to represent the maximum heap_index) and
294 * 'm' is the number of prio_tree_nodes that overlap the interval X.
297 static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
298 unsigned long *r_index, unsigned long *h_index)
300 if (prio_tree_left_empty(iter->cur))
303 get_index(iter->cur->left, r_index, h_index);
305 if (iter->r_index <= *h_index) {
306 iter->cur = iter->cur->left;
309 if (iter->size_level)
312 if (iter->size_level) {
313 assert(prio_tree_left_empty(iter->cur));
314 assert(prio_tree_right_empty(iter->cur));
316 iter->mask = ULONG_MAX;
318 iter->size_level = 1;
319 iter->mask = 1UL << (BITS_PER_LONG - 1);
328 static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
329 unsigned long *r_index, unsigned long *h_index)
333 if (prio_tree_right_empty(iter->cur))
336 if (iter->size_level)
339 value = iter->value | iter->mask;
341 if (iter->h_index < value)
344 get_index(iter->cur->right, r_index, h_index);
346 if (iter->r_index <= *h_index) {
347 iter->cur = iter->cur->right;
351 if (iter->size_level)
354 if (iter->size_level) {
355 assert(prio_tree_left_empty(iter->cur));
356 assert(prio_tree_right_empty(iter->cur));
358 iter->mask = ULONG_MAX;
360 iter->size_level = 1;
361 iter->mask = 1UL << (BITS_PER_LONG - 1);
370 static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
372 iter->cur = iter->cur->parent;
373 if (iter->mask == ULONG_MAX)
375 else if (iter->size_level == 1)
379 if (iter->size_level)
381 if (!iter->size_level && (iter->value & iter->mask))
382 iter->value ^= iter->mask;
386 static inline int overlap(struct prio_tree_iter *iter,
387 unsigned long r_index, unsigned long h_index)
389 return iter->h_index >= r_index && iter->r_index <= h_index;
395 * Get the first prio_tree_node that overlaps with the interval [radix_index,
396 * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
397 * traversal of the tree.
399 static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
401 struct prio_tree_root *root;
402 unsigned long r_index, h_index;
404 INIT_PRIO_TREE_ITER(iter);
407 if (prio_tree_empty(root))
410 get_index(root->prio_tree_node, &r_index, &h_index);
412 if (iter->r_index > h_index)
415 iter->mask = 1UL << (root->index_bits - 1);
416 iter->cur = root->prio_tree_node;
419 if (overlap(iter, r_index, h_index))
422 if (prio_tree_left(iter, &r_index, &h_index))
425 if (prio_tree_right(iter, &r_index, &h_index))
436 * Get the next prio_tree_node that overlaps with the input interval in iter
438 struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
440 unsigned long r_index, h_index;
442 if (iter->cur == NULL)
443 return prio_tree_first(iter);
446 while (prio_tree_left(iter, &r_index, &h_index))
447 if (overlap(iter, r_index, h_index))
450 while (!prio_tree_right(iter, &r_index, &h_index)) {
451 while (!prio_tree_root(iter->cur) &&
452 iter->cur->parent->right == iter->cur)
453 prio_tree_parent(iter);
455 if (prio_tree_root(iter->cur))
458 prio_tree_parent(iter);
461 if (overlap(iter, r_index, h_index))