98 #ifndef LLVM_ADT_INTERVALMAP_H 99 #define LLVM_ADT_INTERVALMAP_H 136 template <
typename T>
163 template <
typename T>
188 namespace IntervalMapImpl {
219 template <
typename T1,
typename T2,
unsigned N>
222 enum { Capacity =
N };
232 template <
unsigned M>
234 unsigned j,
unsigned Count) {
235 assert(i + Count <= M &&
"Invalid source range");
236 assert(j + Count <=
N &&
"Invalid dest range");
237 for (
unsigned e = i + Count; i !=
e; ++i, ++j) {
238 first[j] = Other.
first[i];
239 second[j] = Other.
second[i];
247 void moveLeft(
unsigned i,
unsigned j,
unsigned Count) {
248 assert(j <= i &&
"Use moveRight shift elements right");
249 copy(*
this, i, j, Count);
256 void moveRight(
unsigned i,
unsigned j,
unsigned Count) {
257 assert(i <= j &&
"Use moveLeft shift elements left");
258 assert(j + Count <=
N &&
"Invalid range");
260 first[j + Count] = first[i + Count];
261 second[j + Count] = second[i + Count];
270 moveLeft(j, i, Size - j);
284 moveRight(i, i + 1, Size - i);
294 Sib.
copy(*
this, 0, SSize, Count);
295 erase(0, Count, Size);
306 Sib.
copy(*
this, Size-Count, 0, Count);
319 unsigned Count = std::min(std::min(
unsigned(Add), SSize),
N - Size);
324 unsigned Count = std::min(std::min(
unsigned(-Add), Size),
N - SSize);
325 transferToLeftSib(Size, Sib, SSize, Count);
336 template <
typename NodeT>
338 unsigned CurSize[],
const unsigned NewSize[]) {
340 for (
int n = Nodes - 1; n; --n) {
341 if (CurSize[n] == NewSize[n])
343 for (
int m = n - 1; m != -1; --m) {
344 int d = Node[n]->adjustFromLeftSib(CurSize[n], *Node[m], CurSize[m],
345 NewSize[n] - CurSize[n]);
349 if (CurSize[n] >= NewSize[n])
358 for (
unsigned n = 0; n != Nodes - 1; ++n) {
359 if (CurSize[n] == NewSize[n])
361 for (
unsigned m = n + 1; m != Nodes; ++m) {
362 int d = Node[m]->adjustFromLeftSib(CurSize[m], *Node[n], CurSize[n],
363 CurSize[n] - NewSize[n]);
367 if (CurSize[n] >= NewSize[n])
373 for (
unsigned n = 0; n != Nodes; n++)
374 assert(CurSize[n] == NewSize[n] &&
"Insufficient element shuffle");
412 const unsigned *CurSize,
unsigned NewSize[],
435 template <
typename KeyT,
typename ValT>
444 static_cast<unsigned>(2*
sizeof(KeyT)+
sizeof(
ValT)),
446 LeafSize = DesiredLeafSize > MinLeafSize ? DesiredLeafSize : MinLeafSize
457 BranchSize = AllocBytes /
458 static_cast<unsigned>(
sizeof(KeyT) +
sizeof(
void*))
491 struct CacheAlignedPointerTraits {
492 static inline void *getAsVoidPointer(
void *
P) {
return P; }
493 static inline void *getFromVoidPointer(
void *P) {
return P; }
506 template <
typename NodeT>
507 NodeRef(NodeT *p,
unsigned n) : pip(p, n - 1) {
508 assert(n <= NodeT::Capacity &&
"Size too big for node");
525 template <
typename NodeT>
527 return *
reinterpret_cast<NodeT*
>(pip.
getPointer());
562 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
565 const KeyT &
start(
unsigned i)
const {
return this->
first[i].first; }
566 const KeyT &
stop(
unsigned i)
const {
return this->
first[i].second; }
570 KeyT &
stop(
unsigned i) {
return this->
first[i].second; }
580 assert(i <= Size && Size <=
N &&
"Bad indices");
581 assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
582 "Index is past the needed point");
583 while (i != Size && Traits::stopLess(stop(i), x)) ++i;
596 assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
597 "Index is past the needed point");
598 while (Traits::stopLess(stop(i), x)) ++i;
599 assert(i <
N &&
"Unsafe intervals");
609 unsigned i = safeFind(0, x);
610 return Traits::startLess(x, start(i)) ?
NotFound : value(i);
613 unsigned insertFrom(
unsigned &Pos,
unsigned Size, KeyT a, KeyT b, ValT y);
625 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
629 assert(i <= Size && Size <=
N &&
"Invalid index");
630 assert(!Traits::stopLess(b, a) &&
"Invalid interval");
633 assert((i == 0 || Traits::stopLess(stop(i - 1), a)));
634 assert((i == Size || !Traits::stopLess(stop(i), a)));
635 assert((i == Size || Traits::stopLess(b, start(i))) &&
"Overlapping insert");
638 if (i && value(i - 1) == y && Traits::adjacent(stop(i - 1), a)) {
641 if (i != Size && value(i) == y && Traits::adjacent(b, start(i))) {
642 stop(i - 1) = stop(i);
643 this->erase(i, Size);
663 if (value(i) == y && Traits::adjacent(b, start(i))) {
673 this->shift(i, Size);
699 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
702 const KeyT &
stop(
unsigned i)
const {
return this->
second[i]; }
715 assert(i <= Size && Size <=
N &&
"Bad indices");
716 assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
717 "Index to findFrom is past the needed point");
718 while (i != Size && Traits::stopLess(stop(i), x)) ++i;
730 assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
731 "Index is past the needed point");
732 while (Traits::stopLess(stop(i), x)) ++i;
733 assert(i <
N &&
"Unsafe intervals");
741 return subtree(safeFind(0, x));
750 assert(Size <
N &&
"branch node overflow");
751 assert(i <= Size &&
"Bad insert position");
752 this->shift(i, Size);
779 : node(Node),
size(Size), offset(Offset) {}
781 Entry(
NodeRef Node,
unsigned Offset)
784 NodeRef &subtree(
unsigned i)
const {
785 return reinterpret_cast<NodeRef*
>(node)[i];
794 template <
typename NodeT> NodeT &
node(
unsigned Level)
const {
795 return *
reinterpret_cast<NodeT*
>(path[
Level].node);
802 template <
typename NodeT> NodeT &
leaf()
const {
803 return *
reinterpret_cast<NodeT*
>(path.
back().node);
822 return path[
Level].subtree(path[Level].offset);
828 path[
Level] = Entry(subtree(Level - 1), offset(Level));
850 subtree(Level - 1).setSize(Size);
859 path.
push_back(Entry(Node, Size, Offset));
877 void moveLeft(
unsigned Level);
882 while (height() < Height)
883 push(subtree(height()), 0);
889 NodeRef getRightSibling(
unsigned Level)
const;
894 void moveRight(
unsigned Level);
898 for (
unsigned i = 0,
e = path.
size(); i !=
e; ++i)
899 if (path[i].offset != 0)
920 ++path[
Level].offset;
930 template <
typename KeyT,
typename ValT,
944 DesiredRootBranchCap = (
sizeof(
RootLeaf) -
sizeof(KeyT)) /
946 RootBranchCap = DesiredRootBranchCap ? DesiredRootBranchCap : 1
953 struct RootBranchData {
966 alignas(
RootLeaf)
alignas(RootBranchData)
982 template <
typename T>
988 assert(!branched() &&
"Cannot acces leaf data in branched root");
989 return dataAs<RootLeaf>();
992 assert(!branched() &&
"Cannot acces leaf data in branched root");
993 return dataAs<RootLeaf>();
996 RootBranchData &rootBranchData()
const {
997 assert(branched() &&
"Cannot access branch data in non-branched root");
998 return dataAs<RootBranchData>();
1000 RootBranchData &rootBranchData() {
1001 assert(branched() &&
"Cannot access branch data in non-branched root");
1002 return dataAs<RootBranchData>();
1005 const RootBranch &rootBranch()
const {
return rootBranchData().node; }
1006 RootBranch &rootBranch() {
return rootBranchData().node; }
1007 KeyT rootBranchStart()
const {
return rootBranchData().start; }
1008 KeyT &rootBranchStart() {
return rootBranchData().start; }
1010 template <
typename NodeT> NodeT *newNode() {
1011 return new(allocator.template Allocate<NodeT>()) NodeT();
1014 template <
typename NodeT>
void deleteNode(NodeT *
P) {
1016 allocator.Deallocate(P);
1019 IdxPair branchRoot(
unsigned Position);
1020 IdxPair splitRoot(
unsigned Position);
1022 void switchRootToBranch() {
1023 rootLeaf().~RootLeaf();
1025 new (&rootBranchData()) RootBranchData();
1028 void switchRootToLeaf() {
1029 rootBranchData().~RootBranchData();
1034 bool branched()
const {
return height > 0; }
1036 ValT treeSafeLookup(KeyT x, ValT
NotFound)
const;
1044 "Insufficient alignment");
1050 rootLeaf().~RootLeaf();
1055 return rootSize == 0;
1060 assert(!
empty() &&
"Empty IntervalMap has no start");
1061 return !branched() ? rootLeaf().start(0) : rootBranchStart();
1066 assert(!
empty() &&
"Empty IntervalMap has no stop");
1067 return !branched() ? rootLeaf().stop(rootSize - 1) :
1068 rootBranch().stop(rootSize - 1);
1072 ValT
lookup(KeyT x, ValT NotFound = ValT())
const {
1073 if (
empty() || Traits::startLess(x, start()) || Traits::stopLess(stop(), x))
1075 return branched() ? treeSafeLookup(x, NotFound) :
1076 rootLeaf().safeLookup(x, NotFound);
1083 if (branched() || rootSize == RootLeaf::Capacity)
1084 return find(a).insert(a, b, y);
1087 unsigned p = rootLeaf().findFrom(0, rootSize, a);
1088 rootSize = rootLeaf().insertFrom(p, rootSize, a, b, y);
1097 friend class iterator;
1140 assert(Traits::nonEmpty(a, b));
1147 return !Traits::stopLess(b, I.start());
1153 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1156 assert(branched() &&
"treeLookup assumes a branched root");
1159 for (
unsigned h = height-1; h; --h)
1161 return NR.
get<
Leaf>().safeLookup(x, NotFound);
1166 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1169 using namespace IntervalMapImpl;
1171 const unsigned Nodes = RootLeaf::Capacity / Leaf::Capacity + 1;
1174 unsigned size[Nodes];
1175 IdxPair NewOffset(0, Position);
1181 NewOffset =
distribute(Nodes, rootSize, Leaf::Capacity,
nullptr, size,
1186 NodeRef node[Nodes];
1187 for (
unsigned n = 0; n != Nodes; ++n) {
1188 Leaf *L = newNode<Leaf>();
1189 L->
copy(rootLeaf(), pos, 0, size[n]);
1190 node[n] = NodeRef(L, size[n]);
1195 switchRootToBranch();
1196 for (
unsigned n = 0; n != Nodes; ++n) {
1197 rootBranch().stop(n) = node[n].template get<Leaf>().stop(size[n]-1);
1198 rootBranch().subtree(n) = node[n];
1200 rootBranchStart() = node[0].template get<Leaf>().start(0);
1207 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1210 using namespace IntervalMapImpl;
1212 const unsigned Nodes = RootBranch::Capacity / Branch::Capacity + 1;
1215 unsigned Size[Nodes];
1216 IdxPair NewOffset(0, Position);
1222 NewOffset =
distribute(Nodes, rootSize, Leaf::Capacity,
nullptr, Size,
1227 NodeRef Node[Nodes];
1228 for (
unsigned n = 0; n != Nodes; ++n) {
1229 Branch *
B = newNode<Branch>();
1230 B->
copy(rootBranch(), Pos, 0, Size[n]);
1231 Node[n] = NodeRef(B, Size[n]);
1235 for (
unsigned n = 0; n != Nodes; ++n) {
1236 rootBranch().stop(n) = Node[n].template get<Branch>().stop(Size[n]-1);
1237 rootBranch().subtree(n) = Node[n];
1245 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1253 for (
unsigned i = 0; i != rootSize; ++i)
1254 Refs.
push_back(rootBranch().subtree(i));
1257 for (
unsigned h = height - 1; h; --h) {
1258 for (
unsigned i = 0,
e = Refs.
size(); i !=
e; ++i) {
1259 for (
unsigned j = 0, s = Refs[i].
size(); j != s; ++j)
1261 (this->*f)(Refs[i], h);
1264 Refs.
swap(NextRefs);
1268 for (
unsigned i = 0,
e = Refs.
size(); i !=
e; ++i)
1269 (this->*f)(Refs[i], 0);
1272 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1278 deleteNode(&Node.
get<
Leaf>());
1281 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1285 visitNodes(&IntervalMap::deleteNode);
1295 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1297 public std::iterator<std::bidirectional_iterator_tag, ValT> {
1313 assert(map &&
"Invalid iterator");
1314 return map->branched();
1324 void pathFillFind(KeyT x);
1325 void treeFind(KeyT x);
1326 void treeAdvanceTo(KeyT x);
1330 assert(valid() &&
"Cannot access invalid iterator");
1337 assert(valid() &&
"Cannot access invalid iterator");
1344 assert(valid() &&
"Cannot access invalid iterator");
1364 const KeyT &
start()
const {
return unsafeStart(); }
1367 const KeyT &
stop()
const {
return unsafeStop(); }
1370 const ValT &
value()
const {
return unsafeValue(); }
1375 assert(map == RHS.
map &&
"Cannot compare iterators from different maps");
1377 return !RHS.
valid();
1380 return &path.template leaf<Leaf>() == &RHS.
path.template leaf<Leaf>();
1396 setRoot(map->rootSize);
1401 assert(valid() &&
"Cannot increment end()");
1416 if (path.
leafOffset() && (valid() || !branched()))
1436 setRoot(map->rootLeaf().
findFrom(0, map->rootSize, x));
1455 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1459 for (
unsigned i = map->height - path.height() - 1; i; --i) {
1460 unsigned p = NR.
get<
Branch>().safeFind(0, x);
1469 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1472 setRoot(map->rootBranch().findFrom(0, map->rootSize, x));
1479 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1483 if (!Traits::stopLess(path.leaf<
Leaf>().
stop(path.leafSize() - 1), x)) {
1484 path.leafOffset() = path.leaf<
Leaf>().safeFind(path.leafOffset(), x);
1492 if (path.height()) {
1493 for (
unsigned l = path.height() - 1; l; --l) {
1494 if (!Traits::stopLess(path.node<
Branch>(l).
stop(path.offset(l)), x)) {
1496 path.offset(l + 1) =
1497 path.node<
Branch>(l + 1).safeFind(path.offset(l + 1), x);
1498 return pathFillFind(x);
1503 if (!Traits::stopLess(map->rootBranch().stop(path.offset(0)), x)) {
1504 path.offset(1) = path.node<
Branch>(1).safeFind(path.offset(1), x);
1505 return pathFillFind(x);
1510 setRoot(map->rootBranch().findFrom(path.offset(0), map->rootSize, x));
1519 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1527 void setNodeStop(
unsigned Level, KeyT Stop);
1529 template <
typename NodeT>
bool overflow(
unsigned Level);
1530 void treeInsert(KeyT a, KeyT b, ValT y);
1531 void eraseNode(
unsigned Level);
1532 void treeErase(
bool UpdateRoot =
true);
1533 bool canCoalesceLeft(KeyT Start, ValT x);
1534 bool canCoalesceRight(KeyT Stop, ValT x);
1543 void setStart(KeyT a);
1548 void setStop(KeyT b);
1553 void setValue(ValT x);
1566 this->unsafeStop() = b;
1568 if (this->path.atLastEntry(this->path.height()))
1569 setNodeStop(this->path.height(), b);
1578 void insert(KeyT a, KeyT b, ValT y);
1584 const_iterator::operator++();
1595 const_iterator::operator--();
1611 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1614 using namespace IntervalMapImpl;
1615 Path &P = this->path;
1616 if (!this->branched()) {
1617 unsigned i = P.leafOffset();
1619 return i && Node.
value(i-1) == Value &&
1620 Traits::adjacent(Node.
stop(i-1), Start);
1623 if (
unsigned i = P.leafOffset()) {
1625 return Node.
value(i-1) == Value && Traits::adjacent(Node.
stop(i-1), Start);
1626 }
else if (NodeRef NR = P.getLeftSibling(P.height())) {
1627 unsigned i = NR.size() - 1;
1629 return Node.
value(i) == Value && Traits::adjacent(Node.
stop(i), Start);
1639 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1642 using namespace IntervalMapImpl;
1643 Path &P = this->path;
1644 unsigned i = P.leafOffset() + 1;
1645 if (!this->branched()) {
1646 if (i >= P.leafSize())
1649 return Node.
value(i) == Value && Traits::adjacent(Stop, Node.
start(i));
1652 if (i < P.leafSize()) {
1654 return Node.
value(i) == Value && Traits::adjacent(Stop, Node.
start(i));
1655 }
else if (NodeRef NR = P.getRightSibling(P.height())) {
1657 return Node.
value(0) == Value && Traits::adjacent(Stop, Node.
start(0));
1663 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1680 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1683 assert(Traits::nonEmpty(a, this->stop()) &&
"Cannot move start beyond stop");
1684 KeyT &CurStart = this->unsafeStart();
1685 if (!Traits::startLess(a, CurStart) || !canCoalesceLeft(a, this->value())) {
1693 setStartUnchecked(a);
1696 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1699 assert(Traits::nonEmpty(this->start(), b) &&
"Cannot move stop beyond start");
1700 if (Traits::startLess(b, this->stop()) ||
1701 !canCoalesceRight(b, this->value())) {
1702 setStopUnchecked(b);
1706 KeyT a = this->start();
1708 setStartUnchecked(a);
1711 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1714 setValueUnchecked(x);
1715 if (canCoalesceRight(this->stop(), x)) {
1716 KeyT a = this->start();
1718 setStartUnchecked(a);
1720 if (canCoalesceLeft(this->start(), x)) {
1722 KeyT a = this->start();
1724 setStartUnchecked(a);
1734 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1737 assert(Level &&
"Cannot insert next to the root");
1738 bool SplitRoot =
false;
1744 if (IM.rootSize < RootBranch::Capacity) {
1745 IM.rootBranch().
insert(P.
offset(0), IM.rootSize, Node, Stop);
1764 if (P.
size(Level) == Branch::Capacity) {
1766 assert(!SplitRoot &&
"Cannot overflow after splitting the root");
1767 SplitRoot = overflow<Branch>(
Level);
1773 setNodeStop(Level, Stop);
1779 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1782 if (this->branched())
1783 return treeInsert(a, b, y);
1791 if (Size <= RootLeaf::Capacity) {
1792 P.
setSize(0, IM.rootSize = Size);
1801 treeInsert(a, b, y);
1804 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1807 using namespace IntervalMapImpl;
1808 Path &P = this->path;
1811 P.legalizeForInsert(this->map->height);
1814 if (P.leafOffset() == 0 && Traits::startLess(a, P.leaf<
Leaf>().
start(0))) {
1816 if (NodeRef Sib = P.getLeftSibling(P.height())) {
1818 unsigned SibOfs = Sib.size() - 1;
1819 if (SibLeaf.
value(SibOfs) == y &&
1820 Traits::adjacent(SibLeaf.
stop(SibOfs), a)) {
1828 if (Traits::stopLess(b, CurLeaf.
start(0)) &&
1829 (y != CurLeaf.
value(0) || !Traits::adjacent(b, CurLeaf.
start(0)))) {
1831 setNodeStop(P.height(), SibLeaf.
stop(SibOfs) = b);
1836 a = SibLeaf.
start(SibOfs);
1842 this->map->rootBranchStart() = a;
1847 unsigned Size = P.leafSize();
1848 bool Grow = P.leafOffset() ==
Size;
1849 Size = P.leaf<
Leaf>().insertFrom(P.leafOffset(),
Size, a, b, y);
1852 if (Size > Leaf::Capacity) {
1853 overflow<Leaf>(P.height());
1854 Grow = P.leafOffset() == P.leafSize();
1855 Size = P.leaf<
Leaf>().insertFrom(P.leafOffset(), P.leafSize(), a, b, y);
1856 assert(Size <= Leaf::Capacity &&
"overflow() didn't make room");
1860 P.setSize(P.height(),
Size);
1864 setNodeStop(P.height(), b);
1868 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1874 if (this->branched())
1881 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1890 IM.deleteNode(&Node);
1891 eraseNode(IM.height);
1893 if (UpdateRoot && IM.branched() && P.
valid() && P.
atBegin())
1894 IM.rootBranchStart() = P.
leaf<
Leaf>().start(0);
1900 unsigned NewSize = P.
leafSize() - 1;
1901 P.
setSize(IM.height, NewSize);
1904 setNodeStop(IM.height, Node.
stop(NewSize - 1));
1906 }
else if (UpdateRoot && P.
atBegin())
1907 IM.rootBranchStart() = P.
leaf<
Leaf>().start(0);
1914 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1917 assert(Level &&
"Cannot erase root node");
1926 IM.switchRootToLeaf();
1933 if (P.
size(Level) == 1) {
1935 IM.deleteNode(&Parent);
1940 unsigned NewSize = P.
size(Level) - 1;
1943 if (P.
offset(Level) == NewSize) {
1944 setNodeStop(Level, Parent.
stop(NewSize - 1));
1962 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1963 template <
typename NodeT>
1966 using namespace IntervalMapImpl;
1967 Path &P = this->path;
1968 unsigned CurSize[4];
1971 unsigned Elements = 0;
1972 unsigned Offset = P.offset(Level);
1975 NodeRef LeftSib = P.getLeftSibling(Level);
1977 Offset += Elements = CurSize[Nodes] = LeftSib.size();
1978 Node[Nodes++] = &LeftSib.get<NodeT>();
1982 Elements += CurSize[Nodes] = P.size(Level);
1983 Node[Nodes++] = &P.node<NodeT>(
Level);
1986 NodeRef RightSib = P.getRightSibling(Level);
1988 Elements += CurSize[Nodes] = RightSib.size();
1989 Node[Nodes++] = &RightSib.get<NodeT>();
1993 unsigned NewNode = 0;
1994 if (Elements + 1 > Nodes * NodeT::Capacity) {
1996 NewNode = Nodes == 1 ? 1 : Nodes - 1;
1997 CurSize[Nodes] = CurSize[NewNode];
1998 Node[Nodes] = Node[NewNode];
1999 CurSize[NewNode] = 0;
2000 Node[NewNode] = this->map->template newNode<NodeT>();
2005 unsigned NewSize[4];
2006 IdxPair NewOffset =
distribute(Nodes, Elements, NodeT::Capacity,
2007 CurSize, NewSize, Offset,
true);
2015 bool SplitRoot =
false;
2018 KeyT Stop = Node[Pos]->stop(NewSize[Pos]-1);
2019 if (NewNode && Pos == NewNode) {
2020 SplitRoot = insertNode(Level, NodeRef(Node[Pos], NewSize[Pos]), Stop);
2023 P.setSize(Level, NewSize[Pos]);
2024 setNodeStop(Level, Stop);
2026 if (Pos + 1 == Nodes)
2033 while(Pos != NewOffset.first) {
2037 P.offset(Level) = NewOffset.second;
2057 template <
typename MapA,
typename MapB>
2059 using KeyType =
typename MapA::KeyType;
2060 using Traits =
typename MapA::KeyTraits;
2062 typename MapA::const_iterator posA;
2063 typename MapB::const_iterator posB;
2072 if (Traits::stopLess(posA.stop(), posB.start())) {
2074 posA.advanceTo(posB.start());
2075 if (!posA.valid() || !Traits::stopLess(posB.stop(), posA.start()))
2077 }
else if (Traits::stopLess(posB.stop(), posA.start())) {
2079 posB.advanceTo(posA.start());
2080 if (!posB.valid() || !Traits::stopLess(posA.stop(), posB.start()))
2088 posA.advanceTo(posB.start());
2089 if (!posA.valid() || !Traits::stopLess(posB.stop(), posA.start()))
2092 posB.advanceTo(posA.start());
2093 if (!posB.valid() || !Traits::stopLess(posA.stop(), posB.start()))
2102 posB(posA.valid() ? b.
find(posA.start()) : b.
end()) { advance(); }
2106 return posA.valid() && posB.valid();
2110 const typename MapA::const_iterator &
a()
const {
return posA; }
2113 const typename MapB::const_iterator &
b()
const {
return posB; }
2117 KeyType ak = a().start();
2118 KeyType bk = b().start();
2119 return Traits::startLess(ak, bk) ? bk : ak;
2124 KeyType ak = a().stop();
2125 KeyType bk = b().stop();
2126 return Traits::startLess(ak, bk) ? ak : bk;
2144 if (Traits::startLess(posB.stop(), posA.stop()))
2157 if (Traits::stopLess(posA.stop(), x))
2159 if (Traits::stopLess(posB.stop(), x))
2167 #endif // LLVM_ADT_INTERVALMAP_H unsigned safeFind(unsigned i, KeyT x) const
safeFind - Find an interval that is known to exist.
void setValueUnchecked(ValT x)
setValueUnchecked - Change the mapped value of the current interval without checking for coalescing...
bool valid() const
valid - Return true if path is at a valid node, not at end().
const_iterator end(StringRef path)
Get end iterator over path.
void setValue(ValT x)
setValue - Change the mapped value of the current interval.
void transferToLeftSib(unsigned Size, NodeBase &Sib, unsigned SSize, unsigned Count)
transferToLeftSib - Transfer elements to a left sibling node.
void setInt(IntType IntVal) LLVM_LVALUE_FUNCTION
void copy(const NodeBase< T1, T2, M > &Other, unsigned i, unsigned j, unsigned Count)
copy - Copy elements from another node.
void adjustSiblingSizes(NodeT *Node[], unsigned Nodes, unsigned CurSize[], const unsigned NewSize[])
IntervalMapImpl::adjustSiblingSizes - Move elements between sibling nodes.
const KeyT & stop(unsigned i) const
This class represents lattice values for constants.
PointerTy getPointer() const
const MapB::const_iterator & b() const
b - access the right hand side in the overlap.
void legalizeForInsert(unsigned Level)
legalizeForInsert - Prepare the path for an insertion at Level.
IntervalMapOverlaps - Iterate over the overlaps of mapped intervals in two IntervalMaps.
char buffer[sizeof(llvm::detail::SizerImpl< T, Ts... >)]
void push_back(const T &Elt)
NodeT & node(unsigned Level) const
IntervalMapOverlaps & operator++()
Preincrement - Move to the next overlap.
void skipA()
skipA - Move to the next overlap that doesn't involve a().
const_iterator(const IntervalMap &map)
void moveLeft(unsigned Level)
moveLeft - Move path to the left sibling at Level.
void pop()
pop - Remove the last path entry.
bool valid() const
valid - Return true if the current position is valid, false for end().
int adjustFromLeftSib(unsigned Size, NodeBase &Sib, unsigned SSize, int Add)
adjustFromLeftSib - Adjust the number if elements in this node by moving elements to or from a left s...
unsigned & offset(unsigned Level)
void setMap(const IntervalMap &m)
setMap - Change the map iterated over.
bool operator!=(const const_iterator &RHS) const
void fillLeft(unsigned Height)
fillLeft - Grow path to Height by taking leftmost branches.
static bool adjacent(const T &a, const T &b)
adjacent - Return true when the intervals [x;a) and [b;y) can coalesce.
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
const_iterator begin() const
bool atBegin() const
atBegin - Return true if path is at begin().
void setRoot(unsigned Offset)
static bool stopLess(const T &b, const T &x)
stopLess - Return true if x is not in [a;b].
const_iterator & operator++()
preincrement - move to the next interval.
ValT safeLookup(KeyT x, ValT NotFound) const
safeLookup - Lookup mapped value for a safe key.
unsigned safeFind(unsigned i, KeyT x) const
safeFind - Find a subtree that is known to exist.
bool operator==(const NodeRef &RHS) const
void advanceTo(KeyT x)
advanceTo - Move to the first interval with stop >= x, or end().
void * getOpaqueValue() const
KeyT & unsafeStart() const
unsafeStart - Writable access to start() for iterator.
RecyclingAllocator - This class wraps an Allocator, adding the functionality of recycling deleted obj...
void treeAdvanceTo(KeyT x)
treeAdvanceTo - Find position after the current one.
Position
Position to insert a new instruction relative to an existing instruction.
ValT lookup(KeyT x, ValT NotFound=ValT()) const
lookup - Return the mapped value at x or NotFound.
NodeT & get() const
get - Dereference as a NodeT reference.
KeyT start() const
start - Return the smallest mapped key in a non-empty map.
static bool startLess(const T &x, const T &a)
startLess - Return true if x is not in [a;b).
unsigned findFrom(unsigned i, unsigned Size, KeyT x) const
findFrom - Find the first subtree after i that may contain x.
unsigned height() const
height - Return the height of the tree corresponding to this path.
void erase()
erase - Erase the current interval.
bool operator==(const const_iterator &RHS) const
void clear()
clear - Remove all entries.
NodeRef & subtree(unsigned i) const
subtree - Access the i'th subtree reference in a branch node.
IntervalMap(Allocator &a)
KeyType stop() const
stop - End of the overlapping interval.
const_iterator find(KeyT x) const
find - Return an iterator pointing to the first interval ending at or after x, or end()...
void insert(unsigned i, unsigned Size, NodeRef Node, KeyT Stop)
insert - Insert a new (subtree, stop) pair.
KeyType start() const
start - Beginning of the overlapping interval.
void erase(unsigned i, unsigned j, unsigned Size)
erase - Erase elements [i;j).
void transferToRightSib(unsigned Size, NodeBase &Sib, unsigned SSize, unsigned Count)
transferToRightSib - Transfer elements to a right sibling node.
void setStartUnchecked(KeyT a)
setStartUnchecked - Move the start of the current interval without checking for coalescing or overlap...
bool valid() const
valid - Return true if iterator is at an overlap.
IdxPair distribute(unsigned Nodes, unsigned Elements, unsigned Capacity, const unsigned *CurSize, unsigned NewSize[], unsigned Position, bool Grow)
IntervalMapImpl::distribute - Compute a new distribution of node elements after an overflow or underf...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void replaceRoot(void *Root, unsigned Size, IdxPair Offsets)
replaceRoot - Replace the current root node with two new entries after the tree height has increased...
typename Sizer::Allocator Allocator
const KeyT & stop(unsigned i) const
void swap(SmallVectorImpl &RHS)
Sentinel value for when no action was found in the specified table.
const MapA::const_iterator & a() const
a - access the left hand side in the overlap.
void moveRight(unsigned i, unsigned j, unsigned Count)
moveRight - Move elements to the right.
const_iterator end() const
void find(KeyT x)
find - Move to the first interval with stop >= x, or end().
const KeyT & start() const
start - Return the beginning of the current interval.
NodeRef & subtree(unsigned Level) const
subtree - Get the subtree referenced from Level.
const_iterator & operator--()
predecrement - move to the previous interval.
void goToBegin()
goToBegin - Move to the first interval in map.
KeyT stop() const
stop - Return the largest mapped key in a non-empty map.
void setStop(KeyT b)
setStop - Move the end of the current interval.
const KeyT & start(unsigned i) const
unsigned leafSize() const
bool atBegin() const
atBegin - Return true if the current position is the first map entry.
static bool startLess(const T &x, const T &a)
startLess - Return true if x is not in [a;b].
IntervalMapOverlaps(const MapA &a, const MapB &b)
IntervalMapOverlaps - Create an iterator for the overlaps of a and b.
const_iterator operator++(int)
postincrement - Dont do that!
static bool stopLess(const T &b, const T &x)
stopLess - Return true if x is not in [a;b).
void skipB()
skipB - Move to the next overlap that doesn't involve b().
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
void insert(KeyT a, KeyT b, ValT y)
insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
unsigned size(unsigned Level) const
const ValT & value(unsigned i) const
NodeRef safeLookup(KeyT x) const
safeLookup - Get the subtree containing x, Assuming that x is in range.
void moveLeft(unsigned i, unsigned j, unsigned Count)
moveLeft - Move elements to the left.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
std::pair< unsigned, unsigned > IdxPair
void goToEnd()
goToEnd - Move beyond the last interval in map.
void setStart(KeyT a)
setStart - Move the start of the current interval.
unsigned leafOffset() const
unsigned findFrom(unsigned i, unsigned Size, KeyT x) const
findFrom - Find the first interval after i that may contain x.
void advanceTo(KeyType x)
advanceTo - Move to the first overlapping interval with stopLess(x, stop()).
const ValT & value() const
value - Return the mapped value at the current interval.
bool operator!=(const NodeRef &RHS) const
bool empty() const
empty - Return true when no intervals are mapped.
void reset(unsigned Level)
reset - Reset cached information about node(Level) from subtree(Level -1).
void erase(unsigned i, unsigned Size)
erase - Erase element at i.
A suitably aligned and sized character array member which can hold elements of any type...
static void clear(coro::Shape &Shape)
void pathFillFind(KeyT x)
pathFillFind - Complete path by searching for x.
void push(NodeRef Node, unsigned Offset)
push - Add entry to path.
const KeyT & stop() const
stop - Return the end of the current interval.
void moveRight(unsigned Level)
moveRight - Move path to the left sibling at Level.
unsigned offset(unsigned Level) const
void insert(KeyT a, KeyT b, ValT y)
insert - Insert mapping [a;b] -> y before the current position.
static bool nonEmpty(const T &a, const T &b)
nonEmpty - Return true if [a;b] is non-empty.
KeyT & unsafeStop() const
unsafeStop - Writable access to stop() for iterator.
unsigned size() const
size - Return the number of elements in the referenced node.
LLVM_NODISCARD bool empty() const
void treeFind(KeyT x)
treeFind - Find in a branched tree.
IntervalMapImpl::Path path
unsigned insertFrom(unsigned &Pos, unsigned Size, KeyT a, KeyT b, ValT y)
insertFrom - Add mapping of [a;b] to y if possible, coalescing as much as possible.
NodeRef & subtree(unsigned i)
const_iterator operator--(int)
postdecrement - Dont do that!
const NodeRef & subtree(unsigned i) const
To bit_cast(const From &from) noexcept
bool atLastEntry(unsigned Level) const
atLastEntry - Return true if the path is at the last entry of the node at Level.
void setStopUnchecked(KeyT b)
setStopUnchecked - Move the end of the current interval without checking for coalescing or overlaps...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void setRoot(void *Node, unsigned Size, unsigned Offset)
setRoot - Clear the path and set a new root node.
void setSize(unsigned n)
setSize - Update the node size.
static bool adjacent(const T &a, const T &b)
adjacent - Return true when the intervals [x;a] and [b;y] can coalesce.
LLVM Value Representation.
void shift(unsigned i, unsigned Size)
shift - Shift elements [i;size) 1 position to the right.
const ValT & operator*() const
bool operator==(uint64_t V1, const APInt &V2)
static bool nonEmpty(const T &a, const T &b)
nonEmpty - Return true if [a;b) is non-empty.
OutputIt copy(R &&Range, OutputIt Out)
SlotIndex - An opaque wrapper around machine indexes.
void setSize(unsigned Level, unsigned Size)
setSize - Set the size of a node both in the path and in the tree.
NodeRef(NodeT *p, unsigned n)
NodeRef - Create a reference to the node p with n elements.
bool overlaps(KeyT a, KeyT b)
overlaps(a, b) - Return true if the intervals in this map overlap with the interval [a;b]...
ValT & unsafeValue() const
unsafeValue - Writable access to value() for iterator.