interval_tree.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <linux/init.h>
  2. #include <linux/interval_tree.h>
  3. /* Callbacks for augmented rbtree insert and remove */
  4. static inline unsigned long
  5. compute_subtree_last(struct interval_tree_node *node)
  6. {
  7. unsigned long max = node->last, subtree_last;
  8. if (node->rb.rb_left) {
  9. subtree_last = rb_entry(node->rb.rb_left,
  10. struct interval_tree_node, rb)->__subtree_last;
  11. if (max < subtree_last)
  12. max = subtree_last;
  13. }
  14. if (node->rb.rb_right) {
  15. subtree_last = rb_entry(node->rb.rb_right,
  16. struct interval_tree_node, rb)->__subtree_last;
  17. if (max < subtree_last)
  18. max = subtree_last;
  19. }
  20. return max;
  21. }
  22. RB_DECLARE_CALLBACKS(static, augment_callbacks, struct interval_tree_node, rb,
  23. unsigned long, __subtree_last, compute_subtree_last)
  24. /* Insert / remove interval nodes from the tree */
  25. void interval_tree_insert(struct interval_tree_node *node,
  26. struct rb_root *root)
  27. {
  28. struct rb_node **link = &root->rb_node, *rb_parent = NULL;
  29. unsigned long start = node->start, last = node->last;
  30. struct interval_tree_node *parent;
  31. while (*link) {
  32. rb_parent = *link;
  33. parent = rb_entry(rb_parent, struct interval_tree_node, rb);
  34. if (parent->__subtree_last < last)
  35. parent->__subtree_last = last;
  36. if (start < parent->start)
  37. link = &parent->rb.rb_left;
  38. else
  39. link = &parent->rb.rb_right;
  40. }
  41. node->__subtree_last = last;
  42. rb_link_node(&node->rb, rb_parent, link);
  43. rb_insert_augmented(&node->rb, root, &augment_callbacks);
  44. }
  45. void interval_tree_remove(struct interval_tree_node *node,
  46. struct rb_root *root)
  47. {
  48. rb_erase_augmented(&node->rb, root, &augment_callbacks);
  49. }
  50. /*
  51. * Iterate over intervals intersecting [start;last]
  52. *
  53. * Note that a node's interval intersects [start;last] iff:
  54. * Cond1: node->start <= last
  55. * and
  56. * Cond2: start <= node->last
  57. */
  58. static struct interval_tree_node *
  59. subtree_search(struct interval_tree_node *node,
  60. unsigned long start, unsigned long last)
  61. {
  62. while (true) {
  63. /*
  64. * Loop invariant: start <= node->__subtree_last
  65. * (Cond2 is satisfied by one of the subtree nodes)
  66. */
  67. if (node->rb.rb_left) {
  68. struct interval_tree_node *left =
  69. rb_entry(node->rb.rb_left,
  70. struct interval_tree_node, rb);
  71. if (start <= left->__subtree_last) {
  72. /*
  73. * Some nodes in left subtree satisfy Cond2.
  74. * Iterate to find the leftmost such node N.
  75. * If it also satisfies Cond1, that's the match
  76. * we are looking for. Otherwise, there is no
  77. * matching interval as nodes to the right of N
  78. * can't satisfy Cond1 either.
  79. */
  80. node = left;
  81. continue;
  82. }
  83. }
  84. if (node->start <= last) { /* Cond1 */
  85. if (start <= node->last) /* Cond2 */
  86. return node; /* node is leftmost match */
  87. if (node->rb.rb_right) {
  88. node = rb_entry(node->rb.rb_right,
  89. struct interval_tree_node, rb);
  90. if (start <= node->__subtree_last)
  91. continue;
  92. }
  93. }
  94. return NULL; /* No match */
  95. }
  96. }
  97. struct interval_tree_node *
  98. interval_tree_iter_first(struct rb_root *root,
  99. unsigned long start, unsigned long last)
  100. {
  101. struct interval_tree_node *node;
  102. if (!root->rb_node)
  103. return NULL;
  104. node = rb_entry(root->rb_node, struct interval_tree_node, rb);
  105. if (node->__subtree_last < start)
  106. return NULL;
  107. return subtree_search(node, start, last);
  108. }
  109. struct interval_tree_node *
  110. interval_tree_iter_next(struct interval_tree_node *node,
  111. unsigned long start, unsigned long last)
  112. {
  113. struct rb_node *rb = node->rb.rb_right, *prev;
  114. while (true) {
  115. /*
  116. * Loop invariants:
  117. * Cond1: node->start <= last
  118. * rb == node->rb.rb_right
  119. *
  120. * First, search right subtree if suitable
  121. */
  122. if (rb) {
  123. struct interval_tree_node *right =
  124. rb_entry(rb, struct interval_tree_node, rb);
  125. if (start <= right->__subtree_last)
  126. return subtree_search(right, start, last);
  127. }
  128. /* Move up the tree until we come from a node's left child */
  129. do {
  130. rb = rb_parent(&node->rb);
  131. if (!rb)
  132. return NULL;
  133. prev = &node->rb;
  134. node = rb_entry(rb, struct interval_tree_node, rb);
  135. rb = node->rb.rb_right;
  136. } while (prev == rb);
  137. /* Check if the node intersects [start;last] */
  138. if (last < node->start) /* !Cond1 */
  139. return NULL;
  140. else if (start <= node->last) /* Cond2 */
  141. return node;
  142. }
  143. }