Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2166 lines
72 KiB

  1. //===- llvm/ADT/IntervalMap.h - A sorted interval map -----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a coalescing interval map for small objects.
  11. //
  12. // KeyT objects are mapped to ValT objects. Intervals of keys that map to the
  13. // same value are represented in a compressed form.
  14. //
  15. // Iterators provide ordered access to the compressed intervals rather than the
  16. // individual keys, and insert and erase operations use key intervals as well.
  17. //
  18. // Like SmallVector, IntervalMap will store the first N intervals in the map
  19. // object itself without any allocations. When space is exhausted it switches to
  20. // a B+-tree representation with very small overhead for small key and value
  21. // objects.
  22. //
  23. // A Traits class specifies how keys are compared. It also allows IntervalMap to
  24. // work with both closed and half-open intervals.
  25. //
  26. // Keys and values are not stored next to each other in a std::pair, so we don't
  27. // provide such a value_type. Dereferencing iterators only returns the mapped
  28. // value. The interval bounds are accessible through the start() and stop()
  29. // iterator methods.
  30. //
  31. // IntervalMap is optimized for small key and value objects, 4 or 8 bytes each
  32. // is the optimal size. For large objects use std::map instead.
  33. //
  34. //===----------------------------------------------------------------------===//
  35. //
  36. // Synopsis:
  37. //
  38. // template <typename KeyT, typename ValT, unsigned N, typename Traits>
  39. // class IntervalMap {
  40. // public:
  41. // typedef KeyT key_type;
  42. // typedef ValT mapped_type;
  43. // typedef RecyclingAllocator<...> Allocator;
  44. // class iterator;
  45. // class const_iterator;
  46. //
  47. // explicit IntervalMap(Allocator&);
  48. // ~IntervalMap():
  49. //
  50. // bool empty() const;
  51. // KeyT start() const;
  52. // KeyT stop() const;
  53. // ValT lookup(KeyT x, Value NotFound = Value()) const;
  54. //
  55. // const_iterator begin() const;
  56. // const_iterator end() const;
  57. // iterator begin();
  58. // iterator end();
  59. // const_iterator find(KeyT x) const;
  60. // iterator find(KeyT x);
  61. //
  62. // void insert(KeyT a, KeyT b, ValT y);
  63. // void clear();
  64. // };
  65. //
  66. // template <typename KeyT, typename ValT, unsigned N, typename Traits>
  67. // class IntervalMap::const_iterator :
  68. // public std::iterator<std::bidirectional_iterator_tag, ValT> {
  69. // public:
  70. // bool operator==(const const_iterator &) const;
  71. // bool operator!=(const const_iterator &) const;
  72. // bool valid() const;
  73. //
  74. // const KeyT &start() const;
  75. // const KeyT &stop() const;
  76. // const ValT &value() const;
  77. // const ValT &operator*() const;
  78. // const ValT *operator->() const;
  79. //
  80. // const_iterator &operator++();
  81. // const_iterator &operator++(int);
  82. // const_iterator &operator--();
  83. // const_iterator &operator--(int);
  84. // void goToBegin();
  85. // void goToEnd();
  86. // void find(KeyT x);
  87. // void advanceTo(KeyT x);
  88. // };
  89. //
  90. // template <typename KeyT, typename ValT, unsigned N, typename Traits>
  91. // class IntervalMap::iterator : public const_iterator {
  92. // public:
  93. // void insert(KeyT a, KeyT b, Value y);
  94. // void erase();
  95. // };
  96. //
  97. //===----------------------------------------------------------------------===//
  98. #ifndef LLVM_ADT_INTERVALMAP_H
  99. #define LLVM_ADT_INTERVALMAP_H
  100. #include "llvm/ADT/PointerIntPair.h"
  101. #include "llvm/ADT/SmallVector.h"
  102. #include "llvm/Support/Allocator.h"
  103. #include "llvm/Support/RecyclingAllocator.h"
  104. #include <iterator>
  105. namespace llvm {
  106. //===----------------------------------------------------------------------===//
  107. //--- Key traits ---//
  108. //===----------------------------------------------------------------------===//
  109. //
  110. // The IntervalMap works with closed or half-open intervals.
  111. // Adjacent intervals that map to the same value are coalesced.
  112. //
  113. // The IntervalMapInfo traits class is used to determine if a key is contained
  114. // in an interval, and if two intervals are adjacent so they can be coalesced.
  115. // The provided implementation works for closed integer intervals, other keys
  116. // probably need a specialized version.
  117. //
  118. // The point x is contained in [a;b] when !startLess(x, a) && !stopLess(b, x).
  119. //
  120. // It is assumed that (a;b] half-open intervals are not used, only [a;b) is
  121. // allowed. This is so that stopLess(a, b) can be used to determine if two
  122. // intervals overlap.
  123. //
  124. //===----------------------------------------------------------------------===//
  125. template <typename T>
  126. struct IntervalMapInfo {
  127. /// startLess - Return true if x is not in [a;b].
  128. /// This is x < a both for closed intervals and for [a;b) half-open intervals.
  129. static inline bool startLess(const T &x, const T &a) {
  130. return x < a;
  131. }
  132. /// stopLess - Return true if x is not in [a;b].
  133. /// This is b < x for a closed interval, b <= x for [a;b) half-open intervals.
  134. static inline bool stopLess(const T &b, const T &x) {
  135. return b < x;
  136. }
  137. /// adjacent - Return true when the intervals [x;a] and [b;y] can coalesce.
  138. /// This is a+1 == b for closed intervals, a == b for half-open intervals.
  139. static inline bool adjacent(const T &a, const T &b) {
  140. return a+1 == b;
  141. }
  142. };
  143. template <typename T>
  144. struct IntervalMapHalfOpenInfo {
  145. /// startLess - Return true if x is not in [a;b).
  146. static inline bool startLess(const T &x, const T &a) {
  147. return x < a;
  148. }
  149. /// stopLess - Return true if x is not in [a;b).
  150. static inline bool stopLess(const T &b, const T &x) {
  151. return b <= x;
  152. }
  153. /// adjacent - Return true when the intervals [x;a) and [b;y) can coalesce.
  154. static inline bool adjacent(const T &a, const T &b) {
  155. return a == b;
  156. }
  157. };
  158. /// IntervalMapImpl - Namespace used for IntervalMap implementation details.
  159. /// It should be considered private to the implementation.
  160. namespace IntervalMapImpl {
  161. // Forward declarations.
  162. template <typename, typename, unsigned, typename> class LeafNode;
  163. template <typename, typename, unsigned, typename> class BranchNode;
  164. typedef std::pair<unsigned,unsigned> IdxPair;
  165. //===----------------------------------------------------------------------===//
  166. //--- IntervalMapImpl::NodeBase ---//
  167. //===----------------------------------------------------------------------===//
  168. //
  169. // Both leaf and branch nodes store vectors of pairs.
  170. // Leaves store ((KeyT, KeyT), ValT) pairs, branches use (NodeRef, KeyT).
  171. //
  172. // Keys and values are stored in separate arrays to avoid padding caused by
  173. // different object alignments. This also helps improve locality of reference
  174. // when searching the keys.
  175. //
  176. // The nodes don't know how many elements they contain - that information is
  177. // stored elsewhere. Omitting the size field prevents padding and allows a node
  178. // to fill the allocated cache lines completely.
  179. //
  180. // These are typical key and value sizes, the node branching factor (N), and
  181. // wasted space when nodes are sized to fit in three cache lines (192 bytes):
  182. //
  183. // T1 T2 N Waste Used by
  184. // 4 4 24 0 Branch<4> (32-bit pointers)
  185. // 8 4 16 0 Leaf<4,4>, Branch<4>
  186. // 8 8 12 0 Leaf<4,8>, Branch<8>
  187. // 16 4 9 12 Leaf<8,4>
  188. // 16 8 8 0 Leaf<8,8>
  189. //
  190. //===----------------------------------------------------------------------===//
  191. template <typename T1, typename T2, unsigned N>
  192. class NodeBase {
  193. public:
  194. enum { Capacity = N };
  195. T1 first[N];
  196. T2 second[N];
  197. /// copy - Copy elements from another node.
  198. /// @param Other Node elements are copied from.
  199. /// @param i Beginning of the source range in other.
  200. /// @param j Beginning of the destination range in this.
  201. /// @param Count Number of elements to copy.
  202. template <unsigned M>
  203. void copy(const NodeBase<T1, T2, M> &Other, unsigned i,
  204. unsigned j, unsigned Count) {
  205. assert(i + Count <= M && "Invalid source range");
  206. assert(j + Count <= N && "Invalid dest range");
  207. for (unsigned e = i + Count; i != e; ++i, ++j) {
  208. first[j] = Other.first[i];
  209. second[j] = Other.second[i];
  210. }
  211. }
  212. /// moveLeft - Move elements to the left.
  213. /// @param i Beginning of the source range.
  214. /// @param j Beginning of the destination range.
  215. /// @param Count Number of elements to copy.
  216. void moveLeft(unsigned i, unsigned j, unsigned Count) {
  217. assert(j <= i && "Use moveRight shift elements right");
  218. copy(*this, i, j, Count);
  219. }
  220. /// moveRight - Move elements to the right.
  221. /// @param i Beginning of the source range.
  222. /// @param j Beginning of the destination range.
  223. /// @param Count Number of elements to copy.
  224. void moveRight(unsigned i, unsigned j, unsigned Count) {
  225. assert(i <= j && "Use moveLeft shift elements left");
  226. assert(j + Count <= N && "Invalid range");
  227. while (Count--) {
  228. first[j + Count] = first[i + Count];
  229. second[j + Count] = second[i + Count];
  230. }
  231. }
  232. /// erase - Erase elements [i;j).
  233. /// @param i Beginning of the range to erase.
  234. /// @param j End of the range. (Exclusive).
  235. /// @param Size Number of elements in node.
  236. void erase(unsigned i, unsigned j, unsigned Size) {
  237. moveLeft(j, i, Size - j);
  238. }
  239. /// erase - Erase element at i.
  240. /// @param i Index of element to erase.
  241. /// @param Size Number of elements in node.
  242. void erase(unsigned i, unsigned Size) {
  243. erase(i, i+1, Size);
  244. }
  245. /// shift - Shift elements [i;size) 1 position to the right.
  246. /// @param i Beginning of the range to move.
  247. /// @param Size Number of elements in node.
  248. void shift(unsigned i, unsigned Size) {
  249. moveRight(i, i + 1, Size - i);
  250. }
  251. /// transferToLeftSib - Transfer elements to a left sibling node.
  252. /// @param Size Number of elements in this.
  253. /// @param Sib Left sibling node.
  254. /// @param SSize Number of elements in sib.
  255. /// @param Count Number of elements to transfer.
  256. void transferToLeftSib(unsigned Size, NodeBase &Sib, unsigned SSize,
  257. unsigned Count) {
  258. Sib.copy(*this, 0, SSize, Count);
  259. erase(0, Count, Size);
  260. }
  261. /// transferToRightSib - Transfer elements to a right sibling node.
  262. /// @param Size Number of elements in this.
  263. /// @param Sib Right sibling node.
  264. /// @param SSize Number of elements in sib.
  265. /// @param Count Number of elements to transfer.
  266. void transferToRightSib(unsigned Size, NodeBase &Sib, unsigned SSize,
  267. unsigned Count) {
  268. Sib.moveRight(0, Count, SSize);
  269. Sib.copy(*this, Size-Count, 0, Count);
  270. }
  271. /// adjustFromLeftSib - Adjust the number if elements in this node by moving
  272. /// elements to or from a left sibling node.
  273. /// @param Size Number of elements in this.
  274. /// @param Sib Right sibling node.
  275. /// @param SSize Number of elements in sib.
  276. /// @param Add The number of elements to add to this node, possibly < 0.
  277. /// @return Number of elements added to this node, possibly negative.
  278. int adjustFromLeftSib(unsigned Size, NodeBase &Sib, unsigned SSize, int Add) {
  279. if (Add > 0) {
  280. // We want to grow, copy from sib.
  281. unsigned Count = std::min(std::min(unsigned(Add), SSize), N - Size);
  282. Sib.transferToRightSib(SSize, *this, Size, Count);
  283. return Count;
  284. } else {
  285. // We want to shrink, copy to sib.
  286. unsigned Count = std::min(std::min(unsigned(-Add), Size), N - SSize);
  287. transferToLeftSib(Size, Sib, SSize, Count);
  288. return -Count;
  289. }
  290. }
  291. };
  292. /// IntervalMapImpl::adjustSiblingSizes - Move elements between sibling nodes.
  293. /// @param Node Array of pointers to sibling nodes.
  294. /// @param Nodes Number of nodes.
  295. /// @param CurSize Array of current node sizes, will be overwritten.
  296. /// @param NewSize Array of desired node sizes.
  297. template <typename NodeT>
  298. void adjustSiblingSizes(NodeT *Node[], unsigned Nodes,
  299. unsigned CurSize[], const unsigned NewSize[]) {
  300. // Move elements right.
  301. for (int n = Nodes - 1; n; --n) {
  302. if (CurSize[n] == NewSize[n])
  303. continue;
  304. for (int m = n - 1; m != -1; --m) {
  305. int d = Node[n]->adjustFromLeftSib(CurSize[n], *Node[m], CurSize[m],
  306. NewSize[n] - CurSize[n]);
  307. CurSize[m] -= d;
  308. CurSize[n] += d;
  309. // Keep going if the current node was exhausted.
  310. if (CurSize[n] >= NewSize[n])
  311. break;
  312. }
  313. }
  314. if (Nodes == 0)
  315. return;
  316. // Move elements left.
  317. for (unsigned n = 0; n != Nodes - 1; ++n) {
  318. if (CurSize[n] == NewSize[n])
  319. continue;
  320. for (unsigned m = n + 1; m != Nodes; ++m) {
  321. int d = Node[m]->adjustFromLeftSib(CurSize[m], *Node[n], CurSize[n],
  322. CurSize[n] - NewSize[n]);
  323. CurSize[m] += d;
  324. CurSize[n] -= d;
  325. // Keep going if the current node was exhausted.
  326. if (CurSize[n] >= NewSize[n])
  327. break;
  328. }
  329. }
  330. #ifndef NDEBUG
  331. for (unsigned n = 0; n != Nodes; n++)
  332. assert(CurSize[n] == NewSize[n] && "Insufficient element shuffle");
  333. #endif
  334. }
  335. /// IntervalMapImpl::distribute - Compute a new distribution of node elements
  336. /// after an overflow or underflow. Reserve space for a new element at Position,
  337. /// and compute the node that will hold Position after redistributing node
  338. /// elements.
  339. ///
  340. /// It is required that
  341. ///
  342. /// Elements == sum(CurSize), and
  343. /// Elements + Grow <= Nodes * Capacity.
  344. ///
  345. /// NewSize[] will be filled in such that:
  346. ///
  347. /// sum(NewSize) == Elements, and
  348. /// NewSize[i] <= Capacity.
  349. ///
  350. /// The returned index is the node where Position will go, so:
  351. ///
  352. /// sum(NewSize[0..idx-1]) <= Position
  353. /// sum(NewSize[0..idx]) >= Position
  354. ///
  355. /// The last equality, sum(NewSize[0..idx]) == Position, can only happen when
  356. /// Grow is set and NewSize[idx] == Capacity-1. The index points to the node
  357. /// before the one holding the Position'th element where there is room for an
  358. /// insertion.
  359. ///
  360. /// @param Nodes The number of nodes.
  361. /// @param Elements Total elements in all nodes.
  362. /// @param Capacity The capacity of each node.
  363. /// @param CurSize Array[Nodes] of current node sizes, or NULL.
  364. /// @param NewSize Array[Nodes] to receive the new node sizes.
  365. /// @param Position Insert position.
  366. /// @param Grow Reserve space for a new element at Position.
  367. /// @return (node, offset) for Position.
  368. IdxPair distribute(unsigned Nodes, unsigned Elements, unsigned Capacity,
  369. const unsigned *CurSize, unsigned NewSize[],
  370. unsigned Position, bool Grow);
  371. //===----------------------------------------------------------------------===//
  372. //--- IntervalMapImpl::NodeSizer ---//
  373. //===----------------------------------------------------------------------===//
  374. //
  375. // Compute node sizes from key and value types.
  376. //
  377. // The branching factors are chosen to make nodes fit in three cache lines.
  378. // This may not be possible if keys or values are very large. Such large objects
  379. // are handled correctly, but a std::map would probably give better performance.
  380. //
  381. //===----------------------------------------------------------------------===//
  382. enum {
  383. // Cache line size. Most architectures have 32 or 64 byte cache lines.
  384. // We use 64 bytes here because it provides good branching factors.
  385. Log2CacheLine = 6,
  386. CacheLineBytes = 1 << Log2CacheLine,
  387. DesiredNodeBytes = 3 * CacheLineBytes
  388. };
  389. template <typename KeyT, typename ValT>
  390. struct NodeSizer {
  391. enum {
  392. // Compute the leaf node branching factor that makes a node fit in three
  393. // cache lines. The branching factor must be at least 3, or some B+-tree
  394. // balancing algorithms won't work.
  395. // LeafSize can't be larger than CacheLineBytes. This is required by the
  396. // PointerIntPair used by NodeRef.
  397. DesiredLeafSize = DesiredNodeBytes /
  398. static_cast<unsigned>(2*sizeof(KeyT)+sizeof(ValT)),
  399. MinLeafSize = 3,
  400. LeafSize = DesiredLeafSize > MinLeafSize ? DesiredLeafSize : MinLeafSize
  401. };
  402. typedef NodeBase<std::pair<KeyT, KeyT>, ValT, LeafSize> LeafBase;
  403. enum {
  404. // Now that we have the leaf branching factor, compute the actual allocation
  405. // unit size by rounding up to a whole number of cache lines.
  406. AllocBytes = (sizeof(LeafBase) + CacheLineBytes-1) & ~(CacheLineBytes-1),
  407. // Determine the branching factor for branch nodes.
  408. BranchSize = AllocBytes /
  409. static_cast<unsigned>(sizeof(KeyT) + sizeof(void*))
  410. };
  411. /// Allocator - The recycling allocator used for both branch and leaf nodes.
  412. /// This typedef is very likely to be identical for all IntervalMaps with
  413. /// reasonably sized entries, so the same allocator can be shared among
  414. /// different kinds of maps.
  415. typedef RecyclingAllocator<BumpPtrAllocator, char,
  416. AllocBytes, CacheLineBytes> Allocator;
  417. };
  418. //===----------------------------------------------------------------------===//
  419. //--- IntervalMapImpl::NodeRef ---//
  420. //===----------------------------------------------------------------------===//
  421. //
  422. // B+-tree nodes can be leaves or branches, so we need a polymorphic node
  423. // pointer that can point to both kinds.
  424. //
  425. // All nodes are cache line aligned and the low 6 bits of a node pointer are
  426. // always 0. These bits are used to store the number of elements in the
  427. // referenced node. Besides saving space, placing node sizes in the parents
  428. // allow tree balancing algorithms to run without faulting cache lines for nodes
  429. // that may not need to be modified.
  430. //
  431. // A NodeRef doesn't know whether it references a leaf node or a branch node.
  432. // It is the responsibility of the caller to use the correct types.
  433. //
  434. // Nodes are never supposed to be empty, and it is invalid to store a node size
  435. // of 0 in a NodeRef. The valid range of sizes is 1-64.
  436. //
  437. //===----------------------------------------------------------------------===//
  438. class NodeRef {
  439. struct CacheAlignedPointerTraits {
  440. static inline void *getAsVoidPointer(void *P) { return P; }
  441. static inline void *getFromVoidPointer(void *P) { return P; }
  442. enum { NumLowBitsAvailable = Log2CacheLine };
  443. };
  444. PointerIntPair<void*, Log2CacheLine, unsigned, CacheAlignedPointerTraits> pip;
  445. public:
  446. /// NodeRef - Create a null ref.
  447. NodeRef() {}
  448. /// operator bool - Detect a null ref.
  449. operator bool() const { return pip.getOpaqueValue(); }
  450. /// NodeRef - Create a reference to the node p with n elements.
  451. template <typename NodeT>
  452. NodeRef(NodeT *p, unsigned n) : pip(p, n - 1) {
  453. assert(n <= NodeT::Capacity && "Size too big for node");
  454. }
  455. /// size - Return the number of elements in the referenced node.
  456. unsigned size() const { return pip.getInt() + 1; }
  457. /// setSize - Update the node size.
  458. void setSize(unsigned n) { pip.setInt(n - 1); }
  459. /// subtree - Access the i'th subtree reference in a branch node.
  460. /// This depends on branch nodes storing the NodeRef array as their first
  461. /// member.
  462. NodeRef &subtree(unsigned i) const {
  463. return reinterpret_cast<NodeRef*>(pip.getPointer())[i];
  464. }
  465. /// get - Dereference as a NodeT reference.
  466. template <typename NodeT>
  467. NodeT &get() const {
  468. return *reinterpret_cast<NodeT*>(pip.getPointer());
  469. }
  470. bool operator==(const NodeRef &RHS) const {
  471. if (pip == RHS.pip)
  472. return true;
  473. assert(pip.getPointer() != RHS.pip.getPointer() && "Inconsistent NodeRefs");
  474. return false;
  475. }
  476. bool operator!=(const NodeRef &RHS) const {
  477. return !operator==(RHS);
  478. }
  479. };
  480. //===----------------------------------------------------------------------===//
  481. //--- IntervalMapImpl::LeafNode ---//
  482. //===----------------------------------------------------------------------===//
  483. //
  484. // Leaf nodes store up to N disjoint intervals with corresponding values.
  485. //
  486. // The intervals are kept sorted and fully coalesced so there are no adjacent
  487. // intervals mapping to the same value.
  488. //
  489. // These constraints are always satisfied:
  490. //
  491. // - Traits::stopLess(start(i), stop(i)) - Non-empty, sane intervals.
  492. //
  493. // - Traits::stopLess(stop(i), start(i + 1) - Sorted.
  494. //
  495. // - value(i) != value(i + 1) || !Traits::adjacent(stop(i), start(i + 1))
  496. // - Fully coalesced.
  497. //
  498. //===----------------------------------------------------------------------===//
  499. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  500. class LeafNode : public NodeBase<std::pair<KeyT, KeyT>, ValT, N> {
  501. public:
  502. const KeyT &start(unsigned i) const { return this->first[i].first; }
  503. const KeyT &stop(unsigned i) const { return this->first[i].second; }
  504. const ValT &value(unsigned i) const { return this->second[i]; }
  505. KeyT &start(unsigned i) { return this->first[i].first; }
  506. KeyT &stop(unsigned i) { return this->first[i].second; }
  507. ValT &value(unsigned i) { return this->second[i]; }
  508. /// findFrom - Find the first interval after i that may contain x.
  509. /// @param i Starting index for the search.
  510. /// @param Size Number of elements in node.
  511. /// @param x Key to search for.
  512. /// @return First index with !stopLess(key[i].stop, x), or size.
  513. /// This is the first interval that can possibly contain x.
  514. unsigned findFrom(unsigned i, unsigned Size, KeyT x) const {
  515. assert(i <= Size && Size <= N && "Bad indices");
  516. assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
  517. "Index is past the needed point");
  518. while (i != Size && Traits::stopLess(stop(i), x)) ++i;
  519. return i;
  520. }
  521. /// safeFind - Find an interval that is known to exist. This is the same as
  522. /// findFrom except is it assumed that x is at least within range of the last
  523. /// interval.
  524. /// @param i Starting index for the search.
  525. /// @param x Key to search for.
  526. /// @return First index with !stopLess(key[i].stop, x), never size.
  527. /// This is the first interval that can possibly contain x.
  528. unsigned safeFind(unsigned i, KeyT x) const {
  529. assert(i < N && "Bad index");
  530. assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
  531. "Index is past the needed point");
  532. while (Traits::stopLess(stop(i), x)) ++i;
  533. assert(i < N && "Unsafe intervals");
  534. return i;
  535. }
  536. /// safeLookup - Lookup mapped value for a safe key.
  537. /// It is assumed that x is within range of the last entry.
  538. /// @param x Key to search for.
  539. /// @param NotFound Value to return if x is not in any interval.
  540. /// @return The mapped value at x or NotFound.
  541. ValT safeLookup(KeyT x, ValT NotFound) const {
  542. unsigned i = safeFind(0, x);
  543. return Traits::startLess(x, start(i)) ? NotFound : value(i);
  544. }
  545. unsigned insertFrom(unsigned &Pos, unsigned Size, KeyT a, KeyT b, ValT y);
  546. };
  547. /// insertFrom - Add mapping of [a;b] to y if possible, coalescing as much as
  548. /// possible. This may cause the node to grow by 1, or it may cause the node
  549. /// to shrink because of coalescing.
  550. /// @param i Starting index = insertFrom(0, size, a)
  551. /// @param Size Number of elements in node.
  552. /// @param a Interval start.
  553. /// @param b Interval stop.
  554. /// @param y Value be mapped.
  555. /// @return (insert position, new size), or (i, Capacity+1) on overflow.
  556. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  557. unsigned LeafNode<KeyT, ValT, N, Traits>::
  558. insertFrom(unsigned &Pos, unsigned Size, KeyT a, KeyT b, ValT y) {
  559. unsigned i = Pos;
  560. assert(i <= Size && Size <= N && "Invalid index");
  561. assert(!Traits::stopLess(b, a) && "Invalid interval");
  562. // Verify the findFrom invariant.
  563. assert((i == 0 || Traits::stopLess(stop(i - 1), a)));
  564. assert((i == Size || !Traits::stopLess(stop(i), a)));
  565. assert((i == Size || Traits::stopLess(b, start(i))) && "Overlapping insert");
  566. // Coalesce with previous interval.
  567. if (i && value(i - 1) == y && Traits::adjacent(stop(i - 1), a)) {
  568. Pos = i - 1;
  569. // Also coalesce with next interval?
  570. if (i != Size && value(i) == y && Traits::adjacent(b, start(i))) {
  571. stop(i - 1) = stop(i);
  572. this->erase(i, Size);
  573. return Size - 1;
  574. }
  575. stop(i - 1) = b;
  576. return Size;
  577. }
  578. // Detect overflow.
  579. if (i == N)
  580. return N + 1;
  581. // Add new interval at end.
  582. if (i == Size) {
  583. start(i) = a;
  584. stop(i) = b;
  585. value(i) = y;
  586. return Size + 1;
  587. }
  588. // Try to coalesce with following interval.
  589. if (value(i) == y && Traits::adjacent(b, start(i))) {
  590. start(i) = a;
  591. return Size;
  592. }
  593. // We must insert before i. Detect overflow.
  594. if (Size == N)
  595. return N + 1;
  596. // Insert before i.
  597. this->shift(i, Size);
  598. start(i) = a;
  599. stop(i) = b;
  600. value(i) = y;
  601. return Size + 1;
  602. }
  603. //===----------------------------------------------------------------------===//
  604. //--- IntervalMapImpl::BranchNode ---//
  605. //===----------------------------------------------------------------------===//
  606. //
  607. // A branch node stores references to 1--N subtrees all of the same height.
  608. //
  609. // The key array in a branch node holds the rightmost stop key of each subtree.
  610. // It is redundant to store the last stop key since it can be found in the
  611. // parent node, but doing so makes tree balancing a lot simpler.
  612. //
  613. // It is unusual for a branch node to only have one subtree, but it can happen
  614. // in the root node if it is smaller than the normal nodes.
  615. //
  616. // When all of the leaf nodes from all the subtrees are concatenated, they must
  617. // satisfy the same constraints as a single leaf node. They must be sorted,
  618. // sane, and fully coalesced.
  619. //
  620. //===----------------------------------------------------------------------===//
  621. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  622. class BranchNode : public NodeBase<NodeRef, KeyT, N> {
  623. public:
  624. const KeyT &stop(unsigned i) const { return this->second[i]; }
  625. const NodeRef &subtree(unsigned i) const { return this->first[i]; }
  626. KeyT &stop(unsigned i) { return this->second[i]; }
  627. NodeRef &subtree(unsigned i) { return this->first[i]; }
  628. /// findFrom - Find the first subtree after i that may contain x.
  629. /// @param i Starting index for the search.
  630. /// @param Size Number of elements in node.
  631. /// @param x Key to search for.
  632. /// @return First index with !stopLess(key[i], x), or size.
  633. /// This is the first subtree that can possibly contain x.
  634. unsigned findFrom(unsigned i, unsigned Size, KeyT x) const {
  635. assert(i <= Size && Size <= N && "Bad indices");
  636. assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
  637. "Index to findFrom is past the needed point");
  638. while (i != Size && Traits::stopLess(stop(i), x)) ++i;
  639. return i;
  640. }
  641. /// safeFind - Find a subtree that is known to exist. This is the same as
  642. /// findFrom except is it assumed that x is in range.
  643. /// @param i Starting index for the search.
  644. /// @param x Key to search for.
  645. /// @return First index with !stopLess(key[i], x), never size.
  646. /// This is the first subtree that can possibly contain x.
  647. unsigned safeFind(unsigned i, KeyT x) const {
  648. assert(i < N && "Bad index");
  649. assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
  650. "Index is past the needed point");
  651. while (Traits::stopLess(stop(i), x)) ++i;
  652. assert(i < N && "Unsafe intervals");
  653. return i;
  654. }
  655. /// safeLookup - Get the subtree containing x, Assuming that x is in range.
  656. /// @param x Key to search for.
  657. /// @return Subtree containing x
  658. NodeRef safeLookup(KeyT x) const {
  659. return subtree(safeFind(0, x));
  660. }
  661. /// insert - Insert a new (subtree, stop) pair.
  662. /// @param i Insert position, following entries will be shifted.
  663. /// @param Size Number of elements in node.
  664. /// @param Node Subtree to insert.
  665. /// @param Stop Last key in subtree.
  666. void insert(unsigned i, unsigned Size, NodeRef Node, KeyT Stop) {
  667. assert(Size < N && "branch node overflow");
  668. assert(i <= Size && "Bad insert position");
  669. this->shift(i, Size);
  670. subtree(i) = Node;
  671. stop(i) = Stop;
  672. }
  673. };
  674. //===----------------------------------------------------------------------===//
  675. //--- IntervalMapImpl::Path ---//
  676. //===----------------------------------------------------------------------===//
  677. //
  678. // A Path is used by iterators to represent a position in a B+-tree, and the
  679. // path to get there from the root.
  680. //
  681. // The Path class also contains the tree navigation code that doesn't have to
  682. // be templatized.
  683. //
  684. //===----------------------------------------------------------------------===//
  685. class Path {
  686. /// Entry - Each step in the path is a node pointer and an offset into that
  687. /// node.
  688. struct Entry {
  689. void *node;
  690. unsigned size;
  691. unsigned offset;
  692. Entry(void *Node, unsigned Size, unsigned Offset)
  693. : node(Node), size(Size), offset(Offset) {}
  694. Entry(NodeRef Node, unsigned Offset)
  695. : node(&Node.subtree(0)), size(Node.size()), offset(Offset) {}
  696. NodeRef &subtree(unsigned i) const {
  697. return reinterpret_cast<NodeRef*>(node)[i];
  698. }
  699. };
  700. /// path - The path entries, path[0] is the root node, path.back() is a leaf.
  701. SmallVector<Entry, 4> path;
  702. public:
  703. // Node accessors.
  704. template <typename NodeT> NodeT &node(unsigned Level) const {
  705. return *reinterpret_cast<NodeT*>(path[Level].node);
  706. }
  707. unsigned size(unsigned Level) const { return path[Level].size; }
  708. unsigned offset(unsigned Level) const { return path[Level].offset; }
  709. unsigned &offset(unsigned Level) { return path[Level].offset; }
  710. // Leaf accessors.
  711. template <typename NodeT> NodeT &leaf() const {
  712. return *reinterpret_cast<NodeT*>(path.back().node);
  713. }
  714. unsigned leafSize() const { return path.back().size; }
  715. unsigned leafOffset() const { return path.back().offset; }
  716. unsigned &leafOffset() { return path.back().offset; }
  717. /// valid - Return true if path is at a valid node, not at end().
  718. bool valid() const {
  719. return !path.empty() && path.front().offset < path.front().size;
  720. }
  721. /// height - Return the height of the tree corresponding to this path.
  722. /// This matches map->height in a full path.
  723. unsigned height() const { return path.size() - 1; }
  724. /// subtree - Get the subtree referenced from Level. When the path is
  725. /// consistent, node(Level + 1) == subtree(Level).
  726. /// @param Level 0..height-1. The leaves have no subtrees.
  727. NodeRef &subtree(unsigned Level) const {
  728. return path[Level].subtree(path[Level].offset);
  729. }
  730. /// reset - Reset cached information about node(Level) from subtree(Level -1).
  731. /// @param Level 1..height. THe node to update after parent node changed.
  732. void reset(unsigned Level) {
  733. path[Level] = Entry(subtree(Level - 1), offset(Level));
  734. }
  735. /// push - Add entry to path.
  736. /// @param Node Node to add, should be subtree(path.size()-1).
  737. /// @param Offset Offset into Node.
  738. void push(NodeRef Node, unsigned Offset) {
  739. path.push_back(Entry(Node, Offset));
  740. }
  741. /// pop - Remove the last path entry.
  742. void pop() {
  743. path.pop_back();
  744. }
  745. /// setSize - Set the size of a node both in the path and in the tree.
  746. /// @param Level 0..height. Note that setting the root size won't change
  747. /// map->rootSize.
  748. /// @param Size New node size.
  749. void setSize(unsigned Level, unsigned Size) {
  750. path[Level].size = Size;
  751. if (Level)
  752. subtree(Level - 1).setSize(Size);
  753. }
  754. /// setRoot - Clear the path and set a new root node.
  755. /// @param Node New root node.
  756. /// @param Size New root size.
  757. /// @param Offset Offset into root node.
  758. void setRoot(void *Node, unsigned Size, unsigned Offset) {
  759. path.clear();
  760. path.push_back(Entry(Node, Size, Offset));
  761. }
  762. /// replaceRoot - Replace the current root node with two new entries after the
  763. /// tree height has increased.
  764. /// @param Root The new root node.
  765. /// @param Size Number of entries in the new root.
  766. /// @param Offsets Offsets into the root and first branch nodes.
  767. void replaceRoot(void *Root, unsigned Size, IdxPair Offsets);
  768. /// getLeftSibling - Get the left sibling node at Level, or a null NodeRef.
  769. /// @param Level Get the sibling to node(Level).
  770. /// @return Left sibling, or NodeRef().
  771. NodeRef getLeftSibling(unsigned Level) const;
  772. /// moveLeft - Move path to the left sibling at Level. Leave nodes below Level
  773. /// unaltered.
  774. /// @param Level Move node(Level).
  775. void moveLeft(unsigned Level);
  776. /// fillLeft - Grow path to Height by taking leftmost branches.
  777. /// @param Height The target height.
  778. void fillLeft(unsigned Height) {
  779. while (height() < Height)
  780. push(subtree(height()), 0);
  781. }
  782. /// getLeftSibling - Get the left sibling node at Level, or a null NodeRef.
  783. /// @param Level Get the sinbling to node(Level).
  784. /// @return Left sibling, or NodeRef().
  785. NodeRef getRightSibling(unsigned Level) const;
  786. /// moveRight - Move path to the left sibling at Level. Leave nodes below
  787. /// Level unaltered.
  788. /// @param Level Move node(Level).
  789. void moveRight(unsigned Level);
  790. /// atBegin - Return true if path is at begin().
  791. bool atBegin() const {
  792. for (unsigned i = 0, e = path.size(); i != e; ++i)
  793. if (path[i].offset != 0)
  794. return false;
  795. return true;
  796. }
  797. /// atLastEntry - Return true if the path is at the last entry of the node at
  798. /// Level.
  799. /// @param Level Node to examine.
  800. bool atLastEntry(unsigned Level) const {
  801. return path[Level].offset == path[Level].size - 1;
  802. }
  803. /// legalizeForInsert - Prepare the path for an insertion at Level. When the
  804. /// path is at end(), node(Level) may not be a legal node. legalizeForInsert
  805. /// ensures that node(Level) is real by moving back to the last node at Level,
  806. /// and setting offset(Level) to size(Level) if required.
  807. /// @param Level The level where an insertion is about to take place.
  808. void legalizeForInsert(unsigned Level) {
  809. if (valid())
  810. return;
  811. moveLeft(Level);
  812. ++path[Level].offset;
  813. }
  814. };
  815. } // namespace IntervalMapImpl
  816. //===----------------------------------------------------------------------===//
  817. //--- IntervalMap ----//
  818. //===----------------------------------------------------------------------===//
  819. template <typename KeyT, typename ValT,
  820. unsigned N = IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
  821. typename Traits = IntervalMapInfo<KeyT> >
  822. class IntervalMap {
  823. typedef IntervalMapImpl::NodeSizer<KeyT, ValT> Sizer;
  824. typedef IntervalMapImpl::LeafNode<KeyT, ValT, Sizer::LeafSize, Traits> Leaf;
  825. typedef IntervalMapImpl::BranchNode<KeyT, ValT, Sizer::BranchSize, Traits>
  826. Branch;
  827. typedef IntervalMapImpl::LeafNode<KeyT, ValT, N, Traits> RootLeaf;
  828. typedef IntervalMapImpl::IdxPair IdxPair;
  829. // The RootLeaf capacity is given as a template parameter. We must compute the
  830. // corresponding RootBranch capacity.
  831. enum {
  832. DesiredRootBranchCap = (sizeof(RootLeaf) - sizeof(KeyT)) /
  833. (sizeof(KeyT) + sizeof(IntervalMapImpl::NodeRef)),
  834. RootBranchCap = DesiredRootBranchCap ? DesiredRootBranchCap : 1
  835. };
  836. typedef IntervalMapImpl::BranchNode<KeyT, ValT, RootBranchCap, Traits>
  837. RootBranch;
  838. // When branched, we store a global start key as well as the branch node.
  839. struct RootBranchData {
  840. KeyT start;
  841. RootBranch node;
  842. };
  843. enum {
  844. RootDataSize = sizeof(RootBranchData) > sizeof(RootLeaf) ?
  845. sizeof(RootBranchData) : sizeof(RootLeaf)
  846. };
  847. public:
  848. typedef typename Sizer::Allocator Allocator;
  849. typedef KeyT KeyType;
  850. typedef ValT ValueType;
  851. typedef Traits KeyTraits;
  852. private:
  853. // The root data is either a RootLeaf or a RootBranchData instance.
  854. // We can't put them in a union since C++03 doesn't allow non-trivial
  855. // constructors in unions.
  856. // Instead, we use a char array with pointer alignment. The alignment is
  857. // ensured by the allocator member in the class, but still verified in the
  858. // constructor. We don't support keys or values that are more aligned than a
  859. // pointer.
  860. char data[RootDataSize];
  861. // Tree height.
  862. // 0: Leaves in root.
  863. // 1: Root points to leaf.
  864. // 2: root->branch->leaf ...
  865. unsigned height;
  866. // Number of entries in the root node.
  867. unsigned rootSize;
  868. // Allocator used for creating external nodes.
  869. Allocator &allocator;
  870. /// dataAs - Represent data as a node type without breaking aliasing rules.
  871. template <typename T>
  872. T &dataAs() const {
  873. union {
  874. const char *d;
  875. T *t;
  876. } u;
  877. u.d = data;
  878. return *u.t;
  879. }
  880. const RootLeaf &rootLeaf() const {
  881. assert(!branched() && "Cannot acces leaf data in branched root");
  882. return dataAs<RootLeaf>();
  883. }
  884. RootLeaf &rootLeaf() {
  885. assert(!branched() && "Cannot acces leaf data in branched root");
  886. return dataAs<RootLeaf>();
  887. }
  888. RootBranchData &rootBranchData() const {
  889. assert(branched() && "Cannot access branch data in non-branched root");
  890. return dataAs<RootBranchData>();
  891. }
  892. RootBranchData &rootBranchData() {
  893. assert(branched() && "Cannot access branch data in non-branched root");
  894. return dataAs<RootBranchData>();
  895. }
  896. const RootBranch &rootBranch() const { return rootBranchData().node; }
  897. RootBranch &rootBranch() { return rootBranchData().node; }
  898. KeyT rootBranchStart() const { return rootBranchData().start; }
  899. KeyT &rootBranchStart() { return rootBranchData().start; }
  900. template <typename NodeT> NodeT *newNode() {
  901. return new(allocator.template Allocate<NodeT>()) NodeT();
  902. }
  903. template <typename NodeT> void deleteNode(NodeT *P) {
  904. P->~NodeT();
  905. allocator.Deallocate(P);
  906. }
  907. IdxPair branchRoot(unsigned Position);
  908. IdxPair splitRoot(unsigned Position);
  909. void switchRootToBranch() {
  910. rootLeaf().~RootLeaf();
  911. height = 1;
  912. new (&rootBranchData()) RootBranchData();
  913. }
  914. void switchRootToLeaf() {
  915. rootBranchData().~RootBranchData();
  916. height = 0;
  917. new(&rootLeaf()) RootLeaf();
  918. }
  919. bool branched() const { return height > 0; }
  920. ValT treeSafeLookup(KeyT x, ValT NotFound) const;
  921. void visitNodes(void (IntervalMap::*f)(IntervalMapImpl::NodeRef,
  922. unsigned Level));
  923. void deleteNode(IntervalMapImpl::NodeRef Node, unsigned Level);
  924. public:
  925. explicit IntervalMap(Allocator &a) : height(0), rootSize(0), allocator(a) {
  926. assert((uintptr_t(data) & (alignOf<RootLeaf>() - 1)) == 0 &&
  927. "Insufficient alignment");
  928. new(&rootLeaf()) RootLeaf();
  929. }
  930. ~IntervalMap() {
  931. clear();
  932. rootLeaf().~RootLeaf();
  933. }
  934. /// empty - Return true when no intervals are mapped.
  935. bool empty() const {
  936. return rootSize == 0;
  937. }
  938. /// start - Return the smallest mapped key in a non-empty map.
  939. KeyT start() const {
  940. assert(!empty() && "Empty IntervalMap has no start");
  941. return !branched() ? rootLeaf().start(0) : rootBranchStart();
  942. }
  943. /// stop - Return the largest mapped key in a non-empty map.
  944. KeyT stop() const {
  945. assert(!empty() && "Empty IntervalMap has no stop");
  946. return !branched() ? rootLeaf().stop(rootSize - 1) :
  947. rootBranch().stop(rootSize - 1);
  948. }
  949. /// lookup - Return the mapped value at x or NotFound.
  950. ValT lookup(KeyT x, ValT NotFound = ValT()) const {
  951. if (empty() || Traits::startLess(x, start()) || Traits::stopLess(stop(), x))
  952. return NotFound;
  953. return branched() ? treeSafeLookup(x, NotFound) :
  954. rootLeaf().safeLookup(x, NotFound);
  955. }
  956. /// insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
  957. /// It is assumed that no key in the interval is mapped to another value, but
  958. /// overlapping intervals already mapped to y will be coalesced.
  959. void insert(KeyT a, KeyT b, ValT y) {
  960. if (branched() || rootSize == RootLeaf::Capacity)
  961. return find(a).insert(a, b, y);
  962. // Easy insert into root leaf.
  963. unsigned p = rootLeaf().findFrom(0, rootSize, a);
  964. rootSize = rootLeaf().insertFrom(p, rootSize, a, b, y);
  965. }
  966. /// clear - Remove all entries.
  967. void clear();
  968. class const_iterator;
  969. class iterator;
  970. friend class const_iterator;
  971. friend class iterator;
  972. const_iterator begin() const {
  973. const_iterator I(*this);
  974. I.goToBegin();
  975. return I;
  976. }
  977. iterator begin() {
  978. iterator I(*this);
  979. I.goToBegin();
  980. return I;
  981. }
  982. const_iterator end() const {
  983. const_iterator I(*this);
  984. I.goToEnd();
  985. return I;
  986. }
  987. iterator end() {
  988. iterator I(*this);
  989. I.goToEnd();
  990. return I;
  991. }
  992. /// find - Return an iterator pointing to the first interval ending at or
  993. /// after x, or end().
  994. const_iterator find(KeyT x) const {
  995. const_iterator I(*this);
  996. I.find(x);
  997. return I;
  998. }
  999. iterator find(KeyT x) {
  1000. iterator I(*this);
  1001. I.find(x);
  1002. return I;
  1003. }
  1004. };
  1005. /// treeSafeLookup - Return the mapped value at x or NotFound, assuming a
  1006. /// branched root.
  1007. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1008. ValT IntervalMap<KeyT, ValT, N, Traits>::
  1009. treeSafeLookup(KeyT x, ValT NotFound) const {
  1010. assert(branched() && "treeLookup assumes a branched root");
  1011. IntervalMapImpl::NodeRef NR = rootBranch().safeLookup(x);
  1012. for (unsigned h = height-1; h; --h)
  1013. NR = NR.get<Branch>().safeLookup(x);
  1014. return NR.get<Leaf>().safeLookup(x, NotFound);
  1015. }
  1016. // branchRoot - Switch from a leaf root to a branched root.
  1017. // Return the new (root offset, node offset) corresponding to Position.
  1018. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1019. IntervalMapImpl::IdxPair IntervalMap<KeyT, ValT, N, Traits>::
  1020. branchRoot(unsigned Position) {
  1021. using namespace IntervalMapImpl;
  1022. // How many external leaf nodes to hold RootLeaf+1?
  1023. const unsigned Nodes = RootLeaf::Capacity / Leaf::Capacity + 1;
  1024. // Compute element distribution among new nodes.
  1025. unsigned size[Nodes];
  1026. IdxPair NewOffset(0, Position);
  1027. // Is is very common for the root node to be smaller than external nodes.
  1028. if (Nodes == 1)
  1029. size[0] = rootSize;
  1030. else
  1031. NewOffset = distribute(Nodes, rootSize, Leaf::Capacity, NULL, size,
  1032. Position, true);
  1033. // Allocate new nodes.
  1034. unsigned pos = 0;
  1035. NodeRef node[Nodes];
  1036. for (unsigned n = 0; n != Nodes; ++n) {
  1037. Leaf *L = newNode<Leaf>();
  1038. L->copy(rootLeaf(), pos, 0, size[n]);
  1039. node[n] = NodeRef(L, size[n]);
  1040. pos += size[n];
  1041. }
  1042. // Destroy the old leaf node, construct branch node instead.
  1043. switchRootToBranch();
  1044. for (unsigned n = 0; n != Nodes; ++n) {
  1045. rootBranch().stop(n) = node[n].template get<Leaf>().stop(size[n]-1);
  1046. rootBranch().subtree(n) = node[n];
  1047. }
  1048. rootBranchStart() = node[0].template get<Leaf>().start(0);
  1049. rootSize = Nodes;
  1050. return NewOffset;
  1051. }
  1052. // splitRoot - Split the current BranchRoot into multiple Branch nodes.
  1053. // Return the new (root offset, node offset) corresponding to Position.
  1054. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1055. IntervalMapImpl::IdxPair IntervalMap<KeyT, ValT, N, Traits>::
  1056. splitRoot(unsigned Position) {
  1057. using namespace IntervalMapImpl;
  1058. // How many external leaf nodes to hold RootBranch+1?
  1059. const unsigned Nodes = RootBranch::Capacity / Branch::Capacity + 1;
  1060. // Compute element distribution among new nodes.
  1061. unsigned Size[Nodes];
  1062. IdxPair NewOffset(0, Position);
  1063. // Is is very common for the root node to be smaller than external nodes.
  1064. if (Nodes == 1)
  1065. Size[0] = rootSize;
  1066. else
  1067. NewOffset = distribute(Nodes, rootSize, Leaf::Capacity, NULL, Size,
  1068. Position, true);
  1069. // Allocate new nodes.
  1070. unsigned Pos = 0;
  1071. NodeRef Node[Nodes];
  1072. for (unsigned n = 0; n != Nodes; ++n) {
  1073. Branch *B = newNode<Branch>();
  1074. B->copy(rootBranch(), Pos, 0, Size[n]);
  1075. Node[n] = NodeRef(B, Size[n]);
  1076. Pos += Size[n];
  1077. }
  1078. for (unsigned n = 0; n != Nodes; ++n) {
  1079. rootBranch().stop(n) = Node[n].template get<Branch>().stop(Size[n]-1);
  1080. rootBranch().subtree(n) = Node[n];
  1081. }
  1082. rootSize = Nodes;
  1083. ++height;
  1084. return NewOffset;
  1085. }
  1086. /// visitNodes - Visit each external node.
  1087. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1088. void IntervalMap<KeyT, ValT, N, Traits>::
  1089. visitNodes(void (IntervalMap::*f)(IntervalMapImpl::NodeRef, unsigned Height)) {
  1090. if (!branched())
  1091. return;
  1092. SmallVector<IntervalMapImpl::NodeRef, 4> Refs, NextRefs;
  1093. // Collect level 0 nodes from the root.
  1094. for (unsigned i = 0; i != rootSize; ++i)
  1095. Refs.push_back(rootBranch().subtree(i));
  1096. // Visit all branch nodes.
  1097. for (unsigned h = height - 1; h; --h) {
  1098. for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
  1099. for (unsigned j = 0, s = Refs[i].size(); j != s; ++j)
  1100. NextRefs.push_back(Refs[i].subtree(j));
  1101. (this->*f)(Refs[i], h);
  1102. }
  1103. Refs.clear();
  1104. Refs.swap(NextRefs);
  1105. }
  1106. // Visit all leaf nodes.
  1107. for (unsigned i = 0, e = Refs.size(); i != e; ++i)
  1108. (this->*f)(Refs[i], 0);
  1109. }
  1110. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1111. void IntervalMap<KeyT, ValT, N, Traits>::
  1112. deleteNode(IntervalMapImpl::NodeRef Node, unsigned Level) {
  1113. if (Level)
  1114. deleteNode(&Node.get<Branch>());
  1115. else
  1116. deleteNode(&Node.get<Leaf>());
  1117. }
  1118. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1119. void IntervalMap<KeyT, ValT, N, Traits>::
  1120. clear() {
  1121. if (branched()) {
  1122. visitNodes(&IntervalMap::deleteNode);
  1123. switchRootToLeaf();
  1124. }
  1125. rootSize = 0;
  1126. }
  1127. //===----------------------------------------------------------------------===//
  1128. //--- IntervalMap::const_iterator ----//
  1129. //===----------------------------------------------------------------------===//
  1130. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1131. class IntervalMap<KeyT, ValT, N, Traits>::const_iterator :
  1132. public std::iterator<std::bidirectional_iterator_tag, ValT> {
  1133. protected:
  1134. friend class IntervalMap;
  1135. // The map referred to.
  1136. IntervalMap *map;
  1137. // We store a full path from the root to the current position.
  1138. // The path may be partially filled, but never between iterator calls.
  1139. IntervalMapImpl::Path path;
  1140. explicit const_iterator(const IntervalMap &map) :
  1141. map(const_cast<IntervalMap*>(&map)) {}
  1142. bool branched() const {
  1143. assert(map && "Invalid iterator");
  1144. return map->branched();
  1145. }
  1146. void setRoot(unsigned Offset) {
  1147. if (branched())
  1148. path.setRoot(&map->rootBranch(), map->rootSize, Offset);
  1149. else
  1150. path.setRoot(&map->rootLeaf(), map->rootSize, Offset);
  1151. }
  1152. void pathFillFind(KeyT x);
  1153. void treeFind(KeyT x);
  1154. void treeAdvanceTo(KeyT x);
  1155. /// unsafeStart - Writable access to start() for iterator.
  1156. KeyT &unsafeStart() const {
  1157. assert(valid() && "Cannot access invalid iterator");
  1158. return branched() ? path.leaf<Leaf>().start(path.leafOffset()) :
  1159. path.leaf<RootLeaf>().start(path.leafOffset());
  1160. }
  1161. /// unsafeStop - Writable access to stop() for iterator.
  1162. KeyT &unsafeStop() const {
  1163. assert(valid() && "Cannot access invalid iterator");
  1164. return branched() ? path.leaf<Leaf>().stop(path.leafOffset()) :
  1165. path.leaf<RootLeaf>().stop(path.leafOffset());
  1166. }
  1167. /// unsafeValue - Writable access to value() for iterator.
  1168. ValT &unsafeValue() const {
  1169. assert(valid() && "Cannot access invalid iterator");
  1170. return branched() ? path.leaf<Leaf>().value(path.leafOffset()) :
  1171. path.leaf<RootLeaf>().value(path.leafOffset());
  1172. }
  1173. public:
  1174. /// const_iterator - Create an iterator that isn't pointing anywhere.
  1175. const_iterator() : map(0) {}
  1176. /// setMap - Change the map iterated over. This call must be followed by a
  1177. /// call to goToBegin(), goToEnd(), or find()
  1178. void setMap(const IntervalMap &m) { map = const_cast<IntervalMap*>(&m); }
  1179. /// valid - Return true if the current position is valid, false for end().
  1180. bool valid() const { return path.valid(); }
  1181. /// atBegin - Return true if the current position is the first map entry.
  1182. bool atBegin() const { return path.atBegin(); }
  1183. /// start - Return the beginning of the current interval.
  1184. const KeyT &start() const { return unsafeStart(); }
  1185. /// stop - Return the end of the current interval.
  1186. const KeyT &stop() const { return unsafeStop(); }
  1187. /// value - Return the mapped value at the current interval.
  1188. const ValT &value() const { return unsafeValue(); }
  1189. const ValT &operator*() const { return value(); }
  1190. bool operator==(const const_iterator &RHS) const {
  1191. assert(map == RHS.map && "Cannot compare iterators from different maps");
  1192. if (!valid())
  1193. return !RHS.valid();
  1194. if (path.leafOffset() != RHS.path.leafOffset())
  1195. return false;
  1196. return &path.template leaf<Leaf>() == &RHS.path.template leaf<Leaf>();
  1197. }
  1198. bool operator!=(const const_iterator &RHS) const {
  1199. return !operator==(RHS);
  1200. }
  1201. /// goToBegin - Move to the first interval in map.
  1202. void goToBegin() {
  1203. setRoot(0);
  1204. if (branched())
  1205. path.fillLeft(map->height);
  1206. }
  1207. /// goToEnd - Move beyond the last interval in map.
  1208. void goToEnd() {
  1209. setRoot(map->rootSize);
  1210. }
  1211. /// preincrement - move to the next interval.
  1212. const_iterator &operator++() {
  1213. assert(valid() && "Cannot increment end()");
  1214. if (++path.leafOffset() == path.leafSize() && branched())
  1215. path.moveRight(map->height);
  1216. return *this;
  1217. }
  1218. /// postincrement - Dont do that!
  1219. const_iterator operator++(int) {
  1220. const_iterator tmp = *this;
  1221. operator++();
  1222. return tmp;
  1223. }
  1224. /// predecrement - move to the previous interval.
  1225. const_iterator &operator--() {
  1226. if (path.leafOffset() && (valid() || !branched()))
  1227. --path.leafOffset();
  1228. else
  1229. path.moveLeft(map->height);
  1230. return *this;
  1231. }
  1232. /// postdecrement - Dont do that!
  1233. const_iterator operator--(int) {
  1234. const_iterator tmp = *this;
  1235. operator--();
  1236. return tmp;
  1237. }
  1238. /// find - Move to the first interval with stop >= x, or end().
  1239. /// This is a full search from the root, the current position is ignored.
  1240. void find(KeyT x) {
  1241. if (branched())
  1242. treeFind(x);
  1243. else
  1244. setRoot(map->rootLeaf().findFrom(0, map->rootSize, x));
  1245. }
  1246. /// advanceTo - Move to the first interval with stop >= x, or end().
  1247. /// The search is started from the current position, and no earlier positions
  1248. /// can be found. This is much faster than find() for small moves.
  1249. void advanceTo(KeyT x) {
  1250. if (!valid())
  1251. return;
  1252. if (branched())
  1253. treeAdvanceTo(x);
  1254. else
  1255. path.leafOffset() =
  1256. map->rootLeaf().findFrom(path.leafOffset(), map->rootSize, x);
  1257. }
  1258. };
  1259. /// pathFillFind - Complete path by searching for x.
  1260. /// @param x Key to search for.
  1261. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1262. void IntervalMap<KeyT, ValT, N, Traits>::
  1263. const_iterator::pathFillFind(KeyT x) {
  1264. IntervalMapImpl::NodeRef NR = path.subtree(path.height());
  1265. for (unsigned i = map->height - path.height() - 1; i; --i) {
  1266. unsigned p = NR.get<Branch>().safeFind(0, x);
  1267. path.push(NR, p);
  1268. NR = NR.subtree(p);
  1269. }
  1270. path.push(NR, NR.get<Leaf>().safeFind(0, x));
  1271. }
  1272. /// treeFind - Find in a branched tree.
  1273. /// @param x Key to search for.
  1274. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1275. void IntervalMap<KeyT, ValT, N, Traits>::
  1276. const_iterator::treeFind(KeyT x) {
  1277. setRoot(map->rootBranch().findFrom(0, map->rootSize, x));
  1278. if (valid())
  1279. pathFillFind(x);
  1280. }
  1281. /// treeAdvanceTo - Find position after the current one.
  1282. /// @param x Key to search for.
  1283. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1284. void IntervalMap<KeyT, ValT, N, Traits>::
  1285. const_iterator::treeAdvanceTo(KeyT x) {
  1286. // Can we stay on the same leaf node?
  1287. if (!Traits::stopLess(path.leaf<Leaf>().stop(path.leafSize() - 1), x)) {
  1288. path.leafOffset() = path.leaf<Leaf>().safeFind(path.leafOffset(), x);
  1289. return;
  1290. }
  1291. // Drop the current leaf.
  1292. path.pop();
  1293. // Search towards the root for a usable subtree.
  1294. if (path.height()) {
  1295. for (unsigned l = path.height() - 1; l; --l) {
  1296. if (!Traits::stopLess(path.node<Branch>(l).stop(path.offset(l)), x)) {
  1297. // The branch node at l+1 is usable
  1298. path.offset(l + 1) =
  1299. path.node<Branch>(l + 1).safeFind(path.offset(l + 1), x);
  1300. return pathFillFind(x);
  1301. }
  1302. path.pop();
  1303. }
  1304. // Is the level-1 Branch usable?
  1305. if (!Traits::stopLess(map->rootBranch().stop(path.offset(0)), x)) {
  1306. path.offset(1) = path.node<Branch>(1).safeFind(path.offset(1), x);
  1307. return pathFillFind(x);
  1308. }
  1309. }
  1310. // We reached the root.
  1311. setRoot(map->rootBranch().findFrom(path.offset(0), map->rootSize, x));
  1312. if (valid())
  1313. pathFillFind(x);
  1314. }
  1315. //===----------------------------------------------------------------------===//
  1316. //--- IntervalMap::iterator ----//
  1317. //===----------------------------------------------------------------------===//
  1318. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1319. class IntervalMap<KeyT, ValT, N, Traits>::iterator : public const_iterator {
  1320. friend class IntervalMap;
  1321. typedef IntervalMapImpl::IdxPair IdxPair;
  1322. explicit iterator(IntervalMap &map) : const_iterator(map) {}
  1323. void setNodeStop(unsigned Level, KeyT Stop);
  1324. bool insertNode(unsigned Level, IntervalMapImpl::NodeRef Node, KeyT Stop);
  1325. template <typename NodeT> bool overflow(unsigned Level);
  1326. void treeInsert(KeyT a, KeyT b, ValT y);
  1327. void eraseNode(unsigned Level);
  1328. void treeErase(bool UpdateRoot = true);
  1329. bool canCoalesceLeft(KeyT Start, ValT x);
  1330. bool canCoalesceRight(KeyT Stop, ValT x);
  1331. public:
  1332. /// iterator - Create null iterator.
  1333. iterator() {}
  1334. /// setStart - Move the start of the current interval.
  1335. /// This may cause coalescing with the previous interval.
  1336. /// @param a New start key, must not overlap the previous interval.
  1337. void setStart(KeyT a);
  1338. /// setStop - Move the end of the current interval.
  1339. /// This may cause coalescing with the following interval.
  1340. /// @param b New stop key, must not overlap the following interval.
  1341. void setStop(KeyT b);
  1342. /// setValue - Change the mapped value of the current interval.
  1343. /// This may cause coalescing with the previous and following intervals.
  1344. /// @param x New value.
  1345. void setValue(ValT x);
  1346. /// setStartUnchecked - Move the start of the current interval without
  1347. /// checking for coalescing or overlaps.
  1348. /// This should only be used when it is known that coalescing is not required.
  1349. /// @param a New start key.
  1350. void setStartUnchecked(KeyT a) { this->unsafeStart() = a; }
  1351. /// setStopUnchecked - Move the end of the current interval without checking
  1352. /// for coalescing or overlaps.
  1353. /// This should only be used when it is known that coalescing is not required.
  1354. /// @param b New stop key.
  1355. void setStopUnchecked(KeyT b) {
  1356. this->unsafeStop() = b;
  1357. // Update keys in branch nodes as well.
  1358. if (this->path.atLastEntry(this->path.height()))
  1359. setNodeStop(this->path.height(), b);
  1360. }
  1361. /// setValueUnchecked - Change the mapped value of the current interval
  1362. /// without checking for coalescing.
  1363. /// @param x New value.
  1364. void setValueUnchecked(ValT x) { this->unsafeValue() = x; }
  1365. /// insert - Insert mapping [a;b] -> y before the current position.
  1366. void insert(KeyT a, KeyT b, ValT y);
  1367. /// erase - Erase the current interval.
  1368. void erase();
  1369. iterator &operator++() {
  1370. const_iterator::operator++();
  1371. return *this;
  1372. }
  1373. iterator operator++(int) {
  1374. iterator tmp = *this;
  1375. operator++();
  1376. return tmp;
  1377. }
  1378. iterator &operator--() {
  1379. const_iterator::operator--();
  1380. return *this;
  1381. }
  1382. iterator operator--(int) {
  1383. iterator tmp = *this;
  1384. operator--();
  1385. return tmp;
  1386. }
  1387. };
  1388. /// canCoalesceLeft - Can the current interval coalesce to the left after
  1389. /// changing start or value?
  1390. /// @param Start New start of current interval.
  1391. /// @param Value New value for current interval.
  1392. /// @return True when updating the current interval would enable coalescing.
  1393. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1394. bool IntervalMap<KeyT, ValT, N, Traits>::
  1395. iterator::canCoalesceLeft(KeyT Start, ValT Value) {
  1396. using namespace IntervalMapImpl;
  1397. Path &P = this->path;
  1398. if (!this->branched()) {
  1399. unsigned i = P.leafOffset();
  1400. RootLeaf &Node = P.leaf<RootLeaf>();
  1401. return i && Node.value(i-1) == Value &&
  1402. Traits::adjacent(Node.stop(i-1), Start);
  1403. }
  1404. // Branched.
  1405. if (unsigned i = P.leafOffset()) {
  1406. Leaf &Node = P.leaf<Leaf>();
  1407. return Node.value(i-1) == Value && Traits::adjacent(Node.stop(i-1), Start);
  1408. } else if (NodeRef NR = P.getLeftSibling(P.height())) {
  1409. unsigned i = NR.size() - 1;
  1410. Leaf &Node = NR.get<Leaf>();
  1411. return Node.value(i) == Value && Traits::adjacent(Node.stop(i), Start);
  1412. }
  1413. return false;
  1414. }
  1415. /// canCoalesceRight - Can the current interval coalesce to the right after
  1416. /// changing stop or value?
  1417. /// @param Stop New stop of current interval.
  1418. /// @param Value New value for current interval.
  1419. /// @return True when updating the current interval would enable coalescing.
  1420. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1421. bool IntervalMap<KeyT, ValT, N, Traits>::
  1422. iterator::canCoalesceRight(KeyT Stop, ValT Value) {
  1423. using namespace IntervalMapImpl;
  1424. Path &P = this->path;
  1425. unsigned i = P.leafOffset() + 1;
  1426. if (!this->branched()) {
  1427. if (i >= P.leafSize())
  1428. return false;
  1429. RootLeaf &Node = P.leaf<RootLeaf>();
  1430. return Node.value(i) == Value && Traits::adjacent(Stop, Node.start(i));
  1431. }
  1432. // Branched.
  1433. if (i < P.leafSize()) {
  1434. Leaf &Node = P.leaf<Leaf>();
  1435. return Node.value(i) == Value && Traits::adjacent(Stop, Node.start(i));
  1436. } else if (NodeRef NR = P.getRightSibling(P.height())) {
  1437. Leaf &Node = NR.get<Leaf>();
  1438. return Node.value(0) == Value && Traits::adjacent(Stop, Node.start(0));
  1439. }
  1440. return false;
  1441. }
  1442. /// setNodeStop - Update the stop key of the current node at level and above.
  1443. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1444. void IntervalMap<KeyT, ValT, N, Traits>::
  1445. iterator::setNodeStop(unsigned Level, KeyT Stop) {
  1446. // There are no references to the root node, so nothing to update.
  1447. if (!Level)
  1448. return;
  1449. IntervalMapImpl::Path &P = this->path;
  1450. // Update nodes pointing to the current node.
  1451. while (--Level) {
  1452. P.node<Branch>(Level).stop(P.offset(Level)) = Stop;
  1453. if (!P.atLastEntry(Level))
  1454. return;
  1455. }
  1456. // Update root separately since it has a different layout.
  1457. P.node<RootBranch>(Level).stop(P.offset(Level)) = Stop;
  1458. }
  1459. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1460. void IntervalMap<KeyT, ValT, N, Traits>::
  1461. iterator::setStart(KeyT a) {
  1462. assert(Traits::stopLess(a, this->stop()) && "Cannot move start beyond stop");
  1463. KeyT &CurStart = this->unsafeStart();
  1464. if (!Traits::startLess(a, CurStart) || !canCoalesceLeft(a, this->value())) {
  1465. CurStart = a;
  1466. return;
  1467. }
  1468. // Coalesce with the interval to the left.
  1469. --*this;
  1470. a = this->start();
  1471. erase();
  1472. setStartUnchecked(a);
  1473. }
  1474. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1475. void IntervalMap<KeyT, ValT, N, Traits>::
  1476. iterator::setStop(KeyT b) {
  1477. assert(Traits::stopLess(this->start(), b) && "Cannot move stop beyond start");
  1478. if (Traits::startLess(b, this->stop()) ||
  1479. !canCoalesceRight(b, this->value())) {
  1480. setStopUnchecked(b);
  1481. return;
  1482. }
  1483. // Coalesce with interval to the right.
  1484. KeyT a = this->start();
  1485. erase();
  1486. setStartUnchecked(a);
  1487. }
  1488. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1489. void IntervalMap<KeyT, ValT, N, Traits>::
  1490. iterator::setValue(ValT x) {
  1491. setValueUnchecked(x);
  1492. if (canCoalesceRight(this->stop(), x)) {
  1493. KeyT a = this->start();
  1494. erase();
  1495. setStartUnchecked(a);
  1496. }
  1497. if (canCoalesceLeft(this->start(), x)) {
  1498. --*this;
  1499. KeyT a = this->start();
  1500. erase();
  1501. setStartUnchecked(a);
  1502. }
  1503. }
  1504. /// insertNode - insert a node before the current path at level.
  1505. /// Leave the current path pointing at the new node.
  1506. /// @param Level path index of the node to be inserted.
  1507. /// @param Node The node to be inserted.
  1508. /// @param Stop The last index in the new node.
  1509. /// @return True if the tree height was increased.
  1510. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1511. bool IntervalMap<KeyT, ValT, N, Traits>::
  1512. iterator::insertNode(unsigned Level, IntervalMapImpl::NodeRef Node, KeyT Stop) {
  1513. assert(Level && "Cannot insert next to the root");
  1514. bool SplitRoot = false;
  1515. IntervalMap &IM = *this->map;
  1516. IntervalMapImpl::Path &P = this->path;
  1517. if (Level == 1) {
  1518. // Insert into the root branch node.
  1519. if (IM.rootSize < RootBranch::Capacity) {
  1520. IM.rootBranch().insert(P.offset(0), IM.rootSize, Node, Stop);
  1521. P.setSize(0, ++IM.rootSize);
  1522. P.reset(Level);
  1523. return SplitRoot;
  1524. }
  1525. // We need to split the root while keeping our position.
  1526. SplitRoot = true;
  1527. IdxPair Offset = IM.splitRoot(P.offset(0));
  1528. P.replaceRoot(&IM.rootBranch(), IM.rootSize, Offset);
  1529. // Fall through to insert at the new higher level.
  1530. ++Level;
  1531. }
  1532. // When inserting before end(), make sure we have a valid path.
  1533. P.legalizeForInsert(--Level);
  1534. // Insert into the branch node at Level-1.
  1535. if (P.size(Level) == Branch::Capacity) {
  1536. // Branch node is full, handle handle the overflow.
  1537. assert(!SplitRoot && "Cannot overflow after splitting the root");
  1538. SplitRoot = overflow<Branch>(Level);
  1539. Level += SplitRoot;
  1540. }
  1541. P.node<Branch>(Level).insert(P.offset(Level), P.size(Level), Node, Stop);
  1542. P.setSize(Level, P.size(Level) + 1);
  1543. if (P.atLastEntry(Level))
  1544. setNodeStop(Level, Stop);
  1545. P.reset(Level + 1);
  1546. return SplitRoot;
  1547. }
  1548. // insert
  1549. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1550. void IntervalMap<KeyT, ValT, N, Traits>::
  1551. iterator::insert(KeyT a, KeyT b, ValT y) {
  1552. if (this->branched())
  1553. return treeInsert(a, b, y);
  1554. IntervalMap &IM = *this->map;
  1555. IntervalMapImpl::Path &P = this->path;
  1556. // Try simple root leaf insert.
  1557. unsigned Size = IM.rootLeaf().insertFrom(P.leafOffset(), IM.rootSize, a, b, y);
  1558. // Was the root node insert successful?
  1559. if (Size <= RootLeaf::Capacity) {
  1560. P.setSize(0, IM.rootSize = Size);
  1561. return;
  1562. }
  1563. // Root leaf node is full, we must branch.
  1564. IdxPair Offset = IM.branchRoot(P.leafOffset());
  1565. P.replaceRoot(&IM.rootBranch(), IM.rootSize, Offset);
  1566. // Now it fits in the new leaf.
  1567. treeInsert(a, b, y);
  1568. }
  1569. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1570. void IntervalMap<KeyT, ValT, N, Traits>::
  1571. iterator::treeInsert(KeyT a, KeyT b, ValT y) {
  1572. using namespace IntervalMapImpl;
  1573. Path &P = this->path;
  1574. if (!P.valid())
  1575. P.legalizeForInsert(this->map->height);
  1576. // Check if this insertion will extend the node to the left.
  1577. if (P.leafOffset() == 0 && Traits::startLess(a, P.leaf<Leaf>().start(0))) {
  1578. // Node is growing to the left, will it affect a left sibling node?
  1579. if (NodeRef Sib = P.getLeftSibling(P.height())) {
  1580. Leaf &SibLeaf = Sib.get<Leaf>();
  1581. unsigned SibOfs = Sib.size() - 1;
  1582. if (SibLeaf.value(SibOfs) == y &&
  1583. Traits::adjacent(SibLeaf.stop(SibOfs), a)) {
  1584. // This insertion will coalesce with the last entry in SibLeaf. We can
  1585. // handle it in two ways:
  1586. // 1. Extend SibLeaf.stop to b and be done, or
  1587. // 2. Extend a to SibLeaf, erase the SibLeaf entry and continue.
  1588. // We prefer 1., but need 2 when coalescing to the right as well.
  1589. Leaf &CurLeaf = P.leaf<Leaf>();
  1590. P.moveLeft(P.height());
  1591. if (Traits::stopLess(b, CurLeaf.start(0)) &&
  1592. (y != CurLeaf.value(0) || !Traits::adjacent(b, CurLeaf.start(0)))) {
  1593. // Easy, just extend SibLeaf and we're done.
  1594. setNodeStop(P.height(), SibLeaf.stop(SibOfs) = b);
  1595. return;
  1596. } else {
  1597. // We have both left and right coalescing. Erase the old SibLeaf entry
  1598. // and continue inserting the larger interval.
  1599. a = SibLeaf.start(SibOfs);
  1600. treeErase(/* UpdateRoot= */false);
  1601. }
  1602. }
  1603. } else {
  1604. // No left sibling means we are at begin(). Update cached bound.
  1605. this->map->rootBranchStart() = a;
  1606. }
  1607. }
  1608. // When we are inserting at the end of a leaf node, we must update stops.
  1609. unsigned Size = P.leafSize();
  1610. bool Grow = P.leafOffset() == Size;
  1611. Size = P.leaf<Leaf>().insertFrom(P.leafOffset(), Size, a, b, y);
  1612. // Leaf insertion unsuccessful? Overflow and try again.
  1613. if (Size > Leaf::Capacity) {
  1614. overflow<Leaf>(P.height());
  1615. Grow = P.leafOffset() == P.leafSize();
  1616. Size = P.leaf<Leaf>().insertFrom(P.leafOffset(), P.leafSize(), a, b, y);
  1617. assert(Size <= Leaf::Capacity && "overflow() didn't make room");
  1618. }
  1619. // Inserted, update offset and leaf size.
  1620. P.setSize(P.height(), Size);
  1621. // Insert was the last node entry, update stops.
  1622. if (Grow)
  1623. setNodeStop(P.height(), b);
  1624. }
  1625. /// erase - erase the current interval and move to the next position.
  1626. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1627. void IntervalMap<KeyT, ValT, N, Traits>::
  1628. iterator::erase() {
  1629. IntervalMap &IM = *this->map;
  1630. IntervalMapImpl::Path &P = this->path;
  1631. assert(P.valid() && "Cannot erase end()");
  1632. if (this->branched())
  1633. return treeErase();
  1634. IM.rootLeaf().erase(P.leafOffset(), IM.rootSize);
  1635. P.setSize(0, --IM.rootSize);
  1636. }
  1637. /// treeErase - erase() for a branched tree.
  1638. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1639. void IntervalMap<KeyT, ValT, N, Traits>::
  1640. iterator::treeErase(bool UpdateRoot) {
  1641. IntervalMap &IM = *this->map;
  1642. IntervalMapImpl::Path &P = this->path;
  1643. Leaf &Node = P.leaf<Leaf>();
  1644. // Nodes are not allowed to become empty.
  1645. if (P.leafSize() == 1) {
  1646. IM.deleteNode(&Node);
  1647. eraseNode(IM.height);
  1648. // Update rootBranchStart if we erased begin().
  1649. if (UpdateRoot && IM.branched() && P.valid() && P.atBegin())
  1650. IM.rootBranchStart() = P.leaf<Leaf>().start(0);
  1651. return;
  1652. }
  1653. // Erase current entry.
  1654. Node.erase(P.leafOffset(), P.leafSize());
  1655. unsigned NewSize = P.leafSize() - 1;
  1656. P.setSize(IM.height, NewSize);
  1657. // When we erase the last entry, update stop and move to a legal position.
  1658. if (P.leafOffset() == NewSize) {
  1659. setNodeStop(IM.height, Node.stop(NewSize - 1));
  1660. P.moveRight(IM.height);
  1661. } else if (UpdateRoot && P.atBegin())
  1662. IM.rootBranchStart() = P.leaf<Leaf>().start(0);
  1663. }
  1664. /// eraseNode - Erase the current node at Level from its parent and move path to
  1665. /// the first entry of the next sibling node.
  1666. /// The node must be deallocated by the caller.
  1667. /// @param Level 1..height, the root node cannot be erased.
  1668. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1669. void IntervalMap<KeyT, ValT, N, Traits>::
  1670. iterator::eraseNode(unsigned Level) {
  1671. assert(Level && "Cannot erase root node");
  1672. IntervalMap &IM = *this->map;
  1673. IntervalMapImpl::Path &P = this->path;
  1674. if (--Level == 0) {
  1675. IM.rootBranch().erase(P.offset(0), IM.rootSize);
  1676. P.setSize(0, --IM.rootSize);
  1677. // If this cleared the root, switch to height=0.
  1678. if (IM.empty()) {
  1679. IM.switchRootToLeaf();
  1680. this->setRoot(0);
  1681. return;
  1682. }
  1683. } else {
  1684. // Remove node ref from branch node at Level.
  1685. Branch &Parent = P.node<Branch>(Level);
  1686. if (P.size(Level) == 1) {
  1687. // Branch node became empty, remove it recursively.
  1688. IM.deleteNode(&Parent);
  1689. eraseNode(Level);
  1690. } else {
  1691. // Branch node won't become empty.
  1692. Parent.erase(P.offset(Level), P.size(Level));
  1693. unsigned NewSize = P.size(Level) - 1;
  1694. P.setSize(Level, NewSize);
  1695. // If we removed the last branch, update stop and move to a legal pos.
  1696. if (P.offset(Level) == NewSize) {
  1697. setNodeStop(Level, Parent.stop(NewSize - 1));
  1698. P.moveRight(Level);
  1699. }
  1700. }
  1701. }
  1702. // Update path cache for the new right sibling position.
  1703. if (P.valid()) {
  1704. P.reset(Level + 1);
  1705. P.offset(Level + 1) = 0;
  1706. }
  1707. }
  1708. /// overflow - Distribute entries of the current node evenly among
  1709. /// its siblings and ensure that the current node is not full.
  1710. /// This may require allocating a new node.
  1711. /// @param NodeT The type of node at Level (Leaf or Branch).
  1712. /// @param Level path index of the overflowing node.
  1713. /// @return True when the tree height was changed.
  1714. template <typename KeyT, typename ValT, unsigned N, typename Traits>
  1715. template <typename NodeT>
  1716. bool IntervalMap<KeyT, ValT, N, Traits>::
  1717. iterator::overflow(unsigned Level) {
  1718. using namespace IntervalMapImpl;
  1719. Path &P = this->path;
  1720. unsigned CurSize[4];
  1721. NodeT *Node[4];
  1722. unsigned Nodes = 0;
  1723. unsigned Elements = 0;
  1724. unsigned Offset = P.offset(Level);
  1725. // Do we have a left sibling?
  1726. NodeRef LeftSib = P.getLeftSibling(Level);
  1727. if (LeftSib) {
  1728. Offset += Elements = CurSize[Nodes] = LeftSib.size();
  1729. Node[Nodes++] = &LeftSib.get<NodeT>();
  1730. }
  1731. // Current node.
  1732. Elements += CurSize[Nodes] = P.size(Level);
  1733. Node[Nodes++] = &P.node<NodeT>(Level);
  1734. // Do we have a right sibling?
  1735. NodeRef RightSib = P.getRightSibling(Level);
  1736. if (RightSib) {
  1737. Elements += CurSize[Nodes] = RightSib.size();
  1738. Node[Nodes++] = &RightSib.get<NodeT>();
  1739. }
  1740. // Do we need to allocate a new node?
  1741. unsigned NewNode = 0;
  1742. if (Elements + 1 > Nodes * NodeT::Capacity) {
  1743. // Insert NewNode at the penultimate position, or after a single node.
  1744. NewNode = Nodes == 1 ? 1 : Nodes - 1;
  1745. CurSize[Nodes] = CurSize[NewNode];
  1746. Node[Nodes] = Node[NewNode];
  1747. CurSize[NewNode] = 0;
  1748. Node[NewNode] = this->map->template newNode<NodeT>();
  1749. ++Nodes;
  1750. }
  1751. // Compute the new element distribution.
  1752. unsigned NewSize[4];
  1753. IdxPair NewOffset = distribute(Nodes, Elements, NodeT::Capacity,
  1754. CurSize, NewSize, Offset, true);
  1755. adjustSiblingSizes(Node, Nodes, CurSize, NewSize);
  1756. // Move current location to the leftmost node.
  1757. if (LeftSib)
  1758. P.moveLeft(Level);
  1759. // Elements have been rearranged, now update node sizes and stops.
  1760. bool SplitRoot = false;
  1761. unsigned Pos = 0;
  1762. for (;;) {
  1763. KeyT Stop = Node[Pos]->stop(NewSize[Pos]-1);
  1764. if (NewNode && Pos == NewNode) {
  1765. SplitRoot = insertNode(Level, NodeRef(Node[Pos], NewSize[Pos]), Stop);
  1766. Level += SplitRoot;
  1767. } else {
  1768. P.setSize(Level, NewSize[Pos]);
  1769. setNodeStop(Level, Stop);
  1770. }
  1771. if (Pos + 1 == Nodes)
  1772. break;
  1773. P.moveRight(Level);
  1774. ++Pos;
  1775. }
  1776. // Where was I? Find NewOffset.
  1777. while(Pos != NewOffset.first) {
  1778. P.moveLeft(Level);
  1779. --Pos;
  1780. }
  1781. P.offset(Level) = NewOffset.second;
  1782. return SplitRoot;
  1783. }
  1784. //===----------------------------------------------------------------------===//
  1785. //--- IntervalMapOverlaps ----//
  1786. //===----------------------------------------------------------------------===//
  1787. /// IntervalMapOverlaps - Iterate over the overlaps of mapped intervals in two
  1788. /// IntervalMaps. The maps may be different, but the KeyT and Traits types
  1789. /// should be the same.
  1790. ///
  1791. /// Typical uses:
  1792. ///
  1793. /// 1. Test for overlap:
  1794. /// bool overlap = IntervalMapOverlaps(a, b).valid();
  1795. ///
  1796. /// 2. Enumerate overlaps:
  1797. /// for (IntervalMapOverlaps I(a, b); I.valid() ; ++I) { ... }
  1798. ///
  1799. template <typename MapA, typename MapB>
  1800. class IntervalMapOverlaps {
  1801. typedef typename MapA::KeyType KeyType;
  1802. typedef typename MapA::KeyTraits Traits;
  1803. typename MapA::const_iterator posA;
  1804. typename MapB::const_iterator posB;
  1805. /// advance - Move posA and posB forward until reaching an overlap, or until
  1806. /// either meets end.
  1807. /// Don't move the iterators if they are already overlapping.
  1808. void advance() {
  1809. if (!valid())
  1810. return;
  1811. if (Traits::stopLess(posA.stop(), posB.start())) {
  1812. // A ends before B begins. Catch up.
  1813. posA.advanceTo(posB.start());
  1814. if (!posA.valid() || !Traits::stopLess(posB.stop(), posA.start()))
  1815. return;
  1816. } else if (Traits::stopLess(posB.stop(), posA.start())) {
  1817. // B ends before A begins. Catch up.
  1818. posB.advanceTo(posA.start());
  1819. if (!posB.valid() || !Traits::stopLess(posA.stop(), posB.start()))
  1820. return;
  1821. } else
  1822. // Already overlapping.
  1823. return;
  1824. for (;;) {
  1825. // Make a.end > b.start.
  1826. posA.advanceTo(posB.start());
  1827. if (!posA.valid() || !Traits::stopLess(posB.stop(), posA.start()))
  1828. return;
  1829. // Make b.end > a.start.
  1830. posB.advanceTo(posA.start());
  1831. if (!posB.valid() || !Traits::stopLess(posA.stop(), posB.start()))
  1832. return;
  1833. }
  1834. }
  1835. public:
  1836. /// IntervalMapOverlaps - Create an iterator for the overlaps of a and b.
  1837. IntervalMapOverlaps(const MapA &a, const MapB &b)
  1838. : posA(b.empty() ? a.end() : a.find(b.start())),
  1839. posB(posA.valid() ? b.find(posA.start()) : b.end()) { advance(); }
  1840. /// valid - Return true if iterator is at an overlap.
  1841. bool valid() const {
  1842. return posA.valid() && posB.valid();
  1843. }
  1844. /// a - access the left hand side in the overlap.
  1845. const typename MapA::const_iterator &a() const { return posA; }
  1846. /// b - access the right hand side in the overlap.
  1847. const typename MapB::const_iterator &b() const { return posB; }
  1848. /// start - Beginning of the overlapping interval.
  1849. KeyType start() const {
  1850. KeyType ak = a().start();
  1851. KeyType bk = b().start();
  1852. return Traits::startLess(ak, bk) ? bk : ak;
  1853. }
  1854. /// stop - End of the overlapping interval.
  1855. KeyType stop() const {
  1856. KeyType ak = a().stop();
  1857. KeyType bk = b().stop();
  1858. return Traits::startLess(ak, bk) ? ak : bk;
  1859. }
  1860. /// skipA - Move to the next overlap that doesn't involve a().
  1861. void skipA() {
  1862. ++posA;
  1863. advance();
  1864. }
  1865. /// skipB - Move to the next overlap that doesn't involve b().
  1866. void skipB() {
  1867. ++posB;
  1868. advance();
  1869. }
  1870. /// Preincrement - Move to the next overlap.
  1871. IntervalMapOverlaps &operator++() {
  1872. // Bump the iterator that ends first. The other one may have more overlaps.
  1873. if (Traits::startLess(posB.stop(), posA.stop()))
  1874. skipB();
  1875. else
  1876. skipA();
  1877. return *this;
  1878. }
  1879. /// advanceTo - Move to the first overlapping interval with
  1880. /// stopLess(x, stop()).
  1881. void advanceTo(KeyType x) {
  1882. if (!valid())
  1883. return;
  1884. // Make sure advanceTo sees monotonic keys.
  1885. if (Traits::stopLess(posA.stop(), x))
  1886. posA.advanceTo(x);
  1887. if (Traits::stopLess(posB.stop(), x))
  1888. posB.advanceTo(x);
  1889. advance();
  1890. }
  1891. };
  1892. } // namespace llvm
  1893. #endif