3 (C) 2012 Michel Lespinasse <walken@google.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 include/linux/interval_tree_generic.h
24 #include "rbtree_augmented.h"
27 * Template for implementing interval trees
29 * ITSTRUCT: struct type of the interval tree nodes
30 * ITRB: name of struct rb_node field within ITSTRUCT
31 * ITTYPE: type of the interval endpoints
32 * ITSUBTREE: name of ITTYPE field within ITSTRUCT holding last-in-subtree
33 * ITSTART(n): start endpoint of ITSTRUCT node n
34 * ITLAST(n): last endpoint of ITSTRUCT node n
35 * ITSTATIC: 'static' or empty
36 * ITPREFIX: prefix to use for the inline tree definitions
38 * Note - before using this, please consider if non-generic version
39 * (interval_tree.h) would work for you...
42 #define INTERVAL_TREE_DEFINE(ITSTRUCT, ITRB, ITTYPE, ITSUBTREE, \
43 ITSTART, ITLAST, ITSTATIC, ITPREFIX) \
45 /* Callbacks for augmented rbtree insert and remove */ \
47 static inline ITTYPE ITPREFIX ## _compute_subtree_last(ITSTRUCT *node) \
49 ITTYPE max = ITLAST(node), subtree_last; \
50 if (node->ITRB.rb_left) { \
51 subtree_last = rb_entry(node->ITRB.rb_left, \
52 ITSTRUCT, ITRB)->ITSUBTREE; \
53 if (max < subtree_last) \
56 if (node->ITRB.rb_right) { \
57 subtree_last = rb_entry(node->ITRB.rb_right, \
58 ITSTRUCT, ITRB)->ITSUBTREE; \
59 if (max < subtree_last) \
65 RB_DECLARE_CALLBACKS(static, ITPREFIX ## _augment, ITSTRUCT, ITRB, \
66 ITTYPE, ITSUBTREE, ITPREFIX ## _compute_subtree_last) \
68 /* Insert / remove interval nodes from the tree */ \
70 ITSTATIC void ITPREFIX ## _insert(ITSTRUCT *node, struct rb_root *root) \
72 struct rb_node **link = &root->rb_node, *rb_parent = NULL; \
73 ITTYPE start = ITSTART(node), last = ITLAST(node); \
78 parent = rb_entry(rb_parent, ITSTRUCT, ITRB); \
79 if (parent->ITSUBTREE < last) \
80 parent->ITSUBTREE = last; \
81 if (start < ITSTART(parent)) \
82 link = &parent->ITRB.rb_left; \
84 link = &parent->ITRB.rb_right; \
87 node->ITSUBTREE = last; \
88 rb_link_node(&node->ITRB, rb_parent, link); \
89 rb_insert_augmented(&node->ITRB, root, &ITPREFIX ## _augment); \
92 ITSTATIC void ITPREFIX ## _remove(ITSTRUCT *node, struct rb_root *root) \
94 rb_erase_augmented(&node->ITRB, root, &ITPREFIX ## _augment); \
98 * Iterate over intervals intersecting [start;last] \
100 * Note that a node's interval intersects [start;last] iff: \
101 * Cond1: ITSTART(node) <= last \
103 * Cond2: start <= ITLAST(node) \
107 ITPREFIX ## _subtree_search(ITSTRUCT *node, ITTYPE start, ITTYPE last) \
111 * Loop invariant: start <= node->ITSUBTREE \
112 * (Cond2 is satisfied by one of the subtree nodes) \
114 if (node->ITRB.rb_left) { \
115 ITSTRUCT *left = rb_entry(node->ITRB.rb_left, \
117 if (start <= left->ITSUBTREE) { \
119 * Some nodes in left subtree satisfy Cond2. \
120 * Iterate to find the leftmost such node N. \
121 * If it also satisfies Cond1, that's the \
122 * match we are looking for. Otherwise, there \
123 * is no matching interval as nodes to the \
124 * right of N can't satisfy Cond1 either. \
130 if (ITSTART(node) <= last) { /* Cond1 */ \
131 if (start <= ITLAST(node)) /* Cond2 */ \
132 return node; /* node is leftmost match */ \
133 if (node->ITRB.rb_right) { \
134 node = rb_entry(node->ITRB.rb_right, \
136 if (start <= node->ITSUBTREE) \
140 return NULL; /* No match */ \
144 ITSTATIC ITSTRUCT * \
145 ITPREFIX ## _iter_first(struct rb_root *root, ITTYPE start, ITTYPE last) \
149 if (!root->rb_node) \
151 node = rb_entry(root->rb_node, ITSTRUCT, ITRB); \
152 if (node->ITSUBTREE < start) \
154 return ITPREFIX ## _subtree_search(node, start, last); \
157 ITSTATIC ITSTRUCT * \
158 ITPREFIX ## _iter_next(ITSTRUCT *node, ITTYPE start, ITTYPE last) \
160 struct rb_node *rb = node->ITRB.rb_right, *prev; \
165 * Cond1: ITSTART(node) <= last \
166 * rb == node->ITRB.rb_right \
168 * First, search right subtree if suitable \
171 ITSTRUCT *right = rb_entry(rb, ITSTRUCT, ITRB); \
172 if (start <= right->ITSUBTREE) \
173 return ITPREFIX ## _subtree_search(right, \
177 /* Move up the tree until we come from a node's left child */ \
179 rb = rb_parent(&node->ITRB); \
182 prev = &node->ITRB; \
183 node = rb_entry(rb, ITSTRUCT, ITRB); \
184 rb = node->ITRB.rb_right; \
185 } while (prev == rb); \
187 /* Check if the node intersects [start;last] */ \
188 if (last < ITSTART(node)) /* !Cond1 */ \
190 else if (start <= ITLAST(node)) /* Cond2 */ \