disjoint_interval_tree.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #pragma once
  2. #include <util/generic/map.h>
  3. #include <util/system/yassert.h>
  4. #include <type_traits>
  5. template <class T>
  6. class TDisjointIntervalTree {
  7. private:
  8. static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value");
  9. using TTree = TMap<T, T>; // [key, value)
  10. using TIterator = typename TTree::iterator;
  11. using TConstIterator = typename TTree::const_iterator;
  12. using TReverseIterator = typename TTree::reverse_iterator;
  13. using TThis = TDisjointIntervalTree<T>;
  14. TTree Tree;
  15. size_t NumElements;
  16. public:
  17. TDisjointIntervalTree()
  18. : NumElements()
  19. {
  20. }
  21. void Insert(const T t) {
  22. InsertInterval(t, t + 1);
  23. }
  24. // we assume that none of elements from [begin, end) belong to tree.
  25. void InsertInterval(const T begin, const T end) {
  26. InsertIntervalImpl(begin, end);
  27. NumElements += (size_t)(end - begin);
  28. }
  29. bool Has(const T t) const {
  30. return const_cast<TThis*>(this)->FindContaining(t) != Tree.end();
  31. }
  32. bool Intersects(const T begin, const T end) {
  33. if (Empty()) {
  34. return false;
  35. }
  36. TIterator l = Tree.lower_bound(begin);
  37. if (l != Tree.end()) {
  38. if (l->first < end) {
  39. return true;
  40. } else if (l != Tree.begin()) {
  41. --l;
  42. return l->second > begin;
  43. } else {
  44. return false;
  45. }
  46. } else {
  47. auto last = Tree.rbegin();
  48. return begin < last->second;
  49. }
  50. }
  51. TConstIterator FindContaining(const T t) const {
  52. return const_cast<TThis*>(this)->FindContaining(t);
  53. }
  54. // Erase element. Returns true when element has been deleted, otherwise false.
  55. bool Erase(const T t) {
  56. TIterator n = FindContaining(t);
  57. if (n == Tree.end()) {
  58. return false;
  59. }
  60. --NumElements;
  61. T& begin = const_cast<T&>(n->first);
  62. T& end = const_cast<T&>(n->second);
  63. // Optimization hack.
  64. if (t == begin) {
  65. if (++begin == end) { // OK to change key since intervals do not intersect.
  66. Tree.erase(n);
  67. return true;
  68. }
  69. } else if (t == end - 1) {
  70. --end;
  71. } else {
  72. const T e = end;
  73. end = t;
  74. InsertIntervalImpl(t + 1, e);
  75. }
  76. Y_ASSERT(begin < end);
  77. return true;
  78. }
  79. // Erase interval. Returns number of elements removed from set.
  80. size_t EraseInterval(const T begin, const T end) {
  81. Y_ASSERT(begin < end);
  82. if (Empty()) {
  83. return 0;
  84. }
  85. size_t elementsRemoved = 0;
  86. TIterator completelyRemoveBegin = Tree.lower_bound(begin);
  87. if ((completelyRemoveBegin != Tree.end() && completelyRemoveBegin->first > begin && completelyRemoveBegin != Tree.begin())
  88. || completelyRemoveBegin == Tree.end()) {
  89. // Look at the interval. It could contain [begin, end).
  90. TIterator containingBegin = completelyRemoveBegin;
  91. --containingBegin;
  92. if (containingBegin->first < begin && begin < containingBegin->second) { // Contains begin.
  93. if (containingBegin->second > end) { // Contains end.
  94. const T prevEnd = containingBegin->second;
  95. Y_ASSERT(containingBegin->second - begin <= NumElements);
  96. Y_ASSERT(containingBegin->second - containingBegin->first > end - begin);
  97. containingBegin->second = begin;
  98. InsertIntervalImpl(end, prevEnd);
  99. elementsRemoved = end - begin;
  100. NumElements -= elementsRemoved;
  101. return elementsRemoved;
  102. } else {
  103. elementsRemoved += containingBegin->second - begin;
  104. containingBegin->second = begin;
  105. }
  106. }
  107. }
  108. TIterator completelyRemoveEnd = completelyRemoveBegin != Tree.end() ? Tree.lower_bound(end) : Tree.end();
  109. if (completelyRemoveEnd != Tree.begin() && (completelyRemoveEnd == Tree.end() || completelyRemoveEnd->first != end)) {
  110. TIterator containingEnd = completelyRemoveEnd;
  111. --containingEnd;
  112. if (containingEnd->second > end) {
  113. T& leftBorder = const_cast<T&>(containingEnd->first);
  114. Y_ASSERT(leftBorder < end);
  115. --completelyRemoveEnd; // Don't remove the whole interval.
  116. // Optimization hack.
  117. elementsRemoved += end - leftBorder;
  118. leftBorder = end; // OK to change key since intervals do not intersect.
  119. }
  120. }
  121. for (TIterator i = completelyRemoveBegin; i != completelyRemoveEnd; ++i) {
  122. elementsRemoved += i->second - i->first;
  123. }
  124. Tree.erase(completelyRemoveBegin, completelyRemoveEnd);
  125. Y_ASSERT(elementsRemoved <= NumElements);
  126. NumElements -= elementsRemoved;
  127. return elementsRemoved;
  128. }
  129. void Swap(TDisjointIntervalTree& rhv) {
  130. Tree.swap(rhv.Tree);
  131. std::swap(NumElements, rhv.NumElements);
  132. }
  133. void Clear() {
  134. Tree.clear();
  135. NumElements = 0;
  136. }
  137. bool Empty() const {
  138. return Tree.empty();
  139. }
  140. size_t GetNumElements() const {
  141. return NumElements;
  142. }
  143. size_t GetNumIntervals() const {
  144. return Tree.size();
  145. }
  146. T Min() const {
  147. Y_ASSERT(!Empty());
  148. return Tree.begin()->first;
  149. }
  150. T Max() const {
  151. Y_ASSERT(!Empty());
  152. return Tree.rbegin()->second;
  153. }
  154. TConstIterator begin() const {
  155. return Tree.begin();
  156. }
  157. TConstIterator end() const {
  158. return Tree.end();
  159. }
  160. private:
  161. void InsertIntervalImpl(const T begin, const T end) {
  162. Y_ASSERT(begin < end);
  163. Y_ASSERT(!Intersects(begin, end));
  164. TIterator l = Tree.lower_bound(begin);
  165. TIterator p = Tree.end();
  166. if (l != Tree.begin()) {
  167. p = l;
  168. --p;
  169. }
  170. #ifndef NDEBUG
  171. TIterator u = Tree.upper_bound(begin);
  172. Y_DEBUG_ABORT_UNLESS(u == Tree.end() || u->first >= end, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, u->first, u->second);
  173. Y_DEBUG_ABORT_UNLESS(l == Tree.end() || l == u, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, l->first, l->second);
  174. Y_DEBUG_ABORT_UNLESS(p == Tree.end() || p->second <= begin, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, p->first, p->second);
  175. #endif
  176. // try to extend interval
  177. if (p != Tree.end() && p->second == begin) {
  178. p->second = end;
  179. //Try to merge 2 intervals - p and next one if possible
  180. auto next = p;
  181. // Next is not Tree.end() here.
  182. ++next;
  183. if (next != Tree.end() && next->first == end) {
  184. p->second = next->second;
  185. Tree.erase(next);
  186. }
  187. // Maybe new interval extends right interval
  188. } else if (l != Tree.end() && end == l->first) {
  189. T& leftBorder = const_cast<T&>(l->first);
  190. // Optimization hack.
  191. leftBorder = begin; // OK to change key since intervals do not intersect.
  192. } else {
  193. Tree.insert(std::make_pair(begin, end));
  194. }
  195. }
  196. TIterator FindContaining(const T t) {
  197. TIterator l = Tree.lower_bound(t);
  198. if (l != Tree.end()) {
  199. if (l->first == t) {
  200. return l;
  201. }
  202. Y_ASSERT(l->first > t);
  203. if (l == Tree.begin()) {
  204. return Tree.end();
  205. }
  206. --l;
  207. Y_ASSERT(l->first != t);
  208. if (l->first < t && t < l->second) {
  209. return l;
  210. }
  211. } else if (!Tree.empty()) { // l is larger than Begin of any interval, but maybe it belongs to last interval?
  212. TReverseIterator last = Tree.rbegin();
  213. Y_ASSERT(last->first != t);
  214. if (last->first < t && t < last->second) {
  215. return (++last).base();
  216. }
  217. }
  218. return Tree.end();
  219. }
  220. };