Add constructors.
[oota-llvm.git] / include / llvm / ADT / ImmutableIntervalMap.h
1 //===--- ImmutableIntervalMap.h - Immutable (functional) 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 defines the ImmutableIntervalMap class.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/ImmutableMap.h"
14
15 namespace llvm {
16
17 class Interval {
18 private:
19   uint64_t Start;
20   uint64_t End;
21
22 public:
23   Interval(uint64_t S, uint64_t E) : Start(S), End(E) {}
24
25   uint64_t getStart() const { return Start; }
26   uint64_t getEnd() const { return End; }
27 };
28
29 template <typename T>
30 struct ImutIntervalInfo {
31   typedef const std::pair<Interval, T> value_type;
32   typedef const value_type &value_type_ref;
33   typedef const Interval key_type;
34   typedef const Interval &key_type_ref;
35   typedef const T data_type;
36   typedef const T &data_type_ref;
37
38   static key_type_ref KeyOfValue(value_type_ref V) {
39     return V.first;
40   }
41
42   static data_type_ref DataOfValue(value_type_ref V) {
43     return V.second;
44   }
45
46   static bool isEqual(key_type_ref L, key_type_ref R) {
47     return L.getStart() == R.getStart() && L.getEnd() == R.getEnd();
48   }
49
50   static bool isDataEqual(data_type_ref L, data_type_ref R) {
51     return ImutContainerInfo<T>::isEqual(L,R);
52   }
53
54   static bool isLess(key_type_ref L, key_type_ref R) {
55     // Assume L and R does not overlap.
56     if (L.getStart() < R.getStart()) {
57       assert(L.getEnd() < R.getStart());
58       return true;
59     } else if (L.getStart() == R.getStart()) {
60       assert(L.getEnd() == R.getEnd());
61       return false;
62     } else {
63       assert(L.getStart() > R.getEnd());
64       return false;
65     }
66   }
67
68   static bool isContainedIn(key_type_ref K, key_type_ref L) {
69     if (K.getStart() >= L.getStart() && K.getEnd() <= L.getEnd())
70       return true;
71     else
72       return false;
73   }
74
75   static void Profile(FoldingSetNodeID &ID, value_type_ref V) {
76     ID.AddInteger(V.first.getStart());
77     ID.AddInteger(V.first.getEnd());
78     ImutProfileInfo<T>::Profile(ID, V.second);
79   }
80 };
81
82 template <typename ImutInfo> class ImutIntervalAVLFactory;
83
84 template <typename ImutInfo>
85 class ImutIntervalAVLFactory : public ImutAVLFactory<ImutInfo> {
86   typedef ImutAVLTree<ImutInfo> TreeTy;
87   typedef typename ImutInfo::value_type     value_type;
88   typedef typename ImutInfo::value_type_ref value_type_ref;
89   typedef typename ImutInfo::key_type       key_type;
90   typedef typename ImutInfo::key_type_ref   key_type_ref;
91   typedef typename ImutInfo::data_type      data_type;
92   typedef typename ImutInfo::data_type_ref  data_type_ref;
93
94 public:
95   ImutIntervalAVLFactory(BumpPtrAllocator &Alloc) 
96     : ImutAVLFactory<ImutInfo>(Alloc) {}
97
98   TreeTy *Add(TreeTy *T, value_type_ref V) {
99     T = Add_internal(V,T);
100     MarkImmutable(T);
101     return T;
102   }
103
104   TreeTy *Find(TreeTy *T, key_type_ref K) {
105     if (!T)
106       return NULL;
107
108     key_type_ref CurrentKey = ImutInfo::KeyOfValue(Value(T));
109
110     if (ImutInfo::isContainedIn(K, CurrentKey))
111       return T;
112     else if (ImutInfo::isLess(K, CurrentKey))
113       return Find(Left(T), K);
114     else
115       return Find(Right(T), K);
116   }
117
118 private:
119   TreeTy *Add_internal(value_type_ref V, TreeTy *T) {
120     key_type_ref K = ImutInfo::KeyOfValue(V);
121     T = RemoveAllOverlaps(T, K);
122     if (isEmpty(T))
123       return CreateNode(NULL, V, NULL);
124
125     assert(!T->isMutable());
126
127     key_type_ref KCurrent = ImutInfo::KeyOfValue(Value(T));
128
129     if (ImutInfo::isLess(K, KCurrent))
130       return Balance(Add_internal(V, Left(T)), Value(T), Right(T));
131     else
132       return Balance(Left(T), Value(T), Add_internal(V, Right(T)));
133   }
134
135   // Remove all overlaps from T.
136   TreeTy *RemoveAllOverlaps(TreeTy *T, key_type_ref K) {
137     bool Changed;
138     do {
139       Changed = false;
140       T = RemoveOverlap(T, K, Changed);
141       MarkImmutable(T);
142     } while (Changed);
143
144     return T;
145   }
146
147   // Remove one overlap from T.
148   TreeTy *RemoveOverlap(TreeTy *T, key_type_ref K, bool &Changed) {
149     if (!T)
150       return NULL;
151     Interval CurrentK = ImutInfo::KeyOfValue(Value(T));
152
153     // If current key does not overlap the inserted key.
154     if (CurrentK.getStart() > K.getEnd())
155       return Balance(RemoveOverlap(Left(T), K, Changed), Value(T), Right(T));
156     else if (CurrentK.getEnd() < K.getStart())
157       return Balance(Left(T), Value(T), RemoveOverlap(Right(T), K, Changed));
158
159     // Current key overlaps with the inserted key.
160     // Remove the current key.
161     Changed = true;
162     data_type_ref OldData = ImutInfo::DataOfValue(Value(T));
163     T = Remove_internal(CurrentK, T);
164     // Add back the unoverlapped part of the current key.
165     if (CurrentK.getStart() < K.getStart()) {
166       if (CurrentK.getEnd() <= K.getEnd()) {
167         Interval NewK(CurrentK.getStart(), K.getStart()-1);
168         return Add_internal(std::make_pair(NewK, OldData), T);
169       } else {
170         Interval NewK1(CurrentK.getStart(), K.getStart()-1);
171         T = Add_internal(std::make_pair(NewK1, OldData), T); 
172
173         Interval NewK2(K.getEnd()+1, CurrentK.getEnd());
174         return Add_internal(std::make_pair(NewK2, OldData), T);
175       }
176     } else {
177       if (CurrentK.getEnd() > K.getEnd()) {
178         Interval NewK(K.getEnd()+1, CurrentK.getEnd());
179         return Add_internal(std::make_pair(NewK, OldData), T);
180       } else
181         return T;
182     }
183   }
184 };
185
186 /// ImmutableIntervalMap maps an interval [start, end] to a value. The intervals
187 /// in the map are guaranteed to be disjoint.
188 template <typename ValT>
189 class ImmutableIntervalMap 
190   : public ImmutableMap<Interval, ValT, ImutIntervalInfo<ValT> > {
191
192   typedef typename ImutIntervalInfo<ValT>::value_type      value_type;
193   typedef typename ImutIntervalInfo<ValT>::value_type_ref  value_type_ref;
194   typedef typename ImutIntervalInfo<ValT>::key_type        key_type;
195   typedef typename ImutIntervalInfo<ValT>::key_type_ref    key_type_ref;
196   typedef typename ImutIntervalInfo<ValT>::data_type       data_type;
197   typedef typename ImutIntervalInfo<ValT>::data_type_ref   data_type_ref;
198   typedef ImutAVLTree<ImutIntervalInfo<ValT> > TreeTy;
199
200 public:
201   explicit ImmutableIntervalMap(TreeTy *R) 
202     : ImmutableMap<Interval, ValT, ImutIntervalInfo<ValT> >(R) {}
203
204   class Factory {
205     ImutIntervalAVLFactory<ImutIntervalInfo<ValT> > F;
206
207   public:
208     Factory(BumpPtrAllocator& Alloc) : F(Alloc) {}
209
210     ImmutableIntervalMap GetEmptyMap() { 
211       return ImmutableIntervalMap(F.GetEmptyTree()); 
212     }
213
214     ImmutableIntervalMap Add(ImmutableIntervalMap Old, 
215                              key_type_ref K, data_type_ref D) {
216       TreeTy *T = F.Add(Old.Root, std::make_pair<key_type, data_type>(K, D));
217       return ImmutableIntervalMap(F.GetCanonicalTree(T));
218     }
219
220     ImmutableIntervalMap Remove(ImmutableIntervalMap Old, key_type_ref K) {
221       TreeTy *T = F.Remove(Old.Root, K);
222       return ImmutableIntervalMap(F.GetCanonicalTree(T));
223     }
224
225     data_type *Lookup(ImmutableIntervalMap M, key_type_ref K) {
226       TreeTy *T = F.Find(M.getRoot(), K);
227       if (T)
228         return &T->getValue().second;
229       else
230         return 0;
231     }
232   };
233
234 private:
235   // For ImmutableIntervalMap, the lookup operation has to be done by the 
236   // factory.
237   data_type* lookup(key_type_ref K) const;
238 };
239
240 } // end namespace llvm