[ADT] Add a single-character version of the small vector split routine
[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) : Canonicalize(canonicalize) {}
102
103     Factory(BumpPtrAllocator &Alloc, bool canonicalize = true)
104         : F(Alloc), Canonicalize(canonicalize) {}
105
106     ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
107
108     ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
109       TreeTy *T = F.add(Old.Root, std::pair<key_type,data_type>(K,D));
110       return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
111     }
112
113     ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
114       TreeTy *T = F.remove(Old.Root,K);
115       return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
116     }
117
118     typename TreeTy::Factory *getTreeFactory() const {
119       return const_cast<typename TreeTy::Factory *>(&F);
120     }
121
122   private:
123     Factory(const Factory& RHS) = delete;
124     void operator=(const Factory& RHS) = delete;
125   };
126
127   bool contains(key_type_ref K) const {
128     return Root ? Root->contains(K) : false;
129   }
130
131   bool operator==(const ImmutableMap &RHS) const {
132     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
133   }
134
135   bool operator!=(const ImmutableMap &RHS) const {
136     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
137   }
138
139   TreeTy *getRoot() const {
140     if (Root) { Root->retain(); }
141     return Root;
142   }
143
144   TreeTy *getRootWithoutRetain() const { return Root; }
145
146   void manualRetain() {
147     if (Root) Root->retain();
148   }
149
150   void manualRelease() {
151     if (Root) Root->release();
152   }
153
154   bool isEmpty() const { return !Root; }
155
156   //===--------------------------------------------------===//
157   // Foreach - A limited form of map iteration.
158   //===--------------------------------------------------===//
159
160 private:
161   template <typename Callback>
162   struct CBWrapper {
163     Callback C;
164     void operator()(value_type_ref V) { C(V.first,V.second); }
165   };
166
167   template <typename Callback>
168   struct CBWrapperRef {
169     Callback &C;
170     CBWrapperRef(Callback& c) : C(c) {}
171
172     void operator()(value_type_ref V) { C(V.first,V.second); }
173   };
174
175 public:
176   template <typename Callback>
177   void foreach(Callback& C) {
178     if (Root) {
179       CBWrapperRef<Callback> CB(C);
180       Root->foreach(CB);
181     }
182   }
183
184   template <typename Callback>
185   void foreach() {
186     if (Root) {
187       CBWrapper<Callback> CB;
188       Root->foreach(CB);
189     }
190   }
191
192   //===--------------------------------------------------===//
193   // For testing.
194   //===--------------------------------------------------===//
195
196   void verify() const { if (Root) Root->verify(); }
197
198   //===--------------------------------------------------===//
199   // Iterators.
200   //===--------------------------------------------------===//
201
202   class iterator : public ImutAVLValueIterator<ImmutableMap> {
203     iterator() = default;
204     explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
205     friend class ImmutableMap;
206
207   public:
208     key_type_ref getKey() const { return (*this)->first; }
209     data_type_ref getData() const { return (*this)->second; }
210   };
211
212   iterator begin() const { return iterator(Root); }
213   iterator end() const { return iterator(); }
214
215   data_type* lookup(key_type_ref K) const {
216     if (Root) {
217       TreeTy* T = Root->find(K);
218       if (T) return &T->getValue().second;
219     }
220
221     return nullptr;
222   }
223
224   /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
225   ///  which key is the highest in the ordering of keys in the map.  This
226   ///  method returns NULL if the map is empty.
227   value_type* getMaxElement() const {
228     return Root ? &(Root->getMaxElement()->getValue()) : nullptr;
229   }
230
231   //===--------------------------------------------------===//
232   // Utility methods.
233   //===--------------------------------------------------===//
234
235   unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
236
237   static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
238     ID.AddPointer(M.Root);
239   }
240
241   inline void Profile(FoldingSetNodeID& ID) const {
242     return Profile(ID,*this);
243   }
244 };
245
246 // NOTE: This will possibly become the new implementation of ImmutableMap some day.
247 template <typename KeyT, typename ValT,
248 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
249 class ImmutableMapRef {
250 public:
251   typedef typename ValInfo::value_type      value_type;
252   typedef typename ValInfo::value_type_ref  value_type_ref;
253   typedef typename ValInfo::key_type        key_type;
254   typedef typename ValInfo::key_type_ref    key_type_ref;
255   typedef typename ValInfo::data_type       data_type;
256   typedef typename ValInfo::data_type_ref   data_type_ref;
257   typedef ImutAVLTree<ValInfo>              TreeTy;
258   typedef typename TreeTy::Factory          FactoryTy;
259
260 protected:
261   TreeTy *Root;
262   FactoryTy *Factory;
263
264 public:
265   /// Constructs a map from a pointer to a tree root.  In general one
266   /// should use a Factory object to create maps instead of directly
267   /// invoking the constructor, but there are cases where make this
268   /// constructor public is useful.
269   explicit ImmutableMapRef(const TreeTy *R, FactoryTy *F)
270       : Root(const_cast<TreeTy *>(R)), Factory(F) {
271     if (Root) {
272       Root->retain();
273     }
274   }
275
276   explicit ImmutableMapRef(const ImmutableMap<KeyT, ValT> &X,
277                            typename ImmutableMap<KeyT, ValT>::Factory &F)
278     : Root(X.getRootWithoutRetain()),
279       Factory(F.getTreeFactory()) {
280     if (Root) { Root->retain(); }
281   }
282
283   ImmutableMapRef(const ImmutableMapRef &X) : Root(X.Root), Factory(X.Factory) {
284     if (Root) {
285       Root->retain();
286     }
287   }
288
289   ImmutableMapRef &operator=(const ImmutableMapRef &X) {
290     if (Root != X.Root) {
291       if (X.Root)
292         X.Root->retain();
293
294       if (Root)
295         Root->release();
296
297       Root = X.Root;
298       Factory = X.Factory;
299     }
300     return *this;
301   }
302
303   ~ImmutableMapRef() {
304     if (Root)
305       Root->release();
306   }
307
308   static inline ImmutableMapRef getEmptyMap(FactoryTy *F) {
309     return ImmutableMapRef(0, F);
310   }
311
312   void manualRetain() {
313     if (Root) Root->retain();
314   }
315
316   void manualRelease() {
317     if (Root) Root->release();
318   }
319
320   ImmutableMapRef add(key_type_ref K, data_type_ref D) const {
321     TreeTy *NewT = Factory->add(Root, std::pair<key_type, data_type>(K, D));
322     return ImmutableMapRef(NewT, Factory);
323   }
324
325   ImmutableMapRef remove(key_type_ref K) const {
326     TreeTy *NewT = Factory->remove(Root, K);
327     return ImmutableMapRef(NewT, Factory);
328   }
329
330   bool contains(key_type_ref K) const {
331     return Root ? Root->contains(K) : false;
332   }
333
334   ImmutableMap<KeyT, ValT> asImmutableMap() const {
335     return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
336   }
337
338   bool operator==(const ImmutableMapRef &RHS) const {
339     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
340   }
341
342   bool operator!=(const ImmutableMapRef &RHS) const {
343     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
344   }
345
346   bool isEmpty() const { return !Root; }
347
348   //===--------------------------------------------------===//
349   // For testing.
350   //===--------------------------------------------------===//
351
352   void verify() const {
353     if (Root)
354       Root->verify();
355   }
356
357   //===--------------------------------------------------===//
358   // Iterators.
359   //===--------------------------------------------------===//
360
361   class iterator : public ImutAVLValueIterator<ImmutableMapRef> {
362     iterator() = default;
363     explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
364     friend class ImmutableMapRef;
365
366   public:
367     key_type_ref getKey() const { return (*this)->first; }
368     data_type_ref getData() const { return (*this)->second; }
369   };
370
371   iterator begin() const { return iterator(Root); }
372   iterator end() const { return iterator(); }
373
374   data_type *lookup(key_type_ref K) const {
375     if (Root) {
376       TreeTy* T = Root->find(K);
377       if (T) return &T->getValue().second;
378     }
379
380     return 0;
381   }
382
383   /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
384   ///  which key is the highest in the ordering of keys in the map.  This
385   ///  method returns NULL if the map is empty.
386   value_type* getMaxElement() const {
387     return Root ? &(Root->getMaxElement()->getValue()) : 0;
388   }
389
390   //===--------------------------------------------------===//
391   // Utility methods.
392   //===--------------------------------------------------===//
393
394   unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
395
396   static inline void Profile(FoldingSetNodeID &ID, const ImmutableMapRef &M) {
397     ID.AddPointer(M.Root);
398   }
399
400   inline void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
401 };
402
403 } // end namespace llvm
404
405 #endif