LLVM 22.0.0git
IntervalTree.h
Go to the documentation of this file.
1//===-- IntervalTree.h ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements an interval tree.
10//
11// Further information:
12// https://en.wikipedia.org/wiki/Interval_tree
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ADT_INTERVALTREE_H
17#define LLVM_ADT_INTERVALTREE_H
18
21#include "llvm/Support/Format.h"
23#include <algorithm>
24#include <cassert>
25#include <iterator>
26
27// IntervalTree is a light tree data structure to hold intervals. It allows
28// finding all intervals that overlap with any given point. At this time,
29// it does not support any deletion or rebalancing operations.
30//
31// The IntervalTree is designed to be set up once, and then queried without
32// any further additions.
33//
34// Synopsis:
35// Closed intervals delimited by PointT objects are mapped to ValueT objects.
36//
37// Restrictions:
38// PointT must be a fundamental type.
39// ValueT must be a fundamental or pointer type.
40//
41// template <typename PointT, typename ValueT, typename DataT>
42// class IntervalTree {
43// public:
44//
45// IntervalTree();
46// ~IntervalTree():
47//
48// using IntervalReferences = SmallVector<IntervalData *>;
49//
50// void create();
51// void insert(PointT Left, PointT Right, ValueT Value);
52//
53// IntervalReferences getContaining(PointT Point);
54// static void sortIntervals(IntervalReferences &Intervals, Sorting Sort);
55//
56// find_iterator begin(PointType Point) const;
57// find_iterator end() const;
58//
59// bool empty() const;
60// void clear();
61//
62// void print(raw_ostream &OS, bool HexFormat = true);
63// };
64//
65//===----------------------------------------------------------------------===//
66//
67// In the below given dataset
68//
69// [a, b] <- (x)
70//
71// 'a' and 'b' describe a range and 'x' the value for that interval.
72//
73// The following data are purely for illustrative purposes:
74//
75// [30, 35] <- (3035), [39, 50] <- (3950), [55, 61] <- (5561),
76// [31, 56] <- (3156), [12, 21] <- (1221), [25, 41] <- (2541),
77// [49, 65] <- (4965), [71, 79] <- (7179), [11, 16] <- (1116),
78// [20, 30] <- (2030), [36, 54] <- (3654), [60, 70] <- (6070),
79// [74, 80] <- (7480), [15, 40] <- (1540), [43, 43] <- (4343),
80// [50, 75] <- (5075), [10, 85] <- (1085)
81//
82// The data represents a set of overlapping intervals:
83//
84// 30--35 39------------50 55----61
85// 31------------------------56
86// 12--------21 25------------41 49-------------65 71-----79
87// 11----16 20-----30 36----------------54 60------70 74---- 80
88// 15---------------------40 43--43 50--------------------75
89// 10----------------------------------------------------------------------85
90//
91// The items are stored in a binary tree with each node storing:
92//
93// MP: A middle point.
94// IL: All intervals whose left value are completely to the left of the middle
95// point. They are sorted in ascending order by their beginning point.
96// IR: All intervals whose right value are completely to the right of the
97// middle point. They are sorted in descending order by their ending point.
98// LS: Left subtree.
99// RS: Right subtree.
100//
101// As IL and IR will contain the same intervals, in order to optimize space,
102// instead of storing intervals on each node, we use two vectors that will
103// contain the intervals described by IL and IR. Each node will contain an
104// index into that vector (global bucket), to indicate the beginning of the
105// intervals assigned to the node.
106//
107// The following is the output from print():
108//
109// 0: MP:43 IR [10,85] [31,56] [36,54] [39,50] [43,43]
110// 0: MP:43 IL [10,85] [31,56] [36,54] [39,50] [43,43]
111// 1: MP:25 IR [25,41] [15,40] [20,30]
112// 1: MP:25 IL [15,40] [20,30] [25,41]
113// 2: MP:15 IR [12,21] [11,16]
114// 2: MP:15 IL [11,16] [12,21]
115// 2: MP:36 IR []
116// 2: MP:36 IL []
117// 3: MP:31 IR [30,35]
118// 3: MP:31 IL [30,35]
119// 1: MP:61 IR [50,75] [60,70] [49,65] [55,61]
120// 1: MP:61 IL [49,65] [50,75] [55,61] [60,70]
121// 2: MP:74 IR [74,80] [71,79]
122// 2: MP:74 IL [71,79] [74,80]
123//
124// with:
125// 0: Root Node.
126// MP: Middle point.
127// IL: Intervals to the left (in ascending order by beginning point).
128// IR: Intervals to the right (in descending order by ending point).
129//
130// Root
131// |
132// V
133// +------------MP:43------------+
134// | IL IR |
135// | [10,85] [10,85] |
136// LS | [31,56] [31,56] | RS
137// | [36,54] [36,54] |
138// | [39,50] [39,50] |
139// | [43,43] [43,43] |
140// V V
141// +------------MP:25------------+ MP:61------------+
142// | IL IR | IL IR |
143// | [15,40] [25,41] | [49,65] [50,75] |
144// LS | [20,30] [15,40] | RS [50,75] [60,70] | RS
145// | [25,41] [20,30] | [55,61] [49,65] |
146// | | [60,70] [55,61] |
147// V V V
148// MP:15 +-------MP:36 MP:74
149// IL IR | IL IR IL IR
150// [11,16] [12,21] LS | [] [] [71,79] [74,80]
151// [12,21] [11,16] | [74,80] [71,79]
152// V
153// MP:31
154// IL IR
155// [30,35] [30,35]
156//
157// The creation of an interval tree is done in 2 steps:
158// 1) Insert the interval items by calling
159// void insert(PointT Left, PointT Right, ValueT Value);
160// Left, Right: the interval left and right limits.
161// Value: the data associated with that specific interval.
162//
163// 2) Create the interval tree by calling
164// void create();
165//
166// Once the tree is created, it is switched to query mode.
167// Query the tree by using iterators or container.
168//
169// a) Iterators over intervals overlapping the given point with very weak
170// ordering guarantees.
171// find_iterator begin(PointType Point) const;
172// find_iterator end() const;
173// Point: a target point to be tested for inclusion in any interval.
174//
175// b) Container:
176// IntervalReferences getContaining(PointT Point);
177// Point: a target point to be tested for inclusion in any interval.
178// Returns vector with all the intervals containing the target point.
179//
180// The returned intervals are in their natural tree location. They can
181// be sorted:
182//
183// static void sortIntervals(IntervalReferences &Intervals, Sorting Sort);
184//
185// Ability to print the constructed interval tree:
186// void print(raw_ostream &OS, bool HexFormat = true);
187// Display the associated data in hexadecimal format.
188
189namespace llvm {
190
191//===----------------------------------------------------------------------===//
192//--- IntervalData ----//
193//===----------------------------------------------------------------------===//
194/// An interval data composed by a \a Left and \a Right points and an
195/// associated \a Value.
196/// \a PointT corresponds to the interval endpoints type.
197/// \a ValueT corresponds to the interval value type.
198template <typename PointT, typename ValueT> class IntervalData {
199protected:
200 using PointType = PointT;
201 using ValueType = ValueT;
202
203private:
204 PointType Left;
205 PointType Right;
206 ValueType Value;
207
208public:
209 IntervalData() = delete;
211 : Left(Left), Right(Right), Value(Value) {
212 assert(Left <= Right && "'Left' must be less or equal to 'Right'");
213 }
214 virtual ~IntervalData() = default;
215 PointType left() const { return Left; }
216 PointType right() const { return Right; }
217 ValueType value() const { return Value; }
218
219 /// Return true if \a Point is inside the left bound of closed interval \a
220 /// [Left;Right]. This is Left <= Point for closed intervals.
221 bool left(const PointType &Point) const { return left() <= Point; }
222
223 /// Return true if \a Point is inside the right bound of closed interval \a
224 /// [Left;Right]. This is Point <= Right for closed intervals.
225 bool right(const PointType &Point) const { return Point <= right(); }
226
227 /// Return true when \a Point is contained in interval \a [Left;Right].
228 /// This is Left <= Point <= Right for closed intervals.
229 bool contains(const PointType &Point) const {
230 return left(Point) && right(Point);
231 }
232};
233
234//===----------------------------------------------------------------------===//
235//--- IntervalTree ----//
236//===----------------------------------------------------------------------===//
237// Helper class template that is used by the IntervalTree to ensure that one
238// does instantiate using only fundamental and/or pointer types.
239template <typename T> using PointTypeIsValid = std::is_fundamental<T>;
240
241template <typename T>
242using ValueTypeIsValid = std::bool_constant<std::is_fundamental<T>::value ||
243 std::is_pointer<T>::value>;
244
245template <typename PointT, typename ValueT,
246 typename DataT = IntervalData<PointT, ValueT>>
249 "PointT must be a fundamental type");
251 "ValueT must be a fundamental or pointer type");
252
253public:
254 using PointType = PointT;
255 using ValueType = ValueT;
256 using DataType = DataT;
258
259 enum class Sorting { Ascending, Descending };
261
262private:
263 using IntervalVector = SmallVector<DataType, 4>;
264 using PointsVector = SmallVector<PointType, 4>;
265
266 class IntervalNode {
267 PointType MiddlePoint; // MP - Middle point.
268 IntervalNode *Left = nullptr; // LS - Left subtree.
269 IntervalNode *Right = nullptr; // RS - Right subtree.
270 unsigned BucketIntervalsStart = 0; // Starting index in global bucket.
271 unsigned BucketIntervalsSize = 0; // Size of bucket.
272
273 public:
274 PointType middle() const { return MiddlePoint; }
275 unsigned start() const { return BucketIntervalsStart; }
276 unsigned size() const { return BucketIntervalsSize; }
277
278 IntervalNode(PointType Point, unsigned Start)
279 : MiddlePoint(Point), BucketIntervalsStart(Start) {}
280
281 friend IntervalTree;
282 };
283
284 Allocator &NodeAllocator; // Allocator used for creating interval nodes.
285 IntervalNode *Root = nullptr; // Interval tree root.
286 IntervalVector Intervals; // Storage for each interval and all of the fields
287 // point back into it.
288 PointsVector EndPoints; // Sorted left and right points of all the intervals.
289
290 // These vectors provide storage that nodes carve buckets of overlapping
291 // intervals out of. All intervals are recorded on each vector.
292 // The bucket with the intervals associated to a node, is determined by
293 // the fields 'BucketIntervalStart' and 'BucketIntervalSize' in the node.
294 // The buckets in the first vector are sorted in ascending order using
295 // the left value and the buckets in the second vector are sorted in
296 // descending order using the right value. Every interval in a bucket
297 // contains the middle point for the node.
298 IntervalReferences IntervalsLeft; // Intervals to the left of middle point.
299 IntervalReferences IntervalsRight; // Intervals to the right of middle point.
300
301 // Working vector used during the tree creation to sort the intervals. It is
302 // cleared once the tree is created.
303 IntervalReferences References;
304
305 /// Recursively delete the constructed tree.
306 void deleteTree(IntervalNode *Node) {
307 if (Node) {
308 deleteTree(Node->Left);
309 deleteTree(Node->Right);
310 Node->~IntervalNode();
311 NodeAllocator.Deallocate(Node);
312 }
313 }
314
315 /// Print the interval list (left and right) for a given \a Node.
316 static void printList(raw_ostream &OS, IntervalReferences &IntervalSet,
317 unsigned Start, unsigned Size, bool HexFormat = true) {
318 assert(Start + Size <= IntervalSet.size() &&
319 "Start + Size must be in bounds of the IntervalSet");
320 const char *Format = HexFormat ? "[0x%08x,0x%08x] " : "[%2d,%2d] ";
321 if (Size) {
322 for (unsigned Position = Start; Position < Start + Size; ++Position)
323 OS << format(Format, IntervalSet[Position]->left(),
324 IntervalSet[Position]->right());
325 } else {
326 OS << "[]";
327 }
328 OS << "\n";
329 }
330
331 /// Print an interval tree \a Node.
332 void printNode(raw_ostream &OS, unsigned Level, IntervalNode *Node,
333 bool HexFormat = true) {
334 const char *Format = HexFormat ? "MP:0x%08x " : "MP:%2d ";
335 auto PrintNodeData = [&](StringRef Text, IntervalReferences &IntervalSet) {
336 OS << format("%5d: ", Level);
337 OS.indent(Level * 2);
338 OS << format(Format, Node->middle()) << Text << " ";
339 printList(OS, IntervalSet, Node->start(), Node->size(), HexFormat);
340 };
341
342 PrintNodeData("IR", IntervalsRight);
343 PrintNodeData("IL", IntervalsLeft);
344 }
345
346 /// Recursively print all the interval nodes.
347 void printTree(raw_ostream &OS, unsigned Level, IntervalNode *Node,
348 bool HexFormat = true) {
349 if (Node) {
350 printNode(OS, Level, Node, HexFormat);
351 ++Level;
352 printTree(OS, Level, Node->Left, HexFormat);
353 printTree(OS, Level, Node->Right, HexFormat);
354 }
355 }
356
357 /// Recursively construct the interval tree.
358 /// IntervalsSize: Number of intervals that have been processed and it will
359 /// be used as the start for the intervals bucket for a node.
360 /// PointsBeginIndex, PointsEndIndex: Determine the range into the EndPoints
361 /// vector of end points to be processed.
362 /// ReferencesBeginIndex, ReferencesSize: Determine the range into the
363 /// intervals being processed.
364 IntervalNode *createTree(unsigned &IntervalsSize, int PointsBeginIndex,
365 int PointsEndIndex, int ReferencesBeginIndex,
366 int ReferencesSize) {
367 // We start by taking the entire range of all the intervals and dividing
368 // it in half at x_middle (in practice, x_middle should be picked to keep
369 // the tree relatively balanced).
370 // This gives three sets of intervals, those completely to the left of
371 // x_middle which we'll call S_left, those completely to the right of
372 // x_middle which we'll call S_right, and those overlapping x_middle
373 // which we'll call S_middle.
374 // The intervals in S_left and S_right are recursively divided in the
375 // same manner until there are no intervals remaining.
376
377 if (PointsBeginIndex > PointsEndIndex ||
378 ReferencesBeginIndex >= ReferencesSize)
379 return nullptr;
380
381 int MiddleIndex = (PointsBeginIndex + PointsEndIndex) / 2;
382 PointType MiddlePoint = EndPoints[MiddleIndex];
383
384 unsigned NewBucketStart = IntervalsSize;
385 unsigned NewBucketSize = 0;
386 int ReferencesRightIndex = ReferencesSize;
387
388 IntervalNode *Root =
389 new (NodeAllocator) IntervalNode(MiddlePoint, NewBucketStart);
390
391 // A quicksort implementation where all the intervals that overlap
392 // with the pivot are put into the "bucket", and "References" is the
393 // partition space where we recursively sort the remaining intervals.
394 for (int Index = ReferencesBeginIndex; Index < ReferencesRightIndex;) {
395
396 // Current interval contains the middle point.
397 if (References[Index]->contains(MiddlePoint)) {
398 IntervalsLeft[IntervalsSize] = References[Index];
399 IntervalsRight[IntervalsSize] = References[Index];
400 ++IntervalsSize;
401 Root->BucketIntervalsSize = ++NewBucketSize;
402
403 if (Index < --ReferencesRightIndex)
404 std::swap(References[Index], References[ReferencesRightIndex]);
405 if (ReferencesRightIndex < --ReferencesSize)
406 std::swap(References[ReferencesRightIndex],
407 References[ReferencesSize]);
408 continue;
409 }
410
411 if (References[Index]->left() > MiddlePoint) {
412 if (Index < --ReferencesRightIndex)
413 std::swap(References[Index], References[ReferencesRightIndex]);
414 continue;
415 }
416 ++Index;
417 }
418
419 // Sort intervals on the left and right of the middle point.
420 if (NewBucketSize > 1) {
421 // Sort the intervals in ascending order by their beginning point.
422 std::stable_sort(IntervalsLeft.begin() + NewBucketStart,
423 IntervalsLeft.begin() + NewBucketStart + NewBucketSize,
424 [](const DataType *LHS, const DataType *RHS) {
425 return LHS->left() < RHS->left();
426 });
427 // Sort the intervals in descending order by their ending point.
428 std::stable_sort(IntervalsRight.begin() + NewBucketStart,
429 IntervalsRight.begin() + NewBucketStart + NewBucketSize,
430 [](const DataType *LHS, const DataType *RHS) {
431 return LHS->right() > RHS->right();
432 });
433 }
434
435 if (PointsBeginIndex <= MiddleIndex - 1) {
436 Root->Left = createTree(IntervalsSize, PointsBeginIndex, MiddleIndex - 1,
437 ReferencesBeginIndex, ReferencesRightIndex);
438 }
439
440 if (MiddleIndex + 1 <= PointsEndIndex) {
441 Root->Right = createTree(IntervalsSize, MiddleIndex + 1, PointsEndIndex,
442 ReferencesRightIndex, ReferencesSize);
443 }
444
445 return Root;
446 }
447
448public:
449 class find_iterator {
450 public:
451 using iterator_category = std::forward_iterator_tag;
454 using pointer = DataType *;
456
457 private:
458 const IntervalReferences *AscendingBuckets = nullptr;
459 const IntervalReferences *DescendingBuckets = nullptr;
460
461 // Current node and index while traversing the intervals that contain
462 // the reference point.
463 IntervalNode *Node = nullptr;
464 PointType Point = {};
465 unsigned Index = 0;
466
467 // For the current node, check if we have intervals that contain the
468 // reference point. We return when the node does have intervals that
469 // contain such point. Otherwise we keep descending on that branch.
470 void initNode() {
471 Index = 0;
472 while (Node) {
473 // Return if the reference point is the same as the middle point or
474 // the current node doesn't have any intervals at all.
475 if (Point == Node->middle()) {
476 if (Node->size() == 0) {
477 // No intervals that contain the reference point.
478 Node = nullptr;
479 }
480 return;
481 }
482
483 if (Point < Node->middle()) {
484 // The reference point can be at the left or right of the middle
485 // point. Return if the current node has intervals that contain the
486 // reference point; otherwise descend on the respective branch.
487 if (Node->size() && (*AscendingBuckets)[Node->start()]->left(Point)) {
488 return;
489 }
490 Node = Node->Left;
491 } else {
492 if (Node->size() &&
493 (*DescendingBuckets)[Node->start()]->right(Point)) {
494 return;
495 }
496 Node = Node->Right;
497 }
498 }
499 }
500
501 // Given the current node (which was initialized by initNode), move to
502 // the next interval in the list of intervals that contain the reference
503 // point. Otherwise move to the next node, as the intervals contained
504 // in that node, can contain the reference point.
505 void nextInterval() {
506 // If there are available intervals that contain the reference point,
507 // traverse them; otherwise move to the left or right node, depending
508 // on the middle point value.
509 if (++Index < Node->size()) {
510 if (Node->middle() == Point)
511 return;
512 if (Point < Node->middle()) {
513 // Reference point is on the left.
514 if (!(*AscendingBuckets)[Node->start() + Index]->left(Point)) {
515 // The intervals don't contain the reference point. Move to the
516 // next node, preserving the descending order.
517 Node = Node->Left;
518 initNode();
519 }
520 } else {
521 // Reference point is on the right.
522 if (!(*DescendingBuckets)[Node->start() + Index]->right(Point)) {
523 // The intervals don't contain the reference point. Move to the
524 // next node, preserving the ascending order.
525 Node = Node->Right;
526 initNode();
527 }
528 }
529 } else {
530 // We have traversed all the intervals in the current node.
531 if (Point == Node->middle()) {
532 Node = nullptr;
533 Index = 0;
534 return;
535 }
536 // Select a branch based on the middle point.
537 Node = Point < Node->middle() ? Node->Left : Node->Right;
538 initNode();
539 }
540 }
541
542 find_iterator() = default;
543 explicit find_iterator(const IntervalReferences *Left,
544 const IntervalReferences *Right, IntervalNode *Node,
545 PointType Point)
546 : AscendingBuckets(Left), DescendingBuckets(Right), Node(Node),
547 Point(Point), Index(0) {
548 initNode();
549 }
550
551 const DataType *current() const {
552 return (Point <= Node->middle())
553 ? (*AscendingBuckets)[Node->start() + Index]
554 : (*DescendingBuckets)[Node->start() + Index];
555 }
556
557 public:
558 find_iterator &operator++() {
559 nextInterval();
560 return *this;
561 }
562
563 find_iterator operator++(int) {
564 find_iterator Iter(*this);
565 nextInterval();
566 return Iter;
567 }
568
569 /// Dereference operators.
570 const DataType *operator->() const { return current(); }
571 const DataType &operator*() const { return *(current()); }
572
573 /// Comparison operators.
574 friend bool operator==(const find_iterator &LHS, const find_iterator &RHS) {
575 return (!LHS.Node && !RHS.Node && !LHS.Index && !RHS.Index) ||
576 (LHS.Point == RHS.Point && LHS.Node == RHS.Node &&
577 LHS.Index == RHS.Index);
578 }
579 friend bool operator!=(const find_iterator &LHS, const find_iterator &RHS) {
580 return !(LHS == RHS);
581 }
582
584 };
585
586private:
587 find_iterator End;
588
589public:
590 explicit IntervalTree(Allocator &NodeAllocator)
591 : NodeAllocator(NodeAllocator) {}
593
594 /// Return true when no intervals are mapped.
595 bool empty() const { return Root == nullptr; }
596
597 /// Remove all entries.
598 void clear() {
599 deleteTree(Root);
600 Root = nullptr;
601 Intervals.clear();
602 IntervalsLeft.clear();
603 IntervalsRight.clear();
604 EndPoints.clear();
605 }
606
607 /// Add a mapping of [Left;Right] to \a Value.
609 assert(empty() && "Invalid insertion. Interval tree already constructed.");
610 Intervals.emplace_back(Left, Right, Value);
611 }
612
613 /// Return all the intervals in their natural tree location, that
614 /// contain the given point.
616 assert(!empty() && "Interval tree it is not constructed.");
617 IntervalReferences IntervalSet;
618 for (find_iterator Iter = find(Point), E = find_end(); Iter != E; ++Iter)
619 IntervalSet.push_back(const_cast<DataType *>(&(*Iter)));
620 return IntervalSet;
621 }
622
623 /// Sort the given intervals using the following sort options:
624 /// Ascending: return the intervals with the smallest at the front.
625 /// Descending: return the intervals with the biggest at the front.
626 static void sortIntervals(IntervalReferences &IntervalSet, Sorting Sort) {
627 std::stable_sort(IntervalSet.begin(), IntervalSet.end(),
628 [Sort](const DataType *RHS, const DataType *LHS) {
629 return Sort == Sorting::Ascending
630 ? (LHS->right() - LHS->left()) >
631 (RHS->right() - RHS->left())
632 : (LHS->right() - LHS->left()) <
633 (RHS->right() - RHS->left());
634 });
635 }
636
637 /// Print the interval tree.
638 /// When \a HexFormat is true, the interval tree interval ranges and
639 /// associated values are printed in hexadecimal format.
640 void print(raw_ostream &OS, bool HexFormat = true) {
641 printTree(OS, 0, Root, HexFormat);
642 }
643
644 /// Create the interval tree.
645 void create() {
646 assert(empty() && "Interval tree already constructed.");
647 // Sorted vector of unique end points values of all the intervals.
648 // Records references to the collected intervals.
650 for (const DataType &Data : Intervals) {
651 Points.push_back(Data.left());
652 Points.push_back(Data.right());
653 References.push_back(std::addressof(Data));
654 }
655 std::stable_sort(Points.begin(), Points.end());
656 auto Last = llvm::unique(Points);
657 Points.erase(Last, Points.end());
658
659 EndPoints.assign(Points.begin(), Points.end());
660
661 IntervalsLeft.resize(Intervals.size());
662 IntervalsRight.resize(Intervals.size());
663
664 // Given a set of n intervals, construct a data structure so that
665 // we can efficiently retrieve all intervals overlapping another
666 // interval or point.
667 unsigned IntervalsSize = 0;
668 Root =
669 createTree(IntervalsSize, /*PointsBeginIndex=*/0, EndPoints.size() - 1,
670 /*ReferencesBeginIndex=*/0, References.size());
671
672 // Save to clear this storage, as it used only to sort the intervals.
673 References.clear();
674 }
675
676 /// Iterator to start a find operation; it returns find_end() if the
677 /// tree has not been built.
678 /// There is no support to iterate over all the elements of the tree.
679 find_iterator find(PointType Point) const {
680 return empty()
681 ? find_end()
682 : find_iterator(&IntervalsLeft, &IntervalsRight, Root, Point);
683 }
684
685 /// Iterator to end find operation.
686 find_iterator find_end() const { return End; }
687};
688
689} // namespace llvm
690
691#endif // LLVM_ADT_INTERVALTREE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Basic Register Allocator
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file defines the SmallVector class.
Value * RHS
Value * LHS
An interval data composed by a Left and Right points and an associated Value.
bool right(const PointType &Point) const
Return true if Point is inside the right bound of closed interval [Left;Right].
virtual ~IntervalData()=default
IntervalData(PointType Left, PointType Right, ValueType Value)
bool left(const PointType &Point) const
Return true if Point is inside the left bound of closed interval [Left;Right].
PointType right() const
bool contains(const PointType &Point) const
Return true when Point is contained in interval [Left;Right].
ValueType value() const
PointType left() const
std::forward_iterator_tag iterator_category
friend bool operator!=(const find_iterator &LHS, const find_iterator &RHS)
friend bool operator==(const find_iterator &LHS, const find_iterator &RHS)
Comparison operators.
const DataType * operator->() const
Dereference operators.
const DataType & operator*() const
SmallVector< const DataType *, 4 > IntervalReferences
BumpPtrAllocator Allocator
void clear()
Remove all entries.
bool empty() const
Return true when no intervals are mapped.
void print(raw_ostream &OS, bool HexFormat=true)
Print the interval tree.
void insert(PointType Left, PointType Right, ValueType Value)
Add a mapping of [Left;Right] to Value.
IntervalTree(Allocator &NodeAllocator)
find_iterator find_end() const
Iterator to end find operation.
static void sortIntervals(IntervalReferences &IntervalSet, Sorting Sort)
Sort the given intervals using the following sort options: Ascending: return the intervals with the s...
void create()
Create the interval tree.
find_iterator find(PointType Point) const
Iterator to start a find operation; it returns find_end() if the tree has not been built.
IntervalReferences getContaining(PointType Point) const
Return all the intervals in their natural tree location, that contain the given point.
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1657
std::bool_constant< std::is_fundamental< T >::value|| std::is_pointer< T >::value > ValueTypeIsValid
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2056
std::is_fundamental< T > PointTypeIsValid
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:118
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872