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