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