interval_tree_generic.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_generic.h
  16. */
  17. #include <linux/rbtree_augmented.h>
  18. /*
  19. * Template for implementing interval trees
  20. *
  21. * ITSTRUCT: struct type of the interval tree nodes
  22. * ITRB: name of struct rb_node field within ITSTRUCT
  23. * ITTYPE: type of the interval endpoints
  24. * ITSUBTREE: name of ITTYPE field within ITSTRUCT holding last-in-subtree
  25. * ITSTART(n): start endpoint of ITSTRUCT node n
  26. * ITLAST(n): last endpoint of ITSTRUCT node n
  27. * ITSTATIC: 'static' or empty
  28. * ITPREFIX: prefix to use for the inline tree definitions
  29. *
  30. * Note - before using this, please consider if non-generic version
  31. * (interval_tree.h) would work for you...
  32. */
  33. #define INTERVAL_TREE_DEFINE(ITSTRUCT, ITRB, ITTYPE, ITSUBTREE, \
  34. ITSTART, ITLAST, ITSTATIC, ITPREFIX) \
  35. \
  36. /* Callbacks for augmented rbtree insert and remove */ \
  37. \
  38. static inline ITTYPE ITPREFIX ## _compute_subtree_last(ITSTRUCT *node) \
  39. { \
  40. ITTYPE max = ITLAST(node), subtree_last; \
  41. if (node->ITRB.rb_left) { \
  42. subtree_last = rb_entry(node->ITRB.rb_left, \
  43. ITSTRUCT, ITRB)->ITSUBTREE; \
  44. if (max < subtree_last) \
  45. max = subtree_last; \
  46. } \
  47. if (node->ITRB.rb_right) { \
  48. subtree_last = rb_entry(node->ITRB.rb_right, \
  49. ITSTRUCT, ITRB)->ITSUBTREE; \
  50. if (max < subtree_last) \
  51. max = subtree_last; \
  52. } \
  53. return max; \
  54. } \
  55. \
  56. RB_DECLARE_CALLBACKS(static, ITPREFIX ## _augment, ITSTRUCT, ITRB, \
  57. ITTYPE, ITSUBTREE, ITPREFIX ## _compute_subtree_last) \
  58. \
  59. /* Insert / remove interval nodes from the tree */ \
  60. \
  61. ITSTATIC void ITPREFIX ## _insert(ITSTRUCT *node, struct rb_root *root) \
  62. { \
  63. struct rb_node **link = &root->rb_node, *rb_parent = NULL; \
  64. ITTYPE start = ITSTART(node), last = ITLAST(node); \
  65. ITSTRUCT *parent; \
  66. \
  67. while (*link) { \
  68. rb_parent = *link; \
  69. parent = rb_entry(rb_parent, ITSTRUCT, ITRB); \
  70. if (parent->ITSUBTREE < last) \
  71. parent->ITSUBTREE = last; \
  72. if (start < ITSTART(parent)) \
  73. link = &parent->ITRB.rb_left; \
  74. else \
  75. link = &parent->ITRB.rb_right; \
  76. } \
  77. \
  78. node->ITSUBTREE = last; \
  79. rb_link_node(&node->ITRB, rb_parent, link); \
  80. rb_insert_augmented(&node->ITRB, root, &ITPREFIX ## _augment); \
  81. } \
  82. \
  83. ITSTATIC void ITPREFIX ## _remove(ITSTRUCT *node, struct rb_root *root) \
  84. { \
  85. rb_erase_augmented(&node->ITRB, root, &ITPREFIX ## _augment); \
  86. } \
  87. \
  88. /* \
  89. * Iterate over intervals intersecting [start;last] \
  90. * \
  91. * Note that a node's interval intersects [start;last] iff: \
  92. * Cond1: ITSTART(node) <= last \
  93. * and \
  94. * Cond2: start <= ITLAST(node) \
  95. */ \
  96. \
  97. static ITSTRUCT * \
  98. ITPREFIX ## _subtree_search(ITSTRUCT *node, ITTYPE start, ITTYPE last) \
  99. { \
  100. while (true) { \
  101. /* \
  102. * Loop invariant: start <= node->ITSUBTREE \
  103. * (Cond2 is satisfied by one of the subtree nodes) \
  104. */ \
  105. if (node->ITRB.rb_left) { \
  106. ITSTRUCT *left = rb_entry(node->ITRB.rb_left, \
  107. ITSTRUCT, ITRB); \
  108. if (start <= left->ITSUBTREE) { \
  109. /* \
  110. * Some nodes in left subtree satisfy Cond2. \
  111. * Iterate to find the leftmost such node N. \
  112. * If it also satisfies Cond1, that's the \
  113. * match we are looking for. Otherwise, there \
  114. * is no matching interval as nodes to the \
  115. * right of N can't satisfy Cond1 either. \
  116. */ \
  117. node = left; \
  118. continue; \
  119. } \
  120. } \
  121. if (ITSTART(node) <= last) { /* Cond1 */ \
  122. if (start <= ITLAST(node)) /* Cond2 */ \
  123. return node; /* node is leftmost match */ \
  124. if (node->ITRB.rb_right) { \
  125. node = rb_entry(node->ITRB.rb_right, \
  126. ITSTRUCT, ITRB); \
  127. if (start <= node->ITSUBTREE) \
  128. continue; \
  129. } \
  130. } \
  131. return NULL; /* No match */ \
  132. } \
  133. } \
  134. \
  135. ITSTATIC ITSTRUCT * \
  136. ITPREFIX ## _iter_first(struct rb_root *root, ITTYPE start, ITTYPE last) \
  137. { \
  138. ITSTRUCT *node; \
  139. \
  140. if (!root->rb_node) \
  141. return NULL; \
  142. node = rb_entry(root->rb_node, ITSTRUCT, ITRB); \
  143. if (node->ITSUBTREE < start) \
  144. return NULL; \
  145. return ITPREFIX ## _subtree_search(node, start, last); \
  146. } \
  147. \
  148. ITSTATIC ITSTRUCT * \
  149. ITPREFIX ## _iter_next(ITSTRUCT *node, ITTYPE start, ITTYPE last) \
  150. { \
  151. struct rb_node *rb = node->ITRB.rb_right, *prev; \
  152. \
  153. while (true) { \
  154. /* \
  155. * Loop invariants: \
  156. * Cond1: ITSTART(node) <= last \
  157. * rb == node->ITRB.rb_right \
  158. * \
  159. * First, search right subtree if suitable \
  160. */ \
  161. if (rb) { \
  162. ITSTRUCT *right = rb_entry(rb, ITSTRUCT, ITRB); \
  163. if (start <= right->ITSUBTREE) \
  164. return ITPREFIX ## _subtree_search(right, \
  165. start, last); \
  166. } \
  167. \
  168. /* Move up the tree until we come from a node's left child */ \
  169. do { \
  170. rb = rb_parent(&node->ITRB); \
  171. if (!rb) \
  172. return NULL; \
  173. prev = &node->ITRB; \
  174. node = rb_entry(rb, ITSTRUCT, ITRB); \
  175. rb = node->ITRB.rb_right; \
  176. } while (prev == rb); \
  177. \
  178. /* Check if the node intersects [start;last] */ \
  179. if (last < ITSTART(node)) /* !Cond1 */ \
  180. return NULL; \
  181. else if (start <= ITLAST(node)) /* Cond2 */ \
  182. return node; \
  183. } \
  184. }