interval_tree_tmpl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Interval Trees
  3. (C) 2012 Michel Lespinasse <walken@google.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. include/linux/interval_tree_tmpl.h
  16. */
  17. /*
  18. * Template for implementing interval trees
  19. *
  20. * ITSTRUCT: struct type of the interval tree nodes
  21. * ITRB: name of struct rb_node field within ITSTRUCT
  22. * ITTYPE: type of the interval endpoints
  23. * ITSUBTREE: name of ITTYPE field within ITSTRUCT holding last-in-subtree
  24. * ITSTART(n): start endpoint of ITSTRUCT node n
  25. * ITLAST(n): last endpoing of ITSTRUCT node n
  26. * ITSTATIC: 'static' or empty
  27. * ITPREFIX: prefix to use for the inline tree definitions
  28. */
  29. /* IT(name) -> ITPREFIX_name */
  30. #define _ITNAME(prefix, name) prefix ## _ ## name
  31. #define ITNAME(prefix, name) _ITNAME(prefix, name)
  32. #define IT(name) ITNAME(ITPREFIX, name)
  33. /* Callbacks for augmented rbtree insert and remove */
  34. static inline ITTYPE IT(compute_subtree_last)(ITSTRUCT *node)
  35. {
  36. ITTYPE max = ITLAST(node), subtree_last;
  37. if (node->ITRB.rb_left) {
  38. subtree_last = rb_entry(node->ITRB.rb_left,
  39. ITSTRUCT, ITRB)->ITSUBTREE;
  40. if (max < subtree_last)
  41. max = subtree_last;
  42. }
  43. if (node->ITRB.rb_right) {
  44. subtree_last = rb_entry(node->ITRB.rb_right,
  45. ITSTRUCT, ITRB)->ITSUBTREE;
  46. if (max < subtree_last)
  47. max = subtree_last;
  48. }
  49. return max;
  50. }
  51. static void IT(augment_propagate)(struct rb_node *rb, struct rb_node *stop)
  52. {
  53. while (rb != stop) {
  54. ITSTRUCT *node = rb_entry(rb, ITSTRUCT, ITRB);
  55. ITTYPE subtree_last = IT(compute_subtree_last)(node);
  56. if (node->ITSUBTREE == subtree_last)
  57. break;
  58. node->ITSUBTREE = subtree_last;
  59. rb = rb_parent(&node->ITRB);
  60. }
  61. }
  62. static void IT(augment_copy)(struct rb_node *rb_old, struct rb_node *rb_new)
  63. {
  64. ITSTRUCT *old = rb_entry(rb_old, ITSTRUCT, ITRB);
  65. ITSTRUCT *new = rb_entry(rb_new, ITSTRUCT, ITRB);
  66. new->ITSUBTREE = old->ITSUBTREE;
  67. }
  68. static void IT(augment_rotate)(struct rb_node *rb_old, struct rb_node *rb_new)
  69. {
  70. ITSTRUCT *old = rb_entry(rb_old, ITSTRUCT, ITRB);
  71. ITSTRUCT *new = rb_entry(rb_new, ITSTRUCT, ITRB);
  72. new->ITSUBTREE = old->ITSUBTREE;
  73. old->ITSUBTREE = IT(compute_subtree_last)(old);
  74. }
  75. static const struct rb_augment_callbacks IT(augment_callbacks) = {
  76. IT(augment_propagate), IT(augment_copy), IT(augment_rotate)
  77. };
  78. /* Insert / remove interval nodes from the tree */
  79. ITSTATIC void IT(insert)(ITSTRUCT *node, struct rb_root *root)
  80. {
  81. struct rb_node **link = &root->rb_node, *rb_parent = NULL;
  82. ITTYPE start = ITSTART(node), last = ITLAST(node);
  83. ITSTRUCT *parent;
  84. while (*link) {
  85. rb_parent = *link;
  86. parent = rb_entry(rb_parent, ITSTRUCT, ITRB);
  87. if (parent->ITSUBTREE < last)
  88. parent->ITSUBTREE = last;
  89. if (start < ITSTART(parent))
  90. link = &parent->ITRB.rb_left;
  91. else
  92. link = &parent->ITRB.rb_right;
  93. }
  94. node->ITSUBTREE = last;
  95. rb_link_node(&node->ITRB, rb_parent, link);
  96. rb_insert_augmented(&node->ITRB, root, &IT(augment_callbacks));
  97. }
  98. ITSTATIC void IT(remove)(ITSTRUCT *node, struct rb_root *root)
  99. {
  100. rb_erase_augmented(&node->ITRB, root, &IT(augment_callbacks));
  101. }
  102. /*
  103. * Iterate over intervals intersecting [start;last]
  104. *
  105. * Note that a node's interval intersects [start;last] iff:
  106. * Cond1: ITSTART(node) <= last
  107. * and
  108. * Cond2: start <= ITLAST(node)
  109. */
  110. static ITSTRUCT *IT(subtree_search)(ITSTRUCT *node, ITTYPE start, ITTYPE last)
  111. {
  112. while (true) {
  113. /*
  114. * Loop invariant: start <= node->ITSUBTREE
  115. * (Cond2 is satisfied by one of the subtree nodes)
  116. */
  117. if (node->ITRB.rb_left) {
  118. ITSTRUCT *left = rb_entry(node->ITRB.rb_left,
  119. ITSTRUCT, ITRB);
  120. if (start <= left->ITSUBTREE) {
  121. /*
  122. * Some nodes in left subtree satisfy Cond2.
  123. * Iterate to find the leftmost such node N.
  124. * If it also satisfies Cond1, that's the match
  125. * we are looking for. Otherwise, there is no
  126. * matching interval as nodes to the right of N
  127. * can't satisfy Cond1 either.
  128. */
  129. node = left;
  130. continue;
  131. }
  132. }
  133. if (ITSTART(node) <= last) { /* Cond1 */
  134. if (start <= ITLAST(node)) /* Cond2 */
  135. return node; /* node is leftmost match */
  136. if (node->ITRB.rb_right) {
  137. node = rb_entry(node->ITRB.rb_right,
  138. ITSTRUCT, ITRB);
  139. if (start <= node->ITSUBTREE)
  140. continue;
  141. }
  142. }
  143. return NULL; /* No match */
  144. }
  145. }
  146. ITSTATIC ITSTRUCT *IT(iter_first)(struct rb_root *root,
  147. ITTYPE start, ITTYPE last)
  148. {
  149. ITSTRUCT *node;
  150. if (!root->rb_node)
  151. return NULL;
  152. node = rb_entry(root->rb_node, ITSTRUCT, ITRB);
  153. if (node->ITSUBTREE < start)
  154. return NULL;
  155. return IT(subtree_search)(node, start, last);
  156. }
  157. ITSTATIC ITSTRUCT *IT(iter_next)(ITSTRUCT *node, ITTYPE start, ITTYPE last)
  158. {
  159. struct rb_node *rb = node->ITRB.rb_right, *prev;
  160. while (true) {
  161. /*
  162. * Loop invariants:
  163. * Cond1: ITSTART(node) <= last
  164. * rb == node->ITRB.rb_right
  165. *
  166. * First, search right subtree if suitable
  167. */
  168. if (rb) {
  169. ITSTRUCT *right = rb_entry(rb, ITSTRUCT, ITRB);
  170. if (start <= right->ITSUBTREE)
  171. return IT(subtree_search)(right, start, last);
  172. }
  173. /* Move up the tree until we come from a node's left child */
  174. do {
  175. rb = rb_parent(&node->ITRB);
  176. if (!rb)
  177. return NULL;
  178. prev = &node->ITRB;
  179. node = rb_entry(rb, ITSTRUCT, ITRB);
  180. rb = node->ITRB.rb_right;
  181. } while (prev == rb);
  182. /* Check if the node intersects [start;last] */
  183. if (last < ITSTART(node)) /* !Cond1 */
  184. return NULL;
  185. else if (start <= ITLAST(node)) /* Cond2 */
  186. return node;
  187. }
  188. }