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>
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) {
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");
294 Sib.
copy(*
this, 0, SSize, Count);
295 erase(0, Count, Size);
306 Sib.
copy(*
this, Size-Count, 0, 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[],
413 unsigned Position,
bool Grow);
435 template <
typename KeyT,
typename ValT>
444 static_cast<unsigned>(2*
sizeof(KeyT)+
sizeof(
ValT)),
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; }
503 explicit operator bool()
const {
return pip.getOpaqueValue(); }
506 template <
typename NodeT>
507 NodeRef(NodeT *p,
unsigned n) : pip(p, n - 1) {
508 assert(n <= NodeT::Capacity &&
"Size too big for node");
512 unsigned size()
const {
return pip.getInt() + 1; }
515 void setSize(
unsigned n) { pip.setInt(n - 1); }
521 return reinterpret_cast<NodeRef*
>(pip.getPointer())[i];
525 template <
typename NodeT>
527 return *
reinterpret_cast<NodeT*
>(pip.getPointer());
533 assert(pip.getPointer() != RHS.pip.
getPointer() &&
"Inconsistent NodeRefs");
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)
782 : node(&Node.
subtree(0)), size(Node.
size()), offset(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);
805 unsigned leafSize()
const {
return path.back().size; }
811 return !path.empty() && path.front().offset < path.front().size;
816 unsigned height()
const {
return path.size() - 1; }
828 path[
Level] = Entry(subtree(Level - 1), offset(Level));
835 path.push_back(Entry(Node, Offset));
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);
894 void moveRight(
unsigned Level);
898 for (
unsigned i = 0, e = path.size(); i != e; ++i)
899 if (path[i].offset != 0)
908 return path[
Level].offset == path[
Level].size - 1;
920 ++path[
Level].offset;
930 template <
typename KeyT,
typename ValT,
931 unsigned N = IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
932 typename Traits = IntervalMapInfo<KeyT>>
944 DesiredRootBranchCap = (
sizeof(
RootLeaf) -
sizeof(KeyT)) /
946 RootBranchCap = DesiredRootBranchCap ? DesiredRootBranchCap : 1
953 struct RootBranchData {
981 template <
typename T>
991 const RootLeaf &rootLeaf()
const {
992 assert(!branched() &&
"Cannot acces leaf data in branched root");
993 return dataAs<RootLeaf>();
995 RootLeaf &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();
1035 new(&rootLeaf()) RootLeaf();
1038 bool branched()
const {
return height > 0; }
1040 ValT treeSafeLookup(KeyT x, ValT NotFound)
const;
1041 void visitNodes(
void (IntervalMap::*f)(IntervalMapImpl::NodeRef,
1043 void deleteNode(IntervalMapImpl::NodeRef Node,
unsigned Level);
1047 assert((uintptr_t(data.buffer) & (
alignof(
RootLeaf) - 1)) == 0 &&
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>
1145 ValT IntervalMap<KeyT, ValT, N, Traits>::
1146 treeSafeLookup(KeyT x, ValT NotFound)
const {
1147 assert(branched() &&
"treeLookup assumes a branched root");
1149 IntervalMapImpl::NodeRef NR = rootBranch().safeLookup(x);
1150 for (
unsigned h = height-1; h; --h)
1151 NR = NR.get<
Branch>().safeLookup(x);
1152 return NR.get<Leaf>().safeLookup(x, NotFound);
1157 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1159 branchRoot(
unsigned Position) {
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>
1200 splitRoot(
unsigned Position) {
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>
1237 void IntervalMap<KeyT, ValT, N, Traits>::
1238 visitNodes(
void (IntervalMap::*f)(IntervalMapImpl::NodeRef,
unsigned Height)) {
1241 SmallVector<IntervalMapImpl::NodeRef, 4> Refs, NextRefs;
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)
1251 NextRefs.push_back(Refs[i].subtree(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>
1264 void IntervalMap<KeyT, ValT, N, Traits>::
1265 deleteNode(IntervalMapImpl::NodeRef Node,
unsigned Level) {
1267 deleteNode(&Node.get<
Branch>());
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();
1310 path.setRoot(&map->rootBranch(), map->rootSize,
Offset);
1312 path.setRoot(&map->rootLeaf(), map->rootSize,
Offset);
1315 void pathFillFind(KeyT x);
1316 void treeFind(KeyT x);
1317 void treeAdvanceTo(KeyT x);
1321 assert(valid() &&
"Cannot access invalid iterator");
1322 return branched() ? path.leaf<
Leaf>().start(path.leafOffset()) :
1328 assert(valid() &&
"Cannot access invalid iterator");
1329 return branched() ? path.leaf<
Leaf>().stop(path.leafOffset()) :
1335 assert(valid() &&
"Cannot access invalid iterator");
1336 return branched() ? path.leaf<
Leaf>().value(path.leafOffset()) :
1349 bool valid()
const {
return path.valid(); }
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>();
1382 path.fillLeft(map->height);
1387 setRoot(map->rootSize);
1392 assert(valid() &&
"Cannot increment end()");
1393 if (++path.leafOffset() == path.leafSize() && branched())
1394 path.moveRight(map->height);
1407 if (path.leafOffset() && (valid() || !branched()))
1408 --path.leafOffset();
1410 path.moveLeft(map->height);
1427 setRoot(map->rootLeaf().findFrom(0, map->rootSize, x));
1440 map->rootLeaf().findFrom(path.leafOffset(), 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();
1609 RootLeaf &Node = P.leaf<RootLeaf>();
1610 return i && Node.value(i-1) == Value &&
1611 Traits::adjacent(Node.stop(i-1), Start);
1614 if (
unsigned i = P.leafOffset()) {
1615 Leaf &Node = P.leaf<Leaf>();
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;
1619 Leaf &Node = NR.get<Leaf>();
1620 return Node.value(i) == Value && Traits::adjacent(Node.stop(i), Start);
1630 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1631 bool IntervalMap<KeyT, ValT, N, Traits>::
1632 iterator::canCoalesceRight(KeyT Stop, ValT Value) {
1633 using namespace IntervalMapImpl;
1634 Path &P = this->path;
1635 unsigned i = P.leafOffset() + 1;
1636 if (!this->branched()) {
1637 if (i >= P.leafSize())
1639 RootLeaf &Node = P.leaf<RootLeaf>();
1640 return Node.value(i) == Value && Traits::adjacent(Stop, Node.start(i));
1643 if (i < P.leafSize()) {
1644 Leaf &Node = P.leaf<Leaf>();
1645 return Node.value(i) == Value && Traits::adjacent(Stop, Node.start(i));
1646 }
else if (NodeRef NR = P.getRightSibling(P.height())) {
1647 Leaf &Node = NR.get<Leaf>();
1648 return Node.value(0) == Value && Traits::adjacent(Stop, Node.start(0));
1654 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1655 void IntervalMap<KeyT, ValT, N, Traits>::
1656 iterator::setNodeStop(
unsigned Level, KeyT Stop) {
1660 IntervalMapImpl::Path &P = this->path;
1663 P.node<
Branch>(
Level).stop(P.offset(Level)) = Stop;
1664 if (!P.atLastEntry(Level))
1668 P.node<RootBranch>(
Level).stop(P.offset(Level)) = Stop;
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);
1788 IdxPair Offset = IM.branchRoot(P.
leafOffset());
1792 treeInsert(a, b, y);
1795 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1798 using namespace IntervalMapImpl;
1799 Path &P = this->path;
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;
1843 if (
Size > Leaf::Capacity) {
1844 overflow<Leaf>(P.
height());
1847 assert(
Size <= Leaf::Capacity &&
"overflow() didn't make room");
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>
1906 void IntervalMap<KeyT, ValT, N, Traits>::
1907 iterator::eraseNode(
unsigned Level) {
1908 assert(Level &&
"Cannot erase root node");
1909 IntervalMap &IM = *this->map;
1910 IntervalMapImpl::Path &P = this->path;
1913 IM.rootBranch().erase(P.offset(0), IM.rootSize);
1917 IM.switchRootToLeaf();
1924 if (P.size(Level) == 1) {
1926 IM.deleteNode(&Parent);
1930 Parent.erase(P.offset(Level), P.size(Level));
1931 unsigned NewSize = P.size(Level) - 1;
1932 P.setSize(Level, NewSize);
1934 if (P.offset(Level) == NewSize) {
1935 setNodeStop(Level, Parent.stop(NewSize - 1));
1943 P.offset(Level + 1) = 0;
1953 template <
typename KeyT,
typename ValT,
unsigned N,
typename Traits>
1954 template <
typename NodeT>
1955 bool IntervalMap<KeyT, ValT, N, Traits>::
1956 iterator::overflow(
unsigned Level) {
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];
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())) {
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
KeyT & unsafeStop() const
unsafeStop - Writable access to stop() for iterator.
void setValueUnchecked(ValT x)
setValueUnchecked - Change the mapped value of the current interval without checking for coalescing...
ValT lookup(KeyT x, ValT NotFound=ValT()) const
lookup - Return the mapped value at x or NotFound.
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 copy(const NodeBase< T1, T2, M > &Other, unsigned i, unsigned j, unsigned Count)
copy - Copy elements from another node.
NodeRef getLeftSibling(unsigned Level) const
getLeftSibling - Get the left sibling node at Level, or a null NodeRef.
const_iterator begin() const
void adjustSiblingSizes(NodeT *Node[], unsigned Nodes, unsigned CurSize[], const unsigned NewSize[])
IntervalMapImpl::adjustSiblingSizes - Move elements between sibling nodes.
void legalizeForInsert(unsigned Level)
legalizeForInsert - Prepare the path for an insertion at Level.
IntervalMapOverlaps - Iterate over the overlaps of mapped intervals in two IntervalMaps.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
const KeyT & stop(unsigned i) const
KeyT & unsafeStart() const
unsafeStart - Writable access to start() for iterator.
bool atBegin() const
atBegin - Return true if the current position is the first map entry.
const KeyT & stop(unsigned i) 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.
const_iterator find(KeyT x) const
find - Return an iterator pointing to the first interval ending at or after x, or end()...
KeyT stop() const
stop - Return the largest mapped key in a non-empty map.
void pop()
pop - Remove the last path entry.
KeyType stop() const
stop - End of the overlapping interval.
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.
const_iterator end() 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.
ValT safeLookup(KeyT x, ValT NotFound) const
safeLookup - Lookup mapped value for a safe key.
const KeyT & start(unsigned i) const
void setRoot(unsigned Offset)
const NodeRef & subtree(unsigned i) const
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 & unsafeValue() const
unsafeValue - Writable access to value() for iterator.
bool valid() const
valid - Return true if iterator is at an overlap.
void advanceTo(KeyT x)
advanceTo - Move to the first interval with stop >= x, or end().
RecyclingAllocator - This class wraps an Allocator, adding the functionality of recycling deleted obj...
void treeAdvanceTo(KeyT x)
treeAdvanceTo - Find position after the current one.
bool operator==(const NodeRef &RHS) const
bool operator!=(const NodeRef &RHS) const
bool empty() const
empty - Return true when no intervals are mapped.
static bool startLess(const T &x, const T &a)
startLess - Return true if x is not in [a;b).
PointerTy getPointer() const
const MapA::const_iterator & a() const
a - access the left hand side in the overlap.
void erase()
erase - Erase the current interval.
void clear()
clear - Remove all entries.
IntervalMap(Allocator &a)
NodeRef & subtree(unsigned i) const
subtree - Access the i'th subtree reference in a branch node.
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...
unsigned safeFind(unsigned i, KeyT x) const
safeFind - Find an interval that is known to exist.
unsigned size() const
size - Return the number of elements in the referenced node.
const KeyT & start() const
start - Return the beginning of the current interval.
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...
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
unsigned size(unsigned Level) const
void moveRight(unsigned i, unsigned j, unsigned Count)
moveRight - Move elements to the right.
void find(KeyT x)
find - Move to the first interval with stop >= x, or end().
unsigned findFrom(unsigned i, unsigned Size, KeyT x) const
findFrom - Find the first interval after i that may contain x.
const_iterator & operator--()
predecrement - move to the previous interval.
void goToBegin()
goToBegin - Move to the first interval in map.
bool operator!=(const const_iterator &RHS) const
const MapB::const_iterator & b() const
b - access the right hand side in the overlap.
void setStop(KeyT b)
setStop - Move the end of the current interval.
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.
unsigned leafOffset() const
const_iterator operator++(int)
postincrement - Dont do that!
NodeRef & subtree(unsigned Level) const
subtree - Get the subtree referenced from Level.
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().
const ValT & value(unsigned i) const
unsigned height() const
height - Return the height of the tree corresponding to this path.
void insert(KeyT a, KeyT b, ValT y)
insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
KeyT start() const
start - Return the smallest mapped key in a non-empty map.
void moveLeft(unsigned i, unsigned j, unsigned Count)
moveLeft - Move elements to the left.
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
std::pair< unsigned, unsigned > IdxPair
NodeT & node(unsigned Level) const
void goToEnd()
goToEnd - Move beyond the last interval in map.
unsigned safeFind(unsigned i, KeyT x) const
safeFind - Find a subtree that is known to exist.
void setStart(KeyT a)
setStart - Move the start of the current interval.
bool valid() const
valid - Return true if path is at a valid node, not at end().
bool valid() const
valid - Return true if the current position is valid, false for end().
void advanceTo(KeyType x)
advanceTo - Move to the first overlapping interval with stopLess(x, stop()).
void reset(unsigned Level)
reset - Reset cached information about node(Level) from subtree(Level -1).
RecyclingAllocator< BumpPtrAllocator, char, AllocBytes, CacheLineBytes > Allocator
Allocator - The recycling allocator used for both branch and leaf nodes.
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.
const KeyT & stop() const
stop - Return the end of the current interval.
void push(NodeRef Node, unsigned Offset)
push - Add entry to path.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
void moveRight(unsigned Level)
moveRight - Move path to the left sibling at Level.
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.
void treeFind(KeyT x)
treeFind - Find in a branched tree.
IntervalMapImpl::Path path
unsigned leafSize() const
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)
NodeT & get() const
get - Dereference as a NodeT reference.
unsigned findFrom(unsigned i, unsigned Size, KeyT x) const
findFrom - Find the first subtree after i that may contain x.
const_iterator operator--(int)
postdecrement - Dont do that!
bool atBegin() const
atBegin - Return true if path is at begin().
unsigned offset(unsigned Level) const
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())
NodeRef safeLookup(KeyT x) const
safeLookup - Get the subtree containing x, Assuming that x is in range.
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.
bool operator==(const const_iterator &RHS) const
const ValT & operator*() const
bool operator==(uint64_t V1, const APInt &V2)
const ValT & value() const
value - Return the mapped value at the current interval.
static bool nonEmpty(const T &a, const T &b)
nonEmpty - Return true if [a;b) is non-empty.
SlotIndex - An opaque wrapper around machine indexes.
NodeBase< std::pair< KeyT, KeyT >, ValT, LeafSize > LeafBase
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 atLastEntry(unsigned Level) const
atLastEntry - Return true if the path is at the last entry of the node at Level.