Add test case with randomly ordered insertions, massive coalescing.
[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   /// 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 /// 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 /// distribute - Compute a new distribution of node elements after an overflow
352 /// or underflow. Reserve space for a new element at Position, and compute the
353 /// node that will hold Position after redistributing node elements.
354 ///
355 /// It is required that
356 ///
357 ///   Elements == sum(CurSize), and
358 ///   Elements + Grow <= Nodes * Capacity.
359 ///
360 /// NewSize[] will be filled in such that:
361 ///
362 ///   sum(NewSize) == Elements, and
363 ///   NewSize[i] <= Capacity.
364 ///
365 /// The returned index is the node where Position will go, so:
366 ///
367 ///   sum(NewSize[0..idx-1]) <= Position
368 ///   sum(NewSize[0..idx])   >= Position
369 ///
370 /// The last equality, sum(NewSize[0..idx]) == Position, can only happen when
371 /// Grow is set and NewSize[idx] == Capacity-1. The index points to the node
372 /// before the one holding the Position'th element where there is room for an
373 /// insertion.
374 ///
375 /// @param Nodes    The number of nodes.
376 /// @param Elements Total elements in all nodes.
377 /// @param Capacity The capacity of each node.
378 /// @param CurSize  Array[Nodes] of current node sizes, or NULL.
379 /// @param NewSize  Array[Nodes] to receive the new node sizes.
380 /// @param Position Insert position.
381 /// @param Grow     Reserve space for a new element at Position.
382 /// @return         (node, offset) for Position.
383 IdxPair distribute(unsigned Nodes, unsigned Elements, unsigned Capacity,
384                    const unsigned *CurSize, unsigned NewSize[],
385                    unsigned Position, bool Grow);
386
387
388 //===----------------------------------------------------------------------===//
389 //---                             NodeSizer                                ---//
390 //===----------------------------------------------------------------------===//
391 //
392 // Compute node sizes from key and value types.
393 //
394 // The branching factors are chosen to make nodes fit in three cache lines.
395 // This may not be possible if keys or values are very large. Such large objects
396 // are handled correctly, but a std::map would probably give better performance.
397 //
398 //===----------------------------------------------------------------------===//
399
400 enum {
401   // Cache line size. Most architectures have 32 or 64 byte cache lines.
402   // We use 64 bytes here because it provides good branching factors.
403   Log2CacheLine = 6,
404   CacheLineBytes = 1 << Log2CacheLine,
405   DesiredNodeBytes = 3 * CacheLineBytes
406 };
407
408 template <typename KeyT, typename ValT>
409 struct NodeSizer {
410   enum {
411     // Compute the leaf node branching factor that makes a node fit in three
412     // cache lines. The branching factor must be at least 3, or some B+-tree
413     // balancing algorithms won't work.
414     // LeafSize can't be larger than CacheLineBytes. This is required by the
415     // PointerIntPair used by NodeRef.
416     DesiredLeafSize = DesiredNodeBytes /
417       static_cast<unsigned>(2*sizeof(KeyT)+sizeof(ValT)),
418     MinLeafSize = 3,
419     LeafSize = DesiredLeafSize > MinLeafSize ? DesiredLeafSize : MinLeafSize
420   };
421
422   typedef NodeBase<std::pair<KeyT, KeyT>, ValT, LeafSize> LeafBase;
423
424   enum {
425     // Now that we have the leaf branching factor, compute the actual allocation
426     // unit size by rounding up to a whole number of cache lines.
427     AllocBytes = (sizeof(LeafBase) + CacheLineBytes-1) & ~(CacheLineBytes-1),
428
429     // Determine the branching factor for branch nodes.
430     BranchSize = AllocBytes /
431       static_cast<unsigned>(sizeof(KeyT) + sizeof(void*))
432   };
433
434   /// Allocator - The recycling allocator used for both branch and leaf nodes.
435   /// This typedef is very likely to be identical for all IntervalMaps with
436   /// reasonably sized entries, so the same allocator can be shared among
437   /// different kinds of maps.
438   typedef RecyclingAllocator<BumpPtrAllocator, char,
439                              AllocBytes, CacheLineBytes> Allocator;
440
441 };
442
443
444 //===----------------------------------------------------------------------===//
445 //---                              NodeRef                                 ---//
446 //===----------------------------------------------------------------------===//
447 //
448 // B+-tree nodes can be leaves or branches, so we need a polymorphic node
449 // pointer that can point to both kinds.
450 //
451 // All nodes are cache line aligned and the low 6 bits of a node pointer are
452 // always 0. These bits are used to store the number of elements in the
453 // referenced node. Besides saving space, placing node sizes in the parents
454 // allow tree balancing algorithms to run without faulting cache lines for nodes
455 // that may not need to be modified.
456 //
457 // A NodeRef doesn't know whether it references a leaf node or a branch node.
458 // It is the responsibility of the caller to use the correct types.
459 //
460 // Nodes are never supposed to be empty, and it is invalid to store a node size
461 // of 0 in a NodeRef. The valid range of sizes is 1-64.
462 //
463 //===----------------------------------------------------------------------===//
464
465 struct CacheAlignedPointerTraits {
466   static inline void *getAsVoidPointer(void *P) { return P; }
467   static inline void *getFromVoidPointer(void *P) { return P; }
468   enum { NumLowBitsAvailable = Log2CacheLine };
469 };
470
471 class NodeRef {
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 //---                            Leaf nodes                                ---//
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   IdxPair insertFrom(unsigned i, unsigned Size, KeyT a, KeyT b, ValT y);
590   unsigned extendStop(unsigned i, unsigned Size, KeyT b);
591
592 #ifndef NDEBUG
593   void dump(raw_ostream &OS, unsigned Size) {
594     OS << "  N" << this << " [shape=record label=\"{ " << Size << '/' << N;
595     for (unsigned i = 0; i != Size; ++i)
596       OS << " | {" << start(i) << '-' << stop(i) << "|" << value(i) << '}';
597     OS << "}\"];\n";
598   }
599 #endif
600
601 };
602
603 /// insertFrom - Add mapping of [a;b] to y if possible, coalescing as much as
604 /// possible. This may cause the node to grow by 1, or it may cause the node
605 /// to shrink because of coalescing.
606 /// @param i    Starting index = insertFrom(0, size, a)
607 /// @param Size Number of elements in node.
608 /// @param a    Interval start.
609 /// @param b    Interval stop.
610 /// @param y    Value be mapped.
611 /// @return     (insert position, new size), or (i, Capacity+1) on overflow.
612 template <typename KeyT, typename ValT, unsigned N, typename Traits>
613 IdxPair LeafNode<KeyT, ValT, N, Traits>::
614 insertFrom(unsigned i, unsigned Size, KeyT a, KeyT b, ValT y) {
615   assert(i <= Size && Size <= N && "Invalid index");
616   assert(!Traits::stopLess(b, a) && "Invalid interval");
617
618   // Verify the findFrom invariant.
619   assert((i == 0 || Traits::stopLess(stop(i - 1), a)));
620   assert((i == Size || !Traits::stopLess(stop(i), a)));
621
622   // Coalesce with previous interval.
623   if (i && value(i - 1) == y && Traits::adjacent(stop(i - 1), a))
624     return IdxPair(i - 1, extendStop(i - 1, Size, b));
625
626   // Detect overflow.
627   if (i == N)
628     return IdxPair(i, 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 IdxPair(i, Size + 1);
636   }
637
638   // Overlapping intervals?
639   if (!Traits::stopLess(b, start(i))) {
640     assert(value(i) == y && "Inconsistent values in overlapping intervals");
641     if (Traits::startLess(a, start(i)))
642       start(i) = a;
643     return IdxPair(i, extendStop(i, Size, b));
644   }
645
646   // Try to coalesce with following interval.
647   if (value(i) == y && Traits::adjacent(b, start(i))) {
648     start(i) = a;
649     return IdxPair(i, Size);
650   }
651
652   // We must insert before i. Detect overflow.
653   if (Size == N)
654     return IdxPair(i, N + 1);
655
656   // Insert before i.
657   this->shift(i, Size);
658   start(i) = a;
659   stop(i) = b;
660   value(i) = y;
661   return IdxPair(i, Size + 1);
662 }
663
664 /// extendStop - Extend stop(i) to b, coalescing with following intervals.
665 /// @param i    Interval to extend.
666 /// @param Size Number of elements in node.
667 /// @param b    New interval end point.
668 /// @return     New node size after coalescing.
669 template <typename KeyT, typename ValT, unsigned N, typename Traits>
670 unsigned LeafNode<KeyT, ValT, N, Traits>::
671 extendStop(unsigned i, unsigned Size, KeyT b) {
672   assert(i < Size && Size <= N && "Bad indices");
673
674   // Are we even extending the interval?
675   if (Traits::startLess(b, stop(i)))
676     return Size;
677
678   // Find the first interval that may be preserved.
679   unsigned j = findFrom(i + 1, Size, b);
680   if (j < Size) {
681     // Would key[i] overlap key[j] after the extension?
682     if (Traits::stopLess(b, start(j))) {
683       // Not overlapping. Perhaps adjacent and coalescable?
684       if (value(i) == value(j) && Traits::adjacent(b, start(j)))
685         b = stop(j++);
686     } else {
687       // Overlap. Include key[j] in the new interval.
688       assert(value(i) == value(j) && "Overlapping values");
689       b = stop(j++);
690     }
691   }
692   stop(i) =  b;
693
694   // Entries [i+1;j) were coalesced.
695   if (i + 1 < j && j < Size)
696     this->erase(i + 1, j, Size);
697   return Size - (j - (i + 1));
698 }
699
700
701 //===----------------------------------------------------------------------===//
702 //---                             Branch nodes                             ---//
703 //===----------------------------------------------------------------------===//
704 //
705 // A branch node stores references to 1--N subtrees all of the same height.
706 //
707 // The key array in a branch node holds the rightmost stop key of each subtree.
708 // It is redundant to store the last stop key since it can be found in the
709 // parent node, but doing so makes tree balancing a lot simpler.
710 //
711 // It is unusual for a branch node to only have one subtree, but it can happen
712 // in the root node if it is smaller than the normal nodes.
713 //
714 // When all of the leaf nodes from all the subtrees are concatenated, they must
715 // satisfy the same constraints as a single leaf node. They must be sorted,
716 // sane, and fully coalesced.
717 //
718 //===----------------------------------------------------------------------===//
719
720 template <typename KeyT, typename ValT, unsigned N, typename Traits>
721 class BranchNode : public NodeBase<NodeRef, KeyT, N> {
722 public:
723   const KeyT &stop(unsigned i) const { return this->second[i]; }
724   const NodeRef &subtree(unsigned i) const { return this->first[i]; }
725
726   KeyT &stop(unsigned i) { return this->second[i]; }
727   NodeRef &subtree(unsigned i) { return this->first[i]; }
728
729   /// findFrom - Find the first subtree after i that may contain x.
730   /// @param i    Starting index for the search.
731   /// @param Size Number of elements in node.
732   /// @param x    Key to search for.
733   /// @return     First index with !stopLess(key[i], x), or size.
734   ///             This is the first subtree that can possibly contain x.
735   unsigned findFrom(unsigned i, unsigned Size, KeyT x) const {
736     assert(i <= Size && Size <= N && "Bad indices");
737     assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
738            "Index to findFrom is past the needed point");
739     while (i != Size && Traits::stopLess(stop(i), x)) ++i;
740     return i;
741   }
742
743   /// safeFind - Find a subtree that is known to exist. This is the same as
744   /// findFrom except is it assumed that x is in range.
745   /// @param i Starting index for the search.
746   /// @param x Key to search for.
747   /// @return  First index with !stopLess(key[i], x), never size.
748   ///          This is the first subtree that can possibly contain x.
749   unsigned safeFind(unsigned i, KeyT x) const {
750     assert(i < N && "Bad index");
751     assert((i == 0 || Traits::stopLess(stop(i - 1), x)) &&
752            "Index is past the needed point");
753     while (Traits::stopLess(stop(i), x)) ++i;
754     assert(i < N && "Unsafe intervals");
755     return i;
756   }
757
758   /// safeLookup - Get the subtree containing x, Assuming that x is in range.
759   /// @param x Key to search for.
760   /// @return  Subtree containing x
761   NodeRef safeLookup(KeyT x) const {
762     return subtree(safeFind(0, x));
763   }
764
765   /// insert - Insert a new (subtree, stop) pair.
766   /// @param i    Insert position, following entries will be shifted.
767   /// @param Size Number of elements in node.
768   /// @param Node Subtree to insert.
769   /// @param Stop Last key in subtree.
770   void insert(unsigned i, unsigned Size, NodeRef Node, KeyT Stop) {
771     assert(Size < N && "branch node overflow");
772     assert(i <= Size && "Bad insert position");
773     this->shift(i, Size);
774     subtree(i) = Node;
775     stop(i) = Stop;
776   }
777
778 #ifndef NDEBUG
779   void dump(raw_ostream &OS, unsigned Size) {
780     OS << "  N" << this << " [shape=record label=\"" << Size << '/' << N;
781     for (unsigned i = 0; i != Size; ++i)
782       OS << " | <s" << i << "> " << stop(i);
783     OS << "\"];\n";
784     for (unsigned i = 0; i != Size; ++i)
785       OS << "  N" << this << ":s" << i << " -> N"
786          << &subtree(i).template get<BranchNode>() << ";\n";
787   }
788 #endif
789
790 };
791
792 //===----------------------------------------------------------------------===//
793 //---                                  Path                                ---//
794 //===----------------------------------------------------------------------===//
795 //
796 // A Path is used by iterators to represent a position in a B+-tree, and the
797 // path to get there from the root.
798 //
799 // The Path class also constains the tree navigation code that doesn't have to
800 // be templatized.
801 //
802 //===----------------------------------------------------------------------===//
803
804 class Path {
805   /// Entry - Each step in the path is a node pointer and an offset into that
806   /// node.
807   struct Entry {
808     void *node;
809     unsigned size;
810     unsigned offset;
811
812     Entry(void *Node, unsigned Size, unsigned Offset)
813       : node(Node), size(Size), offset(Offset) {}
814
815     Entry(NodeRef Node, unsigned Offset)
816       : node(&Node.subtree(0)), size(Node.size()), offset(Offset) {}
817
818     NodeRef &subtree(unsigned i) const {
819       return reinterpret_cast<NodeRef*>(node)[i];
820     }
821   };
822
823   /// path - The path entries, path[0] is the root node, path.back() is a leaf.
824   SmallVector<Entry, 4> path;
825
826 public:
827   // Node accessors.
828   template <typename NodeT> NodeT &node(unsigned Level) const {
829     return *reinterpret_cast<NodeT*>(path[Level].node);
830   }
831   unsigned size(unsigned Level) const { return path[Level].size; }
832   unsigned offset(unsigned Level) const { return path[Level].offset; }
833   unsigned &offset(unsigned Level) { return path[Level].offset; }
834
835   // Leaf accessors.
836   template <typename NodeT> NodeT &leaf() const {
837     return *reinterpret_cast<NodeT*>(path.back().node);
838   }
839   unsigned leafSize() const { return path.back().size; }
840   unsigned leafOffset() const { return path.back().offset; }
841   unsigned &leafOffset() { return path.back().offset; }
842
843   /// valid - Return true if path is at a valid node, not at end().
844   bool valid() const {
845     return !path.empty() && path.front().offset < path.front().size;
846   }
847
848   /// height - Return the height of the tree corresponding to this path.
849   /// This matches map->height in a full path.
850   unsigned height() const { return path.size() - 1; }
851
852   /// subtree - Get the subtree referenced from Level. When the path is
853   /// consistent, node(Level + 1) == subtree(Level).
854   /// @param Level 0..height-1. The leaves have no subtrees.
855   NodeRef &subtree(unsigned Level) const {
856     return path[Level].subtree(path[Level].offset);
857   }
858
859   /// reset - Reset cached information about node(Level) from subtree(Level -1).
860   /// @param Level 1..height. THe node to update after parent node changed.
861   void reset(unsigned Level) {
862     path[Level] = Entry(subtree(Level - 1), offset(Level));
863   }
864
865   /// push - Add entry to path.
866   /// @param Node Node to add, should be subtree(path.size()-1).
867   /// @param Offset Offset into Node.
868   void push(NodeRef Node, unsigned Offset) {
869     path.push_back(Entry(Node, Offset));
870   }
871
872   /// setSize - Set the size of a node both in the path and in the tree.
873   /// @param Level 0..height. Note that setting the root size won't change
874   ///              map->rootSize.
875   /// @param Size New node size.
876   void setSize(unsigned Level, unsigned Size) {
877     path[Level].size = Size;
878     if (Level)
879       subtree(Level - 1).setSize(Size);
880   }
881
882   /// setRoot - Clear the path and set a new root node.
883   /// @param Node New root node.
884   /// @param Size New root size.
885   /// @param Offset Offset into root node.
886   void setRoot(void *Node, unsigned Size, unsigned Offset) {
887     path.clear();
888     path.push_back(Entry(Node, Size, Offset));
889   }
890
891   /// replaceRoot - Replace the current root node with two new entries after the
892   /// tree height has increased.
893   /// @param Root The new root node.
894   /// @param Size Number of entries in the new root.
895   /// @param Offsets Offsets into the root and first branch nodes.
896   void replaceRoot(void *Root, unsigned Size, IdxPair Offsets);
897
898   /// getLeftSibling - Get the left sibling node at Level, or a null NodeRef.
899   /// @param Level Get the sibling to node(Level).
900   /// @return Left sibling, or NodeRef().
901   NodeRef getLeftSibling(unsigned Level) const;
902
903   /// moveLeft - Move path to the left sibling at Level. Leave nodes below Level
904   /// unaltered.
905   /// @param Level Move node(Level).
906   void moveLeft(unsigned Level);
907
908   /// fillLeft - Grow path to Height by taking leftmost branches.
909   /// @param Height The target height.
910   void fillLeft(unsigned Height) {
911     while (height() < Height)
912       push(subtree(height()), 0);
913   }
914
915   /// getLeftSibling - Get the left sibling node at Level, or a null NodeRef.
916   /// @param Level Get the sinbling to node(Level).
917   /// @return Left sibling, or NodeRef().
918   NodeRef getRightSibling(unsigned Level) const;
919
920   /// moveRight - Move path to the left sibling at Level. Leave nodes below
921   /// Level unaltered.
922   /// @param Level Move node(Level).
923   void moveRight(unsigned Level);
924
925   /// atLastBranch - Return true if the path is at the last branch of the node
926   /// at Level.
927   /// @param Level Node to examine.
928   bool atLastBranch(unsigned Level) const {
929     return path[Level].offset == path[Level].size - 1;
930   }
931
932   /// legalizeForInsert - Prepare the path for an insertion at Level. When the
933   /// path is at end(), node(Level) may not be a legal node. legalizeForInsert
934   /// ensures that node(Level) is real by moving back to the last node at Level,
935   /// and setting offset(Level) to size(Level) if required.
936   /// @param Level The level where an insertion is about to take place.
937   void legalizeForInsert(unsigned Level) {
938     if (valid())
939       return;
940     moveLeft(Level);
941     ++path[Level].offset;
942   }
943
944 #ifndef NDEBUG
945   void dump() const {
946     for (unsigned l = 0, e = path.size(); l != e; ++l)
947       errs() << l << ": " << path[l].node << ' ' << path[l].size << ' '
948              << path[l].offset << '\n';
949   }
950 #endif
951 };
952
953 } // namespace IntervalMapImpl
954
955
956 //===----------------------------------------------------------------------===//
957 //---                          IntervalMap                                ----//
958 //===----------------------------------------------------------------------===//
959
960 template <typename KeyT, typename ValT,
961           unsigned N = IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
962           typename Traits = IntervalMapInfo<KeyT> >
963 class IntervalMap {
964   typedef IntervalMapImpl::NodeSizer<KeyT, ValT> Sizer;
965   typedef IntervalMapImpl::LeafNode<KeyT, ValT, Sizer::LeafSize, Traits> Leaf;
966   typedef IntervalMapImpl::BranchNode<KeyT, ValT, Sizer::BranchSize, Traits>
967     Branch;
968   typedef IntervalMapImpl::LeafNode<KeyT, ValT, N, Traits> RootLeaf;
969   typedef IntervalMapImpl::IdxPair IdxPair;
970
971   // The RootLeaf capacity is given as a template parameter. We must compute the
972   // corresponding RootBranch capacity.
973   enum {
974     DesiredRootBranchCap = (sizeof(RootLeaf) - sizeof(KeyT)) /
975       (sizeof(KeyT) + sizeof(IntervalMapImpl::NodeRef)),
976     RootBranchCap = DesiredRootBranchCap ? DesiredRootBranchCap : 1
977   };
978
979   typedef IntervalMapImpl::BranchNode<KeyT, ValT, RootBranchCap, Traits>
980     RootBranch;
981
982   // When branched, we store a global start key as well as the branch node.
983   struct RootBranchData {
984     KeyT start;
985     RootBranch node;
986   };
987
988   enum {
989     RootDataSize = sizeof(RootBranchData) > sizeof(RootLeaf) ?
990                    sizeof(RootBranchData) : sizeof(RootLeaf)
991   };
992
993 public:
994   typedef typename Sizer::Allocator Allocator;
995
996 private:
997   // The root data is either a RootLeaf or a RootBranchData instance.
998   // We can't put them in a union since C++03 doesn't allow non-trivial
999   // constructors in unions.
1000   // Instead, we use a char array with pointer alignment. The alignment is
1001   // ensured by the allocator member in the class, but still verified in the
1002   // constructor. We don't support keys or values that are more aligned than a
1003   // pointer.
1004   char data[RootDataSize];
1005
1006   // Tree height.
1007   // 0: Leaves in root.
1008   // 1: Root points to leaf.
1009   // 2: root->branch->leaf ...
1010   unsigned height;
1011
1012   // Number of entries in the root node.
1013   unsigned rootSize;
1014
1015   // Allocator used for creating external nodes.
1016   Allocator &allocator;
1017
1018   /// dataAs - Represent data as a node type without breaking aliasing rules.
1019   template <typename T>
1020   T &dataAs() const {
1021     union {
1022       const char *d;
1023       T *t;
1024     } u;
1025     u.d = data;
1026     return *u.t;
1027   }
1028
1029   const RootLeaf &rootLeaf() const {
1030     assert(!branched() && "Cannot acces leaf data in branched root");
1031     return dataAs<RootLeaf>();
1032   }
1033   RootLeaf &rootLeaf() {
1034     assert(!branched() && "Cannot acces leaf data in branched root");
1035     return dataAs<RootLeaf>();
1036   }
1037   RootBranchData &rootBranchData() const {
1038     assert(branched() && "Cannot access branch data in non-branched root");
1039     return dataAs<RootBranchData>();
1040   }
1041   RootBranchData &rootBranchData() {
1042     assert(branched() && "Cannot access branch data in non-branched root");
1043     return dataAs<RootBranchData>();
1044   }
1045   const RootBranch &rootBranch() const { return rootBranchData().node; }
1046   RootBranch &rootBranch()             { return rootBranchData().node; }
1047   KeyT rootBranchStart() const { return rootBranchData().start; }
1048   KeyT &rootBranchStart()      { return rootBranchData().start; }
1049
1050   template <typename NodeT> NodeT *newNode() {
1051     return new(allocator.template Allocate<NodeT>()) NodeT();
1052   }
1053
1054   template <typename NodeT> void deleteNode(NodeT *P) {
1055     P->~NodeT();
1056     allocator.Deallocate(P);
1057   }
1058
1059   IdxPair branchRoot(unsigned Position);
1060   IdxPair splitRoot(unsigned Position);
1061
1062   void switchRootToBranch() {
1063     rootLeaf().~RootLeaf();
1064     height = 1;
1065     new (&rootBranchData()) RootBranchData();
1066   }
1067
1068   void switchRootToLeaf() {
1069     rootBranchData().~RootBranchData();
1070     height = 0;
1071     new(&rootLeaf()) RootLeaf();
1072   }
1073
1074   bool branched() const { return height > 0; }
1075
1076   ValT treeSafeLookup(KeyT x, ValT NotFound) const;
1077   void visitNodes(void (IntervalMap::*f)(IntervalMapImpl::NodeRef,
1078                   unsigned Level));
1079   void deleteNode(IntervalMapImpl::NodeRef Node, unsigned Level);
1080
1081 public:
1082   explicit IntervalMap(Allocator &a) : height(0), rootSize(0), allocator(a) {
1083     assert((uintptr_t(data) & (alignOf<RootLeaf>() - 1)) == 0 &&
1084            "Insufficient alignment");
1085     new(&rootLeaf()) RootLeaf();
1086   }
1087
1088   ~IntervalMap() {
1089     clear();
1090     rootLeaf().~RootLeaf();
1091   }
1092
1093   /// empty -  Return true when no intervals are mapped.
1094   bool empty() const {
1095     return rootSize == 0;
1096   }
1097
1098   /// start - Return the smallest mapped key in a non-empty map.
1099   KeyT start() const {
1100     assert(!empty() && "Empty IntervalMap has no start");
1101     return !branched() ? rootLeaf().start(0) : rootBranchStart();
1102   }
1103
1104   /// stop - Return the largest mapped key in a non-empty map.
1105   KeyT stop() const {
1106     assert(!empty() && "Empty IntervalMap has no stop");
1107     return !branched() ? rootLeaf().stop(rootSize - 1) :
1108                          rootBranch().stop(rootSize - 1);
1109   }
1110
1111   /// lookup - Return the mapped value at x or NotFound.
1112   ValT lookup(KeyT x, ValT NotFound = ValT()) const {
1113     if (empty() || Traits::startLess(x, start()) || Traits::stopLess(stop(), x))
1114       return NotFound;
1115     return branched() ? treeSafeLookup(x, NotFound) :
1116                         rootLeaf().safeLookup(x, NotFound);
1117   }
1118
1119   /// insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
1120   /// It is assumed that no key in the interval is mapped to another value, but
1121   /// overlapping intervals already mapped to y will be coalesced.
1122   void insert(KeyT a, KeyT b, ValT y) {
1123     find(a).insert(a, b, y);
1124   }
1125
1126   /// clear - Remove all entries.
1127   void clear();
1128
1129   class const_iterator;
1130   class iterator;
1131   friend class const_iterator;
1132   friend class iterator;
1133
1134   const_iterator begin() const {
1135     iterator I(*this);
1136     I.goToBegin();
1137     return I;
1138   }
1139
1140   iterator begin() {
1141     iterator I(*this);
1142     I.goToBegin();
1143     return I;
1144   }
1145
1146   const_iterator end() const {
1147     iterator I(*this);
1148     I.goToEnd();
1149     return I;
1150   }
1151
1152   iterator end() {
1153     iterator I(*this);
1154     I.goToEnd();
1155     return I;
1156   }
1157
1158   /// find - Return an iterator pointing to the first interval ending at or
1159   /// after x, or end().
1160   const_iterator find(KeyT x) const {
1161     iterator I(*this);
1162     I.find(x);
1163     return I;
1164   }
1165
1166   iterator find(KeyT x) {
1167     iterator I(*this);
1168     I.find(x);
1169     return I;
1170   }
1171
1172 #ifndef NDEBUG
1173   raw_ostream *OS;
1174   void dump();
1175   void dumpNode(IntervalMapImpl::NodeRef Node, unsigned Height);
1176 #endif
1177 };
1178
1179 /// treeSafeLookup - Return the mapped value at x or NotFound, assuming a
1180 /// branched root.
1181 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1182 ValT IntervalMap<KeyT, ValT, N, Traits>::
1183 treeSafeLookup(KeyT x, ValT NotFound) const {
1184   assert(branched() && "treeLookup assumes a branched root");
1185
1186   IntervalMapImpl::NodeRef NR = rootBranch().safeLookup(x);
1187   for (unsigned h = height-1; h; --h)
1188     NR = NR.get<Branch>().safeLookup(x);
1189   return NR.get<Leaf>().safeLookup(x, NotFound);
1190 }
1191
1192
1193 // branchRoot - Switch from a leaf root to a branched root.
1194 // Return the new (root offset, node offset) corresponding to Position.
1195 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1196 IntervalMapImpl::IdxPair IntervalMap<KeyT, ValT, N, Traits>::
1197 branchRoot(unsigned Position) {
1198   using namespace IntervalMapImpl;
1199   // How many external leaf nodes to hold RootLeaf+1?
1200   const unsigned Nodes = RootLeaf::Capacity / Leaf::Capacity + 1;
1201
1202   // Compute element distribution among new nodes.
1203   unsigned size[Nodes];
1204   IdxPair NewOffset(0, Position);
1205
1206   // Is is very common for the root node to be smaller than external nodes.
1207   if (Nodes == 1)
1208     size[0] = rootSize;
1209   else
1210     NewOffset = distribute(Nodes, rootSize, Leaf::Capacity,  NULL, size,
1211                            Position, true);
1212
1213   // Allocate new nodes.
1214   unsigned pos = 0;
1215   NodeRef node[Nodes];
1216   for (unsigned n = 0; n != Nodes; ++n) {
1217     Leaf *L = newNode<Leaf>();
1218     L->copy(rootLeaf(), pos, 0, size[n]);
1219     node[n] = NodeRef(L, size[n]);
1220     pos += size[n];
1221   }
1222
1223   // Destroy the old leaf node, construct branch node instead.
1224   switchRootToBranch();
1225   for (unsigned n = 0; n != Nodes; ++n) {
1226     rootBranch().stop(n) = node[n].template get<Leaf>().stop(size[n]-1);
1227     rootBranch().subtree(n) = node[n];
1228   }
1229   rootBranchStart() = node[0].template get<Leaf>().start(0);
1230   rootSize = Nodes;
1231   return NewOffset;
1232 }
1233
1234 // splitRoot - Split the current BranchRoot into multiple Branch nodes.
1235 // Return the new (root offset, node offset) corresponding to Position.
1236 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1237 IntervalMapImpl::IdxPair IntervalMap<KeyT, ValT, N, Traits>::
1238 splitRoot(unsigned Position) {
1239   using namespace IntervalMapImpl;
1240   // How many external leaf nodes to hold RootBranch+1?
1241   const unsigned Nodes = RootBranch::Capacity / Branch::Capacity + 1;
1242
1243   // Compute element distribution among new nodes.
1244   unsigned Size[Nodes];
1245   IdxPair NewOffset(0, Position);
1246
1247   // Is is very common for the root node to be smaller than external nodes.
1248   if (Nodes == 1)
1249     Size[0] = rootSize;
1250   else
1251     NewOffset = distribute(Nodes, rootSize, Leaf::Capacity,  NULL, Size,
1252                            Position, true);
1253
1254   // Allocate new nodes.
1255   unsigned Pos = 0;
1256   NodeRef Node[Nodes];
1257   for (unsigned n = 0; n != Nodes; ++n) {
1258     Branch *B = newNode<Branch>();
1259     B->copy(rootBranch(), Pos, 0, Size[n]);
1260     Node[n] = NodeRef(B, Size[n]);
1261     Pos += Size[n];
1262   }
1263
1264   for (unsigned n = 0; n != Nodes; ++n) {
1265     rootBranch().stop(n) = Node[n].template get<Branch>().stop(Size[n]-1);
1266     rootBranch().subtree(n) = Node[n];
1267   }
1268   rootSize = Nodes;
1269   ++height;
1270   return NewOffset;
1271 }
1272
1273 /// visitNodes - Visit each external node.
1274 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1275 void IntervalMap<KeyT, ValT, N, Traits>::
1276 visitNodes(void (IntervalMap::*f)(IntervalMapImpl::NodeRef, unsigned Height)) {
1277   if (!branched())
1278     return;
1279   SmallVector<IntervalMapImpl::NodeRef, 4> Refs, NextRefs;
1280
1281   // Collect level 0 nodes from the root.
1282   for (unsigned i = 0; i != rootSize; ++i)
1283     Refs.push_back(rootBranch().subtree(i));
1284
1285   // Visit all branch nodes.
1286   for (unsigned h = height - 1; h; --h) {
1287     for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
1288       for (unsigned j = 0, s = Refs[i].size(); j != s; ++j)
1289         NextRefs.push_back(Refs[i].subtree(j));
1290       (this->*f)(Refs[i], h);
1291     }
1292     Refs.clear();
1293     Refs.swap(NextRefs);
1294   }
1295
1296   // Visit all leaf nodes.
1297   for (unsigned i = 0, e = Refs.size(); i != e; ++i)
1298     (this->*f)(Refs[i], 0);
1299 }
1300
1301 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1302 void IntervalMap<KeyT, ValT, N, Traits>::
1303 deleteNode(IntervalMapImpl::NodeRef Node, unsigned Level) {
1304   if (Level)
1305     deleteNode(&Node.get<Branch>());
1306   else
1307     deleteNode(&Node.get<Leaf>());
1308 }
1309
1310 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1311 void IntervalMap<KeyT, ValT, N, Traits>::
1312 clear() {
1313   if (branched()) {
1314     visitNodes(&IntervalMap::deleteNode);
1315     switchRootToLeaf();
1316   }
1317   rootSize = 0;
1318 }
1319
1320 #ifndef NDEBUG
1321 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1322 void IntervalMap<KeyT, ValT, N, Traits>::
1323 dumpNode(IntervalMapImpl::NodeRef Node, unsigned Height) {
1324   if (Height)
1325     Node.get<Branch>().dump(*OS, Node.size());
1326   else
1327     Node.get<Leaf>().dump(*OS, Node.size());
1328 }
1329
1330 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1331 void IntervalMap<KeyT, ValT, N, Traits>::
1332 dump() {
1333   std::string errors;
1334   raw_fd_ostream ofs("tree.dot", errors);
1335   OS = &ofs;
1336   ofs << "digraph {\n";
1337   if (branched())
1338     rootBranch().dump(ofs, rootSize);
1339   else
1340     rootLeaf().dump(ofs, rootSize);
1341   visitNodes(&IntervalMap::dumpNode);
1342   ofs << "}\n";
1343 }
1344 #endif
1345
1346 //===----------------------------------------------------------------------===//
1347 //---                             const_iterator                          ----//
1348 //===----------------------------------------------------------------------===//
1349
1350 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1351 class IntervalMap<KeyT, ValT, N, Traits>::const_iterator :
1352   public std::iterator<std::bidirectional_iterator_tag, ValT> {
1353 protected:
1354   friend class IntervalMap;
1355
1356   // The map referred to.
1357   IntervalMap *map;
1358
1359   // We store a full path from the root to the current position.
1360   // The path may be partially filled, but never between iterator calls.
1361   IntervalMapImpl::Path path;
1362
1363   explicit const_iterator(IntervalMap &map) : map(&map) {}
1364
1365   bool branched() const {
1366     assert(map && "Invalid iterator");
1367     return map->branched();
1368   }
1369
1370   void setRoot(unsigned Offset) {
1371     if (branched())
1372       path.setRoot(&map->rootBranch(), map->rootSize, Offset);
1373     else
1374       path.setRoot(&map->rootLeaf(), map->rootSize, Offset);
1375   }
1376
1377   void pathFillFind(KeyT x);
1378   void treeFind(KeyT x);
1379
1380 public:
1381   /// valid - Return true if the current position is valid, false for end().
1382   bool valid() const { return path.valid(); }
1383
1384   /// start - Return the beginning of the current interval.
1385   const KeyT &start() const {
1386     assert(valid() && "Cannot access invalid iterator");
1387     return branched() ? path.leaf<Leaf>().start(path.leafOffset()) :
1388                         path.leaf<RootLeaf>().start(path.leafOffset());
1389   }
1390
1391   /// stop - Return the end of the current interval.
1392   const KeyT &stop() const {
1393     assert(valid() && "Cannot access invalid iterator");
1394     return branched() ? path.leaf<Leaf>().stop(path.leafOffset()) :
1395                         path.leaf<RootLeaf>().stop(path.leafOffset());
1396   }
1397
1398   /// value - Return the mapped value at the current interval.
1399   const ValT &value() const {
1400     assert(valid() && "Cannot access invalid iterator");
1401     return branched() ? path.leaf<Leaf>().value(path.leafOffset()) :
1402                         path.leaf<RootLeaf>().value(path.leafOffset());
1403   }
1404
1405   const ValT &operator*() const {
1406     return value();
1407   }
1408
1409   bool operator==(const const_iterator &RHS) const {
1410     assert(map == RHS.map && "Cannot compare iterators from different maps");
1411     if (!valid())
1412       return !RHS.valid();
1413     if (path.leafOffset() != RHS.path.leafOffset())
1414       return false;
1415     return &path.template leaf<Leaf>() == &RHS.path.template leaf<Leaf>();
1416   }
1417
1418   bool operator!=(const const_iterator &RHS) const {
1419     return !operator==(RHS);
1420   }
1421
1422   /// goToBegin - Move to the first interval in map.
1423   void goToBegin() {
1424     setRoot(0);
1425     if (branched())
1426       path.fillLeft(map->height);
1427   }
1428
1429   /// goToEnd - Move beyond the last interval in map.
1430   void goToEnd() {
1431     setRoot(map->rootSize);
1432   }
1433
1434   /// preincrement - move to the next interval.
1435   const_iterator &operator++() {
1436     assert(valid() && "Cannot increment end()");
1437     if (++path.leafOffset() == path.leafSize() && branched())
1438       path.moveRight(map->height);
1439     return *this;
1440   }
1441
1442   /// postincrement - Dont do that!
1443   const_iterator operator++(int) {
1444     const_iterator tmp = *this;
1445     operator++();
1446     return tmp;
1447   }
1448
1449   /// predecrement - move to the previous interval.
1450   const_iterator &operator--() {
1451     if (path.leafOffset() && (valid() || !branched()))
1452       --path.leafOffset();
1453     else
1454       path.moveLeft(map->height);
1455     return *this;
1456   }
1457
1458   /// postdecrement - Dont do that!
1459   const_iterator operator--(int) {
1460     const_iterator tmp = *this;
1461     operator--();
1462     return tmp;
1463   }
1464
1465   /// find - Move to the first interval with stop >= x, or end().
1466   /// This is a full search from the root, the current position is ignored.
1467   void find(KeyT x) {
1468     if (branched())
1469       treeFind(x);
1470     else
1471       setRoot(map->rootLeaf().findFrom(0, map->rootSize, x));
1472   }
1473
1474   /// advanceTo - Move to the first interval with stop >= x, or end().
1475   /// The search is started from the current position, and no earlier positions
1476   /// can be found. This is much faster than find() for small moves.
1477   void advanceTo(KeyT x) {
1478     if (branched())
1479       treeAdvanceTo(x);
1480     else
1481       path.leafOffset() =
1482         map->rootLeaf().findFrom(path.leafOffset(), map->rootSize, x);
1483   }
1484
1485 };
1486
1487 // pathFillFind - Complete path by searching for x.
1488 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1489 void IntervalMap<KeyT, ValT, N, Traits>::
1490 const_iterator::pathFillFind(KeyT x) {
1491   IntervalMapImpl::NodeRef NR = path.subtree(path.height());
1492   for (unsigned i = map->height - path.height() - 1; i; --i) {
1493     unsigned p = NR.get<Branch>().safeFind(0, x);
1494     path.push(NR, p);
1495     NR = NR.subtree(p);
1496   }
1497   path.push(NR, NR.get<Leaf>().safeFind(0, x));
1498 }
1499
1500 // treeFind - Find in a branched tree.
1501 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1502 void IntervalMap<KeyT, ValT, N, Traits>::
1503 const_iterator::treeFind(KeyT x) {
1504   setRoot(map->rootBranch().findFrom(0, map->rootSize, x));
1505   if (valid())
1506     pathFillFind(x);
1507 }
1508
1509
1510 //===----------------------------------------------------------------------===//
1511 //---                                iterator                             ----//
1512 //===----------------------------------------------------------------------===//
1513
1514 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1515 class IntervalMap<KeyT, ValT, N, Traits>::iterator : public const_iterator {
1516   friend class IntervalMap;
1517   typedef IntervalMapImpl::IdxPair IdxPair;
1518
1519   explicit iterator(IntervalMap &map) : const_iterator(map) {}
1520
1521   void setNodeStop(unsigned Level, KeyT Stop);
1522   bool insertNode(unsigned Level, IntervalMapImpl::NodeRef Node, KeyT Stop);
1523   template <typename NodeT> bool overflow(unsigned Level);
1524   void treeInsert(KeyT a, KeyT b, ValT y);
1525   void eraseNode(unsigned Level);
1526   void treeErase();
1527 public:
1528   /// insert - Insert mapping [a;b] -> y before the current position.
1529   void insert(KeyT a, KeyT b, ValT y);
1530
1531   /// erase - Erase the current interval.
1532   void erase();
1533 };
1534
1535 /// setNodeStop - Update the stop key of the current node at level and above.
1536 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1537 void IntervalMap<KeyT, ValT, N, Traits>::
1538 iterator::setNodeStop(unsigned Level, KeyT Stop) {
1539   // There are no references to the root node, so nothing to update.
1540   if (!Level)
1541     return;
1542   IntervalMapImpl::Path &P = this->path;
1543   // Update nodes pointing to the current node.
1544   while (--Level) {
1545     P.node<Branch>(Level).stop(P.offset(Level)) = Stop;
1546     if (!P.atLastBranch(Level))
1547       return;
1548   }
1549   // Update root separately since it has a different layout.
1550   P.node<RootBranch>(Level).stop(P.offset(Level)) = Stop;
1551 }
1552
1553 /// insertNode - insert a node before the current path at level.
1554 /// Leave the current path pointing at the new node.
1555 /// @param Level path index of the node to be inserted.
1556 /// @param Node The node to be inserted.
1557 /// @param Stop The last index in the new node.
1558 /// @return True if the tree height was increased.
1559 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1560 bool IntervalMap<KeyT, ValT, N, Traits>::
1561 iterator::insertNode(unsigned Level, IntervalMapImpl::NodeRef Node, KeyT Stop) {
1562   assert(Level && "Cannot insert next to the root");
1563   bool SplitRoot = false;
1564   IntervalMap &IM = *this->map;
1565   IntervalMapImpl::Path &P = this->path;
1566
1567   if (Level == 1) {
1568     // Insert into the root branch node.
1569     if (IM.rootSize < RootBranch::Capacity) {
1570       IM.rootBranch().insert(P.offset(0), IM.rootSize, Node, Stop);
1571       P.setSize(0, ++IM.rootSize);
1572       P.reset(Level);
1573       return SplitRoot;
1574     }
1575
1576     // We need to split the root while keeping our position.
1577     SplitRoot = true;
1578     IdxPair Offset = IM.splitRoot(P.offset(0));
1579     P.replaceRoot(&IM.rootBranch(), IM.rootSize, Offset);
1580
1581     // Fall through to insert at the new higher level.
1582     ++Level;
1583   }
1584
1585   // When inserting before end(), make sure we have a valid path.
1586   P.legalizeForInsert(--Level);
1587
1588   // Insert into the branch node at Level-1.
1589   if (P.size(Level) == Branch::Capacity) {
1590     // Branch node is full, handle handle the overflow.
1591     assert(!SplitRoot && "Cannot overflow after splitting the root");
1592     SplitRoot = overflow<Branch>(Level);
1593     Level += SplitRoot;
1594   }
1595   P.node<Branch>(Level).insert(P.offset(Level), P.size(Level), Node, Stop);
1596   P.setSize(Level, P.size(Level) + 1);
1597   if (P.atLastBranch(Level))
1598     setNodeStop(Level, Stop);
1599   P.reset(Level + 1);
1600   return SplitRoot;
1601 }
1602
1603 // insert
1604 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1605 void IntervalMap<KeyT, ValT, N, Traits>::
1606 iterator::insert(KeyT a, KeyT b, ValT y) {
1607   if (this->branched())
1608     return treeInsert(a, b, y);
1609   IntervalMap &IM = *this->map;
1610   IntervalMapImpl::Path &P = this->path;
1611
1612   // Try simple root leaf insert.
1613   IdxPair IP = IM.rootLeaf().insertFrom(P.leafOffset(), IM.rootSize, a, b, y);
1614
1615   // Was the root node insert successful?
1616   if (IP.second <= RootLeaf::Capacity) {
1617     P.leafOffset() = IP.first;
1618     P.setSize(0, IM.rootSize = IP.second);
1619     return;
1620   }
1621
1622   // Root leaf node is full, we must branch.
1623   IdxPair Offset = IM.branchRoot(P.leafOffset());
1624   P.replaceRoot(&IM.rootBranch(), IM.rootSize, Offset);
1625
1626   // Now it fits in the new leaf.
1627   treeInsert(a, b, y);
1628 }
1629
1630
1631 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1632 void IntervalMap<KeyT, ValT, N, Traits>::
1633 iterator::treeInsert(KeyT a, KeyT b, ValT y) {
1634   using namespace IntervalMapImpl;
1635   IntervalMap &IM = *this->map;
1636   Path &P = this->path;
1637
1638   // Check if this insertion will extend the node to the left.
1639   if (P.valid() && P.leafOffset() == 0 &&
1640       Traits::startLess(a, P.leaf<Leaf>().start(0))) {
1641     // Node is growing to the left, will it affect a left sibling node?
1642     if (NodeRef Sib = P.getLeftSibling(IM.height)) {
1643       Leaf &SibLeaf = Sib.get<Leaf>();
1644       unsigned SibOfs = Sib.size() - 1;
1645       if (SibLeaf.value(SibOfs) == y &&
1646           Traits::adjacent(SibLeaf.stop(SibOfs), a)) {
1647         // This insertion will coalesce with the last entry in SibLeaf. We can
1648         // handle it in two ways:
1649         //  1. Extend SibLeaf.stop to b and be done, or
1650         //  2. Extend a to SibLeaf, erase the SibLeaf entry and continue.
1651         // We prefer 1., but need 2 when coalescing to the right as well.
1652         Leaf &CurLeaf = P.leaf<Leaf>();
1653         P.moveLeft(IM.height);
1654         if (Traits::stopLess(b, CurLeaf.start(0)) &&
1655             (y != CurLeaf.value(0) || !Traits::adjacent(b, CurLeaf.start(0)))) {
1656           // Easy, just extend SibLeaf and we're done.
1657           setNodeStop(IM.height, SibLeaf.stop(SibOfs) = b);
1658           return;
1659         } else {
1660           // We have both left and right coalescing. Erase the old SibLeaf entry
1661           // and continue inserting the larger interval.
1662           a = SibLeaf.start(SibOfs);
1663           erase();
1664         }
1665       }
1666     } else {
1667       // No left sibling means we are at begin(). Update cached bound.
1668       IM.rootBranchStart() = a;
1669     }
1670   }
1671
1672   P.legalizeForInsert(IM.height);
1673   IdxPair IP = P.leaf<Leaf>().insertFrom(P.leafOffset(), P.leafSize(), a, b, y);
1674
1675   // Leaf insertion unsuccessful? Overflow and try again.
1676   if (IP.second > Leaf::Capacity) {
1677     overflow<Leaf>(IM.height);
1678     IP = P.leaf<Leaf>().insertFrom(P.leafOffset(), P.leafSize(), a, b, y);
1679     assert(IP.second <= Leaf::Capacity && "overflow() didn't make room");
1680   }
1681
1682   // Inserted, update offset and leaf size.
1683   P.leafOffset() = IP.first;
1684   P.setSize(IM.height, IP.second);
1685
1686   // Insert was the last node entry, update stops.
1687   if (IP.first == IP.second - 1)
1688     setNodeStop(IM.height, P.leaf<Leaf>().stop(IP.first));
1689 }
1690
1691 /// erase - erase the current interval and move to the next position.
1692 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1693 void IntervalMap<KeyT, ValT, N, Traits>::
1694 iterator::erase() {
1695   IntervalMap &IM = *this->map;
1696   IntervalMapImpl::Path &P = this->path;
1697   assert(P.valid() && "Cannot erase end()");
1698   if (this->branched())
1699     return treeErase();
1700   IM.rootLeaf().erase(P.leafOffset(), IM.rootSize);
1701   P.setSize(0, --IM.rootSize);
1702 }
1703
1704 /// treeErase - erase() for a branched tree.
1705 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1706 void IntervalMap<KeyT, ValT, N, Traits>::
1707 iterator::treeErase() {
1708   IntervalMap &IM = *this->map;
1709   IntervalMapImpl::Path &P = this->path;
1710   Leaf &Node = P.leaf<Leaf>();
1711
1712   // Nodes are not allowed to become empty.
1713   if (P.leafSize() == 1) {
1714     IM.deleteNode(&Node);
1715     eraseNode(IM.height);
1716     return;
1717   }
1718
1719   // Erase current entry.
1720   Node.erase(P.leafOffset(), P.leafSize());
1721   unsigned NewSize = P.leafSize() - 1;
1722   P.setSize(IM.height, NewSize);
1723   // When we erase the last entry, update stop and move to a legal position.
1724   if (P.leafOffset() == NewSize) {
1725     setNodeStop(IM.height, Node.stop(NewSize - 1));
1726     P.moveRight(IM.height);
1727   }
1728 }
1729
1730 /// eraseNode - Erase the current node at Level from its parent and move path to
1731 /// the first entry of the next sibling node.
1732 /// The node must be deallocated by the caller.
1733 /// @param Level 1..height, the root node cannot be erased.
1734 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1735 void IntervalMap<KeyT, ValT, N, Traits>::
1736 iterator::eraseNode(unsigned Level) {
1737   assert(Level && "Cannot erase root node");
1738   IntervalMap &IM = *this->map;
1739   IntervalMapImpl::Path &P = this->path;
1740
1741   if (--Level == 0) {
1742     IM.rootBranch().erase(P.offset(0), IM.rootSize);
1743     P.setSize(0, --IM.rootSize);
1744     // If this cleared the root, switch to height=0.
1745     if (IM.empty()) {
1746       IM.switchRootToLeaf();
1747       this->setRoot(0);
1748       return;
1749     }
1750   } else {
1751     // Remove node ref from branch node at Level.
1752     Branch &Parent = P.node<Branch>(Level);
1753     if (P.size(Level) == 1) {
1754       // Branch node became empty, remove it recursively.
1755       IM.deleteNode(&Parent);
1756       eraseNode(Level);
1757     } else {
1758       // Branch node won't become empty.
1759       Parent.erase(P.offset(Level), P.size(Level));
1760       unsigned NewSize = P.size(Level) - 1;
1761       P.setSize(Level, NewSize);
1762       // If we removed the last branch, update stop and move to a legal pos.
1763       if (P.offset(Level) == NewSize) {
1764         setNodeStop(Level, Parent.stop(NewSize - 1));
1765         P.moveRight(Level);
1766       }
1767     }
1768   }
1769   // Update path cache for the new right sibling position.
1770   if (P.valid()) {
1771     P.reset(Level + 1);
1772     P.offset(Level + 1) = 0;
1773   }
1774 }
1775
1776 /// overflow - Distribute entries of the current node evenly among
1777 /// its siblings and ensure that the current node is not full.
1778 /// This may require allocating a new node.
1779 /// @param NodeT The type of node at Level (Leaf or Branch).
1780 /// @param Level path index of the overflowing node.
1781 /// @return True when the tree height was changed.
1782 template <typename KeyT, typename ValT, unsigned N, typename Traits>
1783 template <typename NodeT>
1784 bool IntervalMap<KeyT, ValT, N, Traits>::
1785 iterator::overflow(unsigned Level) {
1786   using namespace IntervalMapImpl;
1787   Path &P = this->path;
1788   unsigned CurSize[4];
1789   NodeT *Node[4];
1790   unsigned Nodes = 0;
1791   unsigned Elements = 0;
1792   unsigned Offset = P.offset(Level);
1793
1794   // Do we have a left sibling?
1795   NodeRef LeftSib = P.getLeftSibling(Level);
1796   if (LeftSib) {
1797     Offset += Elements = CurSize[Nodes] = LeftSib.size();
1798     Node[Nodes++] = &LeftSib.get<NodeT>();
1799   }
1800
1801   // Current node.
1802   Elements += CurSize[Nodes] = P.size(Level);
1803   Node[Nodes++] = &P.node<NodeT>(Level);
1804
1805   // Do we have a right sibling?
1806   NodeRef RightSib = P.getRightSibling(Level);
1807   if (RightSib) {
1808     Elements += CurSize[Nodes] = RightSib.size();
1809     Node[Nodes++] = &RightSib.get<NodeT>();
1810   }
1811
1812   // Do we need to allocate a new node?
1813   unsigned NewNode = 0;
1814   if (Elements + 1 > Nodes * NodeT::Capacity) {
1815     // Insert NewNode at the penultimate position, or after a single node.
1816     NewNode = Nodes == 1 ? 1 : Nodes - 1;
1817     CurSize[Nodes] = CurSize[NewNode];
1818     Node[Nodes] = Node[NewNode];
1819     CurSize[NewNode] = 0;
1820     Node[NewNode] = this->map->newNode<NodeT>();
1821     ++Nodes;
1822   }
1823
1824   // Compute the new element distribution.
1825   unsigned NewSize[4];
1826   IdxPair NewOffset = distribute(Nodes, Elements, NodeT::Capacity,
1827                                  CurSize, NewSize, Offset, true);
1828   adjustSiblingSizes(Node, Nodes, CurSize, NewSize);
1829
1830   // Move current location to the leftmost node.
1831   if (LeftSib)
1832     P.moveLeft(Level);
1833
1834   // Elements have been rearranged, now update node sizes and stops.
1835   bool SplitRoot = false;
1836   unsigned Pos = 0;
1837   for (;;) {
1838     KeyT Stop = Node[Pos]->stop(NewSize[Pos]-1);
1839     if (NewNode && Pos == NewNode) {
1840       SplitRoot = insertNode(Level, NodeRef(Node[Pos], NewSize[Pos]), Stop);
1841       Level += SplitRoot;
1842     } else {
1843       P.setSize(Level, NewSize[Pos]);
1844       setNodeStop(Level, Stop);
1845     }
1846     if (Pos + 1 == Nodes)
1847       break;
1848     P.moveRight(Level);
1849     ++Pos;
1850   }
1851
1852   // Where was I? Find NewOffset.
1853   while(Pos != NewOffset.first) {
1854     P.moveLeft(Level);
1855     --Pos;
1856   }
1857   P.offset(Level) = NewOffset.second;
1858   return SplitRoot;
1859 }
1860
1861 } // namespace llvm
1862
1863 #endif