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