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