Whitespace.
[oota-llvm.git] / include / llvm / ADT / ImmutableMap.h
1 //===--- ImmutableMap.h - Immutable (functional) map interface --*- 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 ImmutableMap class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_IMMUTABLEMAP_H
15 #define LLVM_ADT_IMMUTABLEMAP_H
16
17 #include "llvm/ADT/ImmutableSet.h"
18
19 namespace llvm {
20
21 /// ImutKeyValueInfo -Traits class used by ImmutableMap.  While both the first
22 /// and second elements in a pair are used to generate profile information,
23 /// only the first element (the key) is used by isEqual and isLess.
24 template <typename T, typename S>
25 struct ImutKeyValueInfo {
26   typedef const std::pair<T,S> value_type;
27   typedef const value_type& value_type_ref;
28   typedef const T   key_type;
29   typedef const T&  key_type_ref;
30   typedef const S   data_type;
31   typedef const S&  data_type_ref;
32
33   static inline key_type_ref KeyOfValue(value_type_ref V) {
34     return V.first;
35   }
36
37   static inline data_type_ref DataOfValue(value_type_ref V) {
38     return V.second;
39   }
40
41   static inline bool isEqual(key_type_ref L, key_type_ref R) {
42     return ImutContainerInfo<T>::isEqual(L,R);
43   }
44   static inline bool isLess(key_type_ref L, key_type_ref R) {
45     return ImutContainerInfo<T>::isLess(L,R);
46   }
47
48   static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
49     return ImutContainerInfo<S>::isEqual(L,R);
50   }
51
52   static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
53     ImutContainerInfo<T>::Profile(ID, V.first);
54     ImutContainerInfo<S>::Profile(ID, V.second);
55   }
56 };
57
58 template <typename KeyT, typename ValT,
59           typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
60 class ImmutableMap {
61 public:
62   typedef typename ValInfo::value_type      value_type;
63   typedef typename ValInfo::value_type_ref  value_type_ref;
64   typedef typename ValInfo::key_type        key_type;
65   typedef typename ValInfo::key_type_ref    key_type_ref;
66   typedef typename ValInfo::data_type       data_type;
67   typedef typename ValInfo::data_type_ref   data_type_ref;
68   typedef ImutAVLTree<ValInfo>              TreeTy;
69
70 protected:
71   TreeTy* Root;
72
73 public:
74   /// Constructs a map from a pointer to a tree root.  In general one
75   /// should use a Factory object to create maps instead of directly
76   /// invoking the constructor, but there are cases where make this
77   /// constructor public is useful.
78   explicit ImmutableMap(const TreeTy* R) : Root(const_cast<TreeTy*>(R)) {
79     if (Root) { Root->retain(); }
80   }
81   ImmutableMap(const ImmutableMap &X) : Root(X.Root) {
82     if (Root) { Root->retain(); }
83   }
84   ImmutableMap &operator=(const ImmutableMap &X) {
85     if (Root != X.Root) {
86       if (X.Root) { X.Root->retain(); }
87       if (Root) { Root->release(); }
88       Root = X.Root;
89     }
90     return *this;
91   }
92   ~ImmutableMap() {
93     if (Root) { Root->release(); }
94   }
95
96   class Factory {
97     typename TreeTy::Factory F;
98     const bool Canonicalize;
99
100   public:
101     Factory(bool canonicalize = true)
102       : Canonicalize(canonicalize) {}
103
104     Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
105       : F(Alloc), Canonicalize(canonicalize) {}
106
107     ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
108
109     ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
110       TreeTy *T = F.add(Old.Root, std::pair<key_type,data_type>(K,D));
111       return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
112     }
113
114     ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
115       TreeTy *T = F.remove(Old.Root,K);
116       return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
117     }
118
119     typename TreeTy::Factory *getTreeFactory() const {
120       return const_cast<typename TreeTy::Factory *>(&F);
121     }
122
123   private:
124     Factory(const Factory& RHS) = delete;
125     void operator=(const Factory& RHS) = delete;
126   };
127
128   bool contains(key_type_ref K) const {
129     return Root ? Root->contains(K) : false;
130   }
131
132   bool operator==(const ImmutableMap &RHS) const {
133     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
134   }
135
136   bool operator!=(const ImmutableMap &RHS) const {
137     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
138   }
139
140   TreeTy *getRoot() const {
141     if (Root) { Root->retain(); }
142     return Root;
143   }
144
145   TreeTy *getRootWithoutRetain() const {
146     return Root;
147   }
148
149   void manualRetain() {
150     if (Root) Root->retain();
151   }
152
153   void manualRelease() {
154     if (Root) Root->release();
155   }
156
157   bool isEmpty() const { return !Root; }
158
159   //===--------------------------------------------------===//
160   // Foreach - A limited form of map iteration.
161   //===--------------------------------------------------===//
162
163 private:
164   template <typename Callback>
165   struct CBWrapper {
166     Callback C;
167     void operator()(value_type_ref V) { C(V.first,V.second); }
168   };
169
170   template <typename Callback>
171   struct CBWrapperRef {
172     Callback &C;
173     CBWrapperRef(Callback& c) : C(c) {}
174
175     void operator()(value_type_ref V) { C(V.first,V.second); }
176   };
177
178 public:
179   template <typename Callback>
180   void foreach(Callback& C) {
181     if (Root) {
182       CBWrapperRef<Callback> CB(C);
183       Root->foreach(CB);
184     }
185   }
186
187   template <typename Callback>
188   void foreach() {
189     if (Root) {
190       CBWrapper<Callback> CB;
191       Root->foreach(CB);
192     }
193   }
194
195   //===--------------------------------------------------===//
196   // For testing.
197   //===--------------------------------------------------===//
198
199   void verify() const { if (Root) Root->verify(); }
200
201   //===--------------------------------------------------===//
202   // Iterators.
203   //===--------------------------------------------------===//
204
205   class iterator : public ImutAVLValueIterator<ImmutableMap> {
206     iterator() = default;
207     explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
208     friend class ImmutableMap;
209
210   public:
211     key_type_ref getKey() const { return (*this)->first; }
212     data_type_ref getData() const { return (*this)->second; }
213   };
214
215   iterator begin() const { return iterator(Root); }
216   iterator end() const { return iterator(); }
217
218   data_type* lookup(key_type_ref K) const {
219     if (Root) {
220       TreeTy* T = Root->find(K);
221       if (T) return &T->getValue().second;
222     }
223
224     return nullptr;
225   }
226
227   /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
228   ///  which key is the highest in the ordering of keys in the map.  This
229   ///  method returns NULL if the map is empty.
230   value_type* getMaxElement() const {
231     return Root ? &(Root->getMaxElement()->getValue()) : nullptr;
232   }
233
234   //===--------------------------------------------------===//
235   // Utility methods.
236   //===--------------------------------------------------===//
237
238   unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
239
240   static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
241     ID.AddPointer(M.Root);
242   }
243
244   inline void Profile(FoldingSetNodeID& ID) const {
245     return Profile(ID,*this);
246   }
247 };
248
249 // NOTE: This will possibly become the new implementation of ImmutableMap some day.
250 template <typename KeyT, typename ValT,
251 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
252 class ImmutableMapRef {
253 public:
254   typedef typename ValInfo::value_type      value_type;
255   typedef typename ValInfo::value_type_ref  value_type_ref;
256   typedef typename ValInfo::key_type        key_type;
257   typedef typename ValInfo::key_type_ref    key_type_ref;
258   typedef typename ValInfo::data_type       data_type;
259   typedef typename ValInfo::data_type_ref   data_type_ref;
260   typedef ImutAVLTree<ValInfo>              TreeTy;
261   typedef typename TreeTy::Factory          FactoryTy;
262
263 protected:
264   TreeTy *Root;
265   FactoryTy *Factory;
266
267 public:
268   /// Constructs a map from a pointer to a tree root.  In general one
269   /// should use a Factory object to create maps instead of directly
270   /// invoking the constructor, but there are cases where make this
271   /// constructor public is useful.
272   explicit ImmutableMapRef(const TreeTy* R, FactoryTy *F)
273     : Root(const_cast<TreeTy*>(R)),
274       Factory(F) {
275     if (Root) { Root->retain(); }
276   }
277
278   explicit ImmutableMapRef(const ImmutableMap<KeyT, ValT> &X,
279                            typename ImmutableMap<KeyT, ValT>::Factory &F)
280     : Root(X.getRootWithoutRetain()),
281       Factory(F.getTreeFactory()) {
282     if (Root) { Root->retain(); }
283   }
284
285   ImmutableMapRef(const ImmutableMapRef &X)
286     : Root(X.Root),
287       Factory(X.Factory) {
288     if (Root) { Root->retain(); }
289   }
290
291   ImmutableMapRef &operator=(const ImmutableMapRef &X) {
292     if (Root != X.Root) {
293       if (X.Root)
294         X.Root->retain();
295
296       if (Root)
297         Root->release();
298
299       Root = X.Root;
300       Factory = X.Factory;
301     }
302     return *this;
303   }
304
305   ~ImmutableMapRef() {
306     if (Root)
307       Root->release();
308   }
309
310   static inline ImmutableMapRef getEmptyMap(FactoryTy *F) {
311     return ImmutableMapRef(0, F);
312   }
313
314   void manualRetain() {
315     if (Root) Root->retain();
316   }
317
318   void manualRelease() {
319     if (Root) Root->release();
320   }
321
322   ImmutableMapRef add(key_type_ref K, data_type_ref D) const {
323     TreeTy *NewT = Factory->add(Root, std::pair<key_type, data_type>(K, D));
324     return ImmutableMapRef(NewT, Factory);
325   }
326
327   ImmutableMapRef remove(key_type_ref K) const {
328     TreeTy *NewT = Factory->remove(Root, K);
329     return ImmutableMapRef(NewT, Factory);
330   }
331
332   bool contains(key_type_ref K) const {
333     return Root ? Root->contains(K) : false;
334   }
335
336   ImmutableMap<KeyT, ValT> asImmutableMap() const {
337     return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
338   }
339
340   bool operator==(const ImmutableMapRef &RHS) const {
341     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
342   }
343
344   bool operator!=(const ImmutableMapRef &RHS) const {
345     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
346   }
347
348   bool isEmpty() const { return !Root; }
349
350   //===--------------------------------------------------===//
351   // For testing.
352   //===--------------------------------------------------===//
353
354   void verify() const { if (Root) Root->verify(); }
355
356   //===--------------------------------------------------===//
357   // Iterators.
358   //===--------------------------------------------------===//
359
360   class iterator : public ImutAVLValueIterator<ImmutableMapRef> {
361     iterator() = default;
362     explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
363     friend class ImmutableMapRef;
364
365   public:
366     key_type_ref getKey() const { return (*this)->first; }
367     data_type_ref getData() const { return (*this)->second; }
368   };
369
370   iterator begin() const { return iterator(Root); }
371   iterator end() const { return iterator(); }
372
373   data_type* lookup(key_type_ref K) const {
374     if (Root) {
375       TreeTy* T = Root->find(K);
376       if (T) return &T->getValue().second;
377     }
378
379     return 0;
380   }
381
382   /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
383   ///  which key is the highest in the ordering of keys in the map.  This
384   ///  method returns NULL if the map is empty.
385   value_type* getMaxElement() const {
386     return Root ? &(Root->getMaxElement()->getValue()) : 0;
387   }
388
389   //===--------------------------------------------------===//
390   // Utility methods.
391   //===--------------------------------------------------===//
392
393   unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
394
395   static inline void Profile(FoldingSetNodeID& ID, const ImmutableMapRef &M) {
396     ID.AddPointer(M.Root);
397   }
398
399   inline void Profile(FoldingSetNodeID& ID) const {
400     return Profile(ID, *this);
401   }
402 };
403
404 } // end namespace llvm
405
406 #endif