]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/ADT/ImmutableMap.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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_IMMAP_H
15 #define LLVM_ADT_IMMAP_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
59 template <typename KeyT, typename ValT,
60           typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
61 class ImmutableMap {
62 public:
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
71 protected:
72   TreeTy* Root;
73
74 public:
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 Canonicalize;
100
101   public:
102     Factory(bool canonicalize = true)
103       : Canonicalize(canonicalize) {}
104     
105     Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
106       : F(Alloc), Canonicalize(canonicalize) {}
107
108     ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
109
110     ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
111       TreeTy *T = F.add(Old.Root, std::pair<key_type,data_type>(K,D));
112       return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
113     }
114
115     ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
116       TreeTy *T = F.remove(Old.Root,K);
117       return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
118     }
119
120   private:
121     Factory(const Factory& RHS); // DO NOT IMPLEMENT
122     void operator=(const Factory& RHS); // DO NOT IMPLEMENT
123   };
124
125   bool contains(key_type_ref K) const {
126     return Root ? Root->contains(K) : false;
127   }
128
129   bool operator==(const ImmutableMap &RHS) const {
130     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
131   }
132
133   bool operator!=(const ImmutableMap &RHS) const {
134     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
135   }
136
137   TreeTy *getRoot() const {
138     if (Root) { Root->retain(); }
139     return Root;
140   }
141
142   TreeTy *getRootWithoutRetain() const {
143     return Root;
144   }
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 {
203     typename TreeTy::iterator itr;
204
205     iterator() {}
206     iterator(TreeTy* t) : itr(t) {}
207     friend class ImmutableMap;
208
209   public:
210     value_type_ref operator*() const { return itr->getValue(); }
211     value_type*    operator->() const { return &itr->getValue(); }
212
213     key_type_ref getKey() const { return itr->getValue().first; }
214     data_type_ref getData() const { return itr->getValue().second; }
215
216
217     iterator& operator++() { ++itr; return *this; }
218     iterator  operator++(int) { iterator tmp(*this); ++itr; return tmp; }
219     iterator& operator--() { --itr; return *this; }
220     iterator  operator--(int) { iterator tmp(*this); --itr; return tmp; }
221     bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
222     bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
223   };
224
225   iterator begin() const { return iterator(Root); }
226   iterator end() const { return iterator(); }
227
228   data_type* lookup(key_type_ref K) const {
229     if (Root) {
230       TreeTy* T = Root->find(K);
231       if (T) return &T->getValue().second;
232     }
233
234     return 0;
235   }
236   
237   /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
238   ///  which key is the highest in the ordering of keys in the map.  This
239   ///  method returns NULL if the map is empty.
240   value_type* getMaxElement() const {
241     return Root ? &(Root->getMaxElement()->getValue()) : 0;
242   }
243
244   //===--------------------------------------------------===//
245   // Utility methods.
246   //===--------------------------------------------------===//
247
248   unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
249
250   static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
251     ID.AddPointer(M.Root);
252   }
253
254   inline void Profile(FoldingSetNodeID& ID) const {
255     return Profile(ID,*this);
256   }
257 };
258
259 } // end namespace llvm
260
261 #endif