99 #ifndef LLVM_ADT_INTERVALMAP_H 100 #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);
778 Entry(
void *Node,
unsigned Size,
unsigned Offset)
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 {
981 template <
typename T>
992 assert(!branched() &&
"Cannot acces leaf data in branched root");
993 return dataAs<RootLeaf>();
996 assert(!branched() &&
"Cannot acces leaf data in branched root");
997 return dataAs<RootLeaf>();
1000 RootBranchData &rootBranchData()
const {
1001 assert(branched() &&
"Cannot access branch data in non-branched root");
1002 return dataAs<RootBranchData>();
1004 RootBranchData &rootBranchData() {
1005 assert(branched() &&
"Cannot access branch data in non-branched root");
1006 return dataAs<RootBranchData>();
1009 const RootBranch &rootBranch()
const {
return rootBranchData().node; }
1010 RootBranch &rootBranch() {
return rootBranchData().node; }
1011 KeyT rootBranchStart()
const {
return rootBranchData().start; }
1012 KeyT &rootBranchStart() {
return rootBranchData().start; }
1014 template <
typename NodeT> NodeT *newNode() {
1015 return new(allocator.template Allocate<NodeT>()) NodeT();
1018 template <
typename NodeT>
void deleteNode(NodeT *
P) {
1020 allocator.Deallocate(P);
1023 IdxPair branchRoot(
unsigned Position);
1024 IdxPair splitRoot(
unsigned Position);
1026 void switchRootToBranch() {
1027 rootLeaf().~RootLeaf();
1029 new (&rootBranchData()) RootBranchData();
1032 void switchRootToLeaf() {
1033 rootBranchData().~RootBranchData();
1038 bool branched()
const {
return height > 0; }
1040 ValT treeSafeLookup(KeyT x, ValT
NotFound)
const;
1048 "Insufficient alignment");
1054 rootLeaf().~RootLeaf();
1059 return rootSize == 0;
1064 assert(!empty() &&
"Empty IntervalMap has no start");
1065 return !branched() ? rootLeaf().start(0) : rootBranchStart();
1070 assert(!empty() &&
"Empty IntervalMap has no stop");
1071 return !branched() ? rootLeaf().stop(rootSize - 1) :
1072 rootBranch().stop(rootSize - 1);
1076 ValT
lookup(KeyT x, ValT NotFound = ValT())
const {
1077 if (empty() || Traits::startLess(x, start()) || Traits::stopLess(stop(), x))
1079 return branched() ? treeSafeLookup(x, NotFound) :
1080 rootLeaf().safeLookup(x, NotFound);
1087 if (branched() || rootSize == RootLeaf::Capacity)
1088 return find(a).insert(a, b, y);
1091 unsigned p = rootLeaf().findFrom(0, rootSize, a);
1092 rootSize = rootLeaf().insertFrom(p, rootSize, a, b, y);
1098 class const_iterator;
1100 friend class const_iterator;
1101 friend class iterator;
1104 const_iterator
I(*
this);
1116 const_iterator
I(*
this);
1129 const_iterator
find(KeyT x)
const {
1130 const_iterator
I(*
this);
1144 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1147 assert(branched() &&
"treeLookup assumes a branched root");
1150 for (
unsigned h = height-1; h; --h)
1152 return NR.
get<
Leaf>().safeLookup(x, NotFound);
1157 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1160 using namespace IntervalMapImpl;
1162 const unsigned Nodes = RootLeaf::Capacity / Leaf::Capacity + 1;
1165 unsigned size[Nodes];
1166 IdxPair NewOffset(0, Position);
1172 NewOffset =
distribute(Nodes, rootSize, Leaf::Capacity,
nullptr, size,
1177 NodeRef node[Nodes];
1178 for (
unsigned n = 0; n != Nodes; ++n) {
1179 Leaf *L = newNode<Leaf>();
1180 L->
copy(rootLeaf(), pos, 0, size[n]);
1181 node[n] = NodeRef(L, size[n]);
1186 switchRootToBranch();
1187 for (
unsigned n = 0; n != Nodes; ++n) {
1188 rootBranch().stop(n) = node[n].template get<Leaf>().stop(size[n]-1);
1189 rootBranch().subtree(n) = node[n];
1191 rootBranchStart() = node[0].template get<Leaf>().start(0);
1198 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1201 using namespace IntervalMapImpl;
1203 const unsigned Nodes = RootBranch::Capacity / Branch::Capacity + 1;
1206 unsigned Size[Nodes];
1207 IdxPair NewOffset(0, Position);
1213 NewOffset =
distribute(Nodes, rootSize, Leaf::Capacity,
nullptr, Size,
1218 NodeRef Node[Nodes];
1219 for (
unsigned n = 0; n != Nodes; ++n) {
1220 Branch *
B = newNode<Branch>();
1221 B->
copy(rootBranch(), Pos, 0, Size[n]);
1222 Node[n] = NodeRef(B, Size[n]);
1226 for (
unsigned n = 0; n != Nodes; ++n) {
1227 rootBranch().stop(n) = Node[n].template get<Branch>().stop(Size[n]-1);
1228 rootBranch().subtree(n) = Node[n];
1236 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1244 for (
unsigned i = 0; i != rootSize; ++i)
1245 Refs.
push_back(rootBranch().subtree(i));
1248 for (
unsigned h = height - 1; h; --h) {
1249 for (
unsigned i = 0, e = Refs.
size(); i != e; ++i) {
1250 for (
unsigned j = 0, s = Refs[i].
size(); j != s; ++j)
1252 (this->*f)(Refs[i], h);
1255 Refs.
swap(NextRefs);
1259 for (
unsigned i = 0, e = Refs.
size(); i != e; ++i)
1260 (this->*f)(Refs[i], 0);
1263 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1269 deleteNode(&Node.
get<
Leaf>());
1272 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1276 visitNodes(&IntervalMap::deleteNode);
1286 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1288 public std::iterator<std::bidirectional_iterator_tag, ValT> {
1304 assert(map &&
"Invalid iterator");
1305 return map->branched();
1315 void pathFillFind(KeyT x);
1316 void treeFind(KeyT x);
1317 void treeAdvanceTo(KeyT x);
1321 assert(valid() &&
"Cannot access invalid iterator");
1328 assert(valid() &&
"Cannot access invalid iterator");
1335 assert(valid() &&
"Cannot access invalid iterator");
1355 const KeyT &
start()
const {
return unsafeStart(); }
1358 const KeyT &
stop()
const {
return unsafeStop(); }
1361 const ValT &
value()
const {
return unsafeValue(); }
1366 assert(map == RHS.
map &&
"Cannot compare iterators from different maps");
1368 return !RHS.
valid();
1371 return &path.template leaf<Leaf>() == &RHS.
path.template leaf<Leaf>();
1387 setRoot(map->rootSize);
1392 assert(valid() &&
"Cannot increment end()");
1407 if (path.
leafOffset() && (valid() || !branched()))
1427 setRoot(map->rootLeaf().
findFrom(0, map->rootSize, x));
1446 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1450 for (
unsigned i = map->height - path.height() - 1; i; --i) {
1451 unsigned p = NR.
get<
Branch>().safeFind(0, x);
1460 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1463 setRoot(map->rootBranch().findFrom(0, map->rootSize, x));
1470 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1474 if (!Traits::stopLess(path.leaf<
Leaf>().
stop(path.leafSize() - 1), x)) {
1475 path.leafOffset() = path.leaf<
Leaf>().safeFind(path.leafOffset(), x);
1483 if (path.height()) {
1484 for (
unsigned l = path.height() - 1; l; --l) {
1485 if (!Traits::stopLess(path.node<
Branch>(l).
stop(path.offset(l)), x)) {
1487 path.offset(l + 1) =
1488 path.node<
Branch>(l + 1).safeFind(path.offset(l + 1), x);
1489 return pathFillFind(x);
1494 if (!Traits::stopLess(map->rootBranch().stop(path.offset(0)), x)) {
1495 path.offset(1) = path.node<
Branch>(1).safeFind(path.offset(1), x);
1496 return pathFillFind(x);
1501 setRoot(map->rootBranch().findFrom(path.offset(0), map->rootSize, x));
1510 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1518 void setNodeStop(
unsigned Level, KeyT Stop);
1520 template <
typename NodeT>
bool overflow(
unsigned Level);
1521 void treeInsert(KeyT a, KeyT b, ValT y);
1522 void eraseNode(
unsigned Level);
1523 void treeErase(
bool UpdateRoot =
true);
1524 bool canCoalesceLeft(KeyT Start, ValT x);
1525 bool canCoalesceRight(KeyT Stop, ValT x);
1534 void setStart(KeyT a);
1539 void setStop(KeyT b);
1544 void setValue(ValT x);
1557 this->unsafeStop() = b;
1559 if (this->path.atLastEntry(this->path.height()))
1560 setNodeStop(this->path.height(), b);
1569 void insert(KeyT a, KeyT b, ValT y);
1575 const_iterator::operator++();
1586 const_iterator::operator--();
1602 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1605 using namespace IntervalMapImpl;
1606 Path &P = this->path;
1607 if (!this->branched()) {
1608 unsigned i = P.leafOffset();
1610 return i && Node.
value(i-1) == Value &&
1611 Traits::adjacent(Node.
stop(i-1), Start);
1614 if (
unsigned i = P.leafOffset()) {
1616 return Node.
value(i-1) == Value && Traits::adjacent(Node.
stop(i-1), Start);
1617 }
else if (NodeRef NR = P.getLeftSibling(P.height())) {
1618 unsigned i = NR.size() - 1;
1620 return Node.
value(i) == Value && Traits::adjacent(Node.
stop(i), Start);
1630 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1633 using namespace IntervalMapImpl;
1634 Path &P = this->path;
1635 unsigned i = P.leafOffset() + 1;
1636 if (!this->branched()) {
1637 if (i >= P.leafSize())
1640 return Node.
value(i) == Value && Traits::adjacent(Stop, Node.
start(i));
1643 if (i < P.leafSize()) {
1645 return Node.
value(i) == Value && Traits::adjacent(Stop, Node.
start(i));
1646 }
else if (NodeRef NR = P.getRightSibling(P.height())) {
1648 return Node.
value(0) == Value && Traits::adjacent(Stop, Node.
start(0));
1654 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1671 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1674 assert(Traits::nonEmpty(a, this->stop()) &&
"Cannot move start beyond stop");
1675 KeyT &CurStart = this->unsafeStart();
1676 if (!Traits::startLess(a, CurStart) || !canCoalesceLeft(a, this->value())) {
1684 setStartUnchecked(a);
1687 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1690 assert(Traits::nonEmpty(this->start(), b) &&
"Cannot move stop beyond start");
1691 if (Traits::startLess(b, this->stop()) ||
1692 !canCoalesceRight(b, this->value())) {
1693 setStopUnchecked(b);
1697 KeyT a = this->start();
1699 setStartUnchecked(a);
1702 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1705 setValueUnchecked(x);
1706 if (canCoalesceRight(this->stop(), x)) {
1707 KeyT a = this->start();
1709 setStartUnchecked(a);
1711 if (canCoalesceLeft(this->start(), x)) {
1713 KeyT a = this->start();
1715 setStartUnchecked(a);
1725 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1728 assert(Level &&
"Cannot insert next to the root");
1729 bool SplitRoot =
false;
1735 if (IM.rootSize < RootBranch::Capacity) {
1736 IM.rootBranch().
insert(P.
offset(0), IM.rootSize, Node, Stop);
1755 if (P.
size(Level) == Branch::Capacity) {
1757 assert(!SplitRoot &&
"Cannot overflow after splitting the root");
1758 SplitRoot = overflow<Branch>(
Level);
1764 setNodeStop(Level, Stop);
1770 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1773 if (this->branched())
1774 return treeInsert(a, b, y);
1782 if (Size <= RootLeaf::Capacity) {
1783 P.
setSize(0, IM.rootSize = Size);
1792 treeInsert(a, b, y);
1795 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1798 using namespace IntervalMapImpl;
1799 Path &P = this->path;
1802 P.legalizeForInsert(this->map->height);
1805 if (P.leafOffset() == 0 && Traits::startLess(a, P.leaf<
Leaf>().
start(0))) {
1807 if (NodeRef Sib = P.getLeftSibling(P.height())) {
1809 unsigned SibOfs = Sib.size() - 1;
1810 if (SibLeaf.
value(SibOfs) == y &&
1811 Traits::adjacent(SibLeaf.
stop(SibOfs), a)) {
1819 if (Traits::stopLess(b, CurLeaf.
start(0)) &&
1820 (y != CurLeaf.
value(0) || !Traits::adjacent(b, CurLeaf.
start(0)))) {
1822 setNodeStop(P.height(), SibLeaf.
stop(SibOfs) = b);
1827 a = SibLeaf.
start(SibOfs);
1833 this->map->rootBranchStart() = a;
1838 unsigned Size = P.leafSize();
1839 bool Grow = P.leafOffset() ==
Size;
1840 Size = P.leaf<
Leaf>().insertFrom(P.leafOffset(),
Size, a, b, y);
1843 if (Size > Leaf::Capacity) {
1844 overflow<Leaf>(P.height());
1845 Grow = P.leafOffset() == P.leafSize();
1846 Size = P.leaf<
Leaf>().insertFrom(P.leafOffset(), P.leafSize(), a, b, y);
1847 assert(Size <= Leaf::Capacity &&
"overflow() didn't make room");
1851 P.setSize(P.height(),
Size);
1855 setNodeStop(P.height(), b);
1859 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1865 if (this->branched())
1872 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1881 IM.deleteNode(&Node);
1882 eraseNode(IM.height);
1884 if (UpdateRoot && IM.branched() && P.
valid() && P.
atBegin())
1885 IM.rootBranchStart() = P.
leaf<
Leaf>().start(0);
1891 unsigned NewSize = P.
leafSize() - 1;
1892 P.
setSize(IM.height, NewSize);
1895 setNodeStop(IM.height, Node.
stop(NewSize - 1));
1897 }
else if (UpdateRoot && P.
atBegin())
1898 IM.rootBranchStart() = P.
leaf<
Leaf>().start(0);
1905 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1908 assert(Level &&
"Cannot erase root node");
1917 IM.switchRootToLeaf();
1924 if (P.
size(Level) == 1) {
1926 IM.deleteNode(&Parent);
1931 unsigned NewSize = P.
size(Level) - 1;
1934 if (P.
offset(Level) == NewSize) {
1935 setNodeStop(Level, Parent.
stop(NewSize - 1));
1953 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1954 template <
typename NodeT>
1957 using namespace IntervalMapImpl;
1958 Path &P = this->path;
1959 unsigned CurSize[4];
1962 unsigned Elements = 0;
1963 unsigned Offset = P.offset(Level);
1966 NodeRef LeftSib = P.getLeftSibling(Level);
1968 Offset += Elements = CurSize[Nodes] = LeftSib.size();
1969 Node[Nodes++] = &LeftSib.get<NodeT>();
1973 Elements += CurSize[Nodes] = P.size(Level);
1974 Node[Nodes++] = &P.node<NodeT>(
Level);
1977 NodeRef RightSib = P.getRightSibling(Level);
1979 Elements += CurSize[Nodes] = RightSib.size();
1980 Node[Nodes++] = &RightSib.get<NodeT>();
1984 unsigned NewNode = 0;
1985 if (Elements + 1 > Nodes * NodeT::Capacity) {
1987 NewNode = Nodes == 1 ? 1 : Nodes - 1;
1988 CurSize[Nodes] = CurSize[NewNode];
1989 Node[Nodes] = Node[NewNode];
1990 CurSize[NewNode] = 0;
1991 Node[NewNode] = this->map->template newNode<NodeT>();
1996 unsigned NewSize[4];
1997 IdxPair NewOffset =
distribute(Nodes, Elements, NodeT::Capacity,
1998 CurSize, NewSize, Offset,
true);
2006 bool SplitRoot =
false;
2009 KeyT Stop = Node[Pos]->stop(NewSize[Pos]-1);
2010 if (NewNode && Pos == NewNode) {
2011 SplitRoot = insertNode(Level, NodeRef(Node[Pos], NewSize[Pos]), Stop);
2014 P.setSize(Level, NewSize[Pos]);
2015 setNodeStop(Level, Stop);
2017 if (Pos + 1 == Nodes)
2024 while(Pos != NewOffset.first) {
2028 P.offset(Level) = NewOffset.second;
2048 template <
typename MapA,
typename MapB>
2050 using KeyType =
typename MapA::KeyType;
2051 using Traits =
typename MapA::KeyTraits;
2053 typename MapA::const_iterator posA;
2054 typename MapB::const_iterator posB;
2063 if (Traits::stopLess(posA.stop(), posB.start())) {
2065 posA.advanceTo(posB.start());
2066 if (!posA.valid() || !Traits::stopLess(posB.stop(), posA.start()))
2068 }
else if (Traits::stopLess(posB.stop(), posA.start())) {
2070 posB.advanceTo(posA.start());
2071 if (!posB.valid() || !Traits::stopLess(posA.stop(), posB.start()))
2079 posA.advanceTo(posB.start());
2080 if (!posA.valid() || !Traits::stopLess(posB.stop(), posA.start()))
2083 posB.advanceTo(posA.start());
2084 if (!posB.valid() || !Traits::stopLess(posA.stop(), posB.start()))
2092 : posA(b.empty() ? a.
end() : a.
find(b.start())),
2093 posB(posA.valid() ? b.
find(posA.start()) : b.
end()) { advance(); }
2097 return posA.valid() && posB.valid();
2101 const typename MapA::const_iterator &
a()
const {
return posA; }
2104 const typename MapB::const_iterator &
b()
const {
return posB; }
2108 KeyType ak = a().start();
2109 KeyType bk = b().start();
2110 return Traits::startLess(ak, bk) ? bk : ak;
2115 KeyType ak = a().stop();
2116 KeyType bk = b().stop();
2117 return Traits::startLess(ak, bk) ? ak : bk;
2135 if (Traits::startLess(posB.stop(), posA.stop()))
2148 if (Traits::stopLess(posA.stop(), x))
2150 if (Traits::stopLess(posB.stop(), x))
2158 #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...
void push_back(const T &Elt)
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)
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
Compute iterated dominance frontiers using a linear time algorithm.
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.
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!
llvm::AlignedCharArray< alignof(llvm::detail::AlignerImpl< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 >), sizeof(::llvm::detail::SizerImpl< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 >)>::buffer char buffer[Size]
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.
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.
This union template exposes a suitably aligned and sized character array member which can hold elemen...
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
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
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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.
ValT & unsafeValue() const
unsafeValue - Writable access to value() for iterator.