1 //===--- ImmutableMap.h - Immutable (functional) map interface --*- 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 ImmutableMap class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_IMMUTABLEMAP_H
15 #define LLVM_ADT_IMMUTABLEMAP_H
17 #include "llvm/ADT/ImmutableSet.h"
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;
33 static inline key_type_ref KeyOfValue(value_type_ref V) {
37 static inline data_type_ref DataOfValue(value_type_ref V) {
41 static inline bool isEqual(key_type_ref L, key_type_ref R) {
42 return ImutContainerInfo<T>::isEqual(L,R);
44 static inline bool isLess(key_type_ref L, key_type_ref R) {
45 return ImutContainerInfo<T>::isLess(L,R);
48 static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
49 return ImutContainerInfo<S>::isEqual(L,R);
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);
58 template <typename KeyT, typename ValT,
59 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
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;
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(); }
81 ImmutableMap(const ImmutableMap &X) : Root(X.Root) {
82 if (Root) { Root->retain(); }
84 ImmutableMap &operator=(const ImmutableMap &X) {
86 if (X.Root) { X.Root->retain(); }
87 if (Root) { Root->release(); }
93 if (Root) { Root->release(); }
97 typename TreeTy::Factory F;
98 const bool Canonicalize;
101 Factory(bool canonicalize = true)
102 : Canonicalize(canonicalize) {}
104 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
105 : F(Alloc), Canonicalize(canonicalize) {}
107 ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
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);
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);
119 typename TreeTy::Factory *getTreeFactory() const {
120 return const_cast<typename TreeTy::Factory *>(&F);
124 Factory(const Factory& RHS) = delete;
125 void operator=(const Factory& RHS) = delete;
128 bool contains(key_type_ref K) const {
129 return Root ? Root->contains(K) : false;
132 bool operator==(const ImmutableMap &RHS) const {
133 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
136 bool operator!=(const ImmutableMap &RHS) const {
137 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
140 TreeTy *getRoot() const {
141 if (Root) { Root->retain(); }
145 TreeTy *getRootWithoutRetain() const {
149 void manualRetain() {
150 if (Root) Root->retain();
153 void manualRelease() {
154 if (Root) Root->release();
157 bool isEmpty() const { return !Root; }
159 //===--------------------------------------------------===//
160 // Foreach - A limited form of map iteration.
161 //===--------------------------------------------------===//
164 template <typename Callback>
167 void operator()(value_type_ref V) { C(V.first,V.second); }
170 template <typename Callback>
171 struct CBWrapperRef {
173 CBWrapperRef(Callback& c) : C(c) {}
175 void operator()(value_type_ref V) { C(V.first,V.second); }
179 template <typename Callback>
180 void foreach(Callback& C) {
182 CBWrapperRef<Callback> CB(C);
187 template <typename Callback>
190 CBWrapper<Callback> CB;
195 //===--------------------------------------------------===//
197 //===--------------------------------------------------===//
199 void verify() const { if (Root) Root->verify(); }
201 //===--------------------------------------------------===//
203 //===--------------------------------------------------===//
205 class iterator : public ImutAVLValueIterator<ImmutableMap> {
206 iterator() = default;
207 explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
208 friend class ImmutableMap;
211 key_type_ref getKey() const { return (*this)->first; }
212 data_type_ref getData() const { return (*this)->second; }
215 iterator begin() const { return iterator(Root); }
216 iterator end() const { return iterator(); }
218 data_type* lookup(key_type_ref K) const {
220 TreeTy* T = Root->find(K);
221 if (T) return &T->getValue().second;
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;
234 //===--------------------------------------------------===//
236 //===--------------------------------------------------===//
238 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
240 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
241 ID.AddPointer(M.Root);
244 inline void Profile(FoldingSetNodeID& ID) const {
245 return Profile(ID,*this);
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 {
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;
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)),
275 if (Root) { Root->retain(); }
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(); }
285 ImmutableMapRef(const ImmutableMapRef &X)
288 if (Root) { Root->retain(); }
291 ImmutableMapRef &operator=(const ImmutableMapRef &X) {
292 if (Root != X.Root) {
310 static inline ImmutableMapRef getEmptyMap(FactoryTy *F) {
311 return ImmutableMapRef(0, F);
314 void manualRetain() {
315 if (Root) Root->retain();
318 void manualRelease() {
319 if (Root) Root->release();
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);
327 ImmutableMapRef remove(key_type_ref K) const {
328 TreeTy *NewT = Factory->remove(Root, K);
329 return ImmutableMapRef(NewT, Factory);
332 bool contains(key_type_ref K) const {
333 return Root ? Root->contains(K) : false;
336 ImmutableMap<KeyT, ValT> asImmutableMap() const {
337 return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
340 bool operator==(const ImmutableMapRef &RHS) const {
341 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
344 bool operator!=(const ImmutableMapRef &RHS) const {
345 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
348 bool isEmpty() const { return !Root; }
350 //===--------------------------------------------------===//
352 //===--------------------------------------------------===//
354 void verify() const { if (Root) Root->verify(); }
356 //===--------------------------------------------------===//
358 //===--------------------------------------------------===//
360 class iterator : public ImutAVLValueIterator<ImmutableMapRef> {
361 iterator() = default;
362 explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
363 friend class ImmutableMapRef;
366 key_type_ref getKey() const { return (*this)->first; }
367 data_type_ref getData() const { return (*this)->second; }
370 iterator begin() const { return iterator(Root); }
371 iterator end() const { return iterator(); }
373 data_type* lookup(key_type_ref K) const {
375 TreeTy* T = Root->find(K);
376 if (T) return &T->getValue().second;
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;
389 //===--------------------------------------------------===//
391 //===--------------------------------------------------===//
393 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
395 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMapRef &M) {
396 ID.AddPointer(M.Root);
399 inline void Profile(FoldingSetNodeID& ID) const {
400 return Profile(ID, *this);
404 } // end namespace llvm