1 //===--- ImmutableIntervalMap.h - Immutable (functional) map ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ImmutableIntervalMap class.
12 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/ImmutableMap.h"
23 Interval(uint64_t S, uint64_t E) : Start(S), End(E) {}
25 uint64_t getStart() const { return Start; }
26 uint64_t getEnd() const { return End; }
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;
38 static key_type_ref KeyOfValue(value_type_ref V) {
42 static data_type_ref DataOfValue(value_type_ref V) {
46 static bool isEqual(key_type_ref L, key_type_ref R) {
47 return L.getStart() == R.getStart() && L.getEnd() == R.getEnd();
50 static bool isDataEqual(data_type_ref L, data_type_ref R) {
51 return ImutContainerInfo<T>::isEqual(L,R);
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());
59 } else if (L.getStart() == R.getStart()) {
60 assert(L.getEnd() == R.getEnd());
63 assert(L.getStart() > R.getEnd());
68 static bool isContainedIn(key_type_ref K, key_type_ref L) {
69 if (K.getStart() >= L.getStart() && K.getEnd() <= L.getEnd())
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);
82 template <typename ImutInfo> class ImutIntervalAVLFactory;
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;
95 TreeTy *Add(TreeTy *T, value_type_ref V) {
96 T = Add_internal(V,T);
101 TreeTy *Find(TreeTy *T, key_type_ref K) {
105 key_type_ref CurrentKey = ImutInfo::KeyOfValue(Value(T));
107 if (ImutInfo::isContainedIn(K, CurrentKey))
109 else if (ImutInfo::isLess(K, CurrentKey))
110 return Find(Left(T), K);
112 return Find(Right(T), K);
116 TreeTy *Add_internal(value_type_ref V, TreeTy *T) {
117 key_type_ref K = ImutInfo::KeyOfValue(V);
118 T = RemoveAllOverlaps(T, K);
120 return CreateNode(NULL, V, NULL);
122 assert(!T->isMutable());
124 key_type_ref KCurrent = ImutInfo::KeyOfValue(Value(T));
126 if (ImutInfo::isLess(K, KCurrent))
127 return Balance(Add_internal(V, Left(T)), Value(T), Right(T));
129 return Balance(Left(T), Value(T), Add_internal(V, Right(T)));
132 // Remove all overlaps from T.
133 TreeTy *RemoveAllOverlaps(TreeTy *T, key_type_ref K) {
134 TreeTy *OldTree, *NewTree;
139 NewTree = RemoveOverlap(OldTree, K);
140 } while (NewTree != OldTree);
145 // Remove one overlap from T.
146 TreeTy *RemoveOverlap(TreeTy *T, key_type_ref K) {
149 Interval CurrentK = ImutInfo::KeyOfValue(Value(T));
151 // If current key does not overlap the inserted key.
152 if (CurrentK.getStart() > K.getEnd())
153 return RemoveOverlap(Left(T), K);
154 else if (CurrentK.getEnd() < K.getStart())
155 return RemoveOverlap(Right(T), K);
157 // Current key overlaps with the inserted key.
158 // Remove the current key.
160 T = Remove_internal(CurrentK, T);
161 // Add back the unoverlapped part of the current key.
162 if (CurrentK.getStart() < K.getStart()) {
163 if (CurrentK.getEnd() <= K.getEnd()) {
164 Interval NewK(CurrentK.getStart(), K.getStart()-1);
165 return Add_internal(std::make_pair<key_type, data_type>(NewK,
166 ImutInfo::DataOfValue(Value(OldNode))), T);
168 Interval NewK1(CurrentK.getStart(), K.getStart()-1);
169 T = Add_internal(std::make_pair<key_type, data_type>(NewK1,
170 ImutInfo::DataOfValue(Value(OldNode))), T);
172 Interval NewK2(K.getEnd()+1, CurrentK.getEnd());
173 return Add_internal(std::make_pair<key_type, data_type>(NewK2,
174 ImutInfo::DataOfValue(Value(OldNode))), T);
177 if (CurrentK.getEnd() > K.getEnd()) {
178 Interval NewK(K.getEnd()+1, CurrentK.getEnd());
179 return Add_internal(std::make_pair<key_type, data_type>(NewK,
180 ImutInfo::DataOfValue(Value(OldNode))), T);
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> > {
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;
201 explicit ImmutableIntervalMap(TreeTy *R)
202 : ImmutableMap<Interval, ValT, ImutIntervalInfo<ValT> >(R) {}
205 ImutIntervalAVLFactory<ImutIntervalInfo<ValT> > F;
208 ImmutableIntervalMap GetEmptyMap() {
209 return ImmutableIntervalMap(F.GetEmptyTree());
212 ImmutableIntervalMap Add(ImmutableIntervalMap Old,
213 key_type_ref K, data_type_ref D) {
214 TreeTy *T = F.Add(Old.Root, std::make_pair<key_type, data_type>(K, D));
215 return ImmutableIntervalMap(F.GetCanonicalTree(T));
218 ImmutableIntervalMap Remove(ImmutableIntervalMap Old, key_type_ref K) {
219 TreeTy *T = F.Remove(Old.Root, K);
220 return ImmutableIntervalMap(F.GetCanonicalTree(T));
223 data_type *Lookup(ImmutableIntervalMap M, key_type_ref K) {
224 TreeTy *T = F.Find(M.getRoot(), K);
226 return &T->getValue().second;
234 // For ImmutableIntervalMap, the lookup operation has to be done by the
236 data_type* lookup(key_type_ref K) const;
239 } // end namespace llvm