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_IMMAP_H
15 #define LLVM_ADT_IMMAP_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);
59 template <typename KeyT, typename ValT,
60 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
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;
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(); }
82 ImmutableMap(const ImmutableMap &X) : Root(X.Root) {
83 if (Root) { Root->retain(); }
85 ImmutableMap &operator=(const ImmutableMap &X) {
87 if (X.Root) { X.Root->retain(); }
88 if (Root) { Root->release(); }
94 if (Root) { Root->release(); }
98 typename TreeTy::Factory F;
99 const bool Canonicalizing;
102 Factory(bool canonicalize = true)
103 : Canonicalizing(canonicalize) {}
105 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
106 : F(Alloc), Canonicalizing(canonicalize) {}
108 ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
110 ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D,
112 TreeTy *T = F.add(Old.Root, std::pair<key_type,data_type>(K,D));
113 return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
116 ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
117 return add(Old, K, D, Canonicalizing);
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);
125 ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
126 return remove(Old, K, Canonicalizing);
129 ImmutableMap getCanonicalMap(ImmutableMap Map) {
130 return ImmutableMap(F.getCanonicalTree(Map.Root));
133 typename TreeTy::Factory *getTreeFactory() const {
134 return const_cast<typename TreeTy::Factory *>(&F);
138 Factory(const Factory& RHS) LLVM_DELETED_FUNCTION;
139 void operator=(const Factory& RHS) LLVM_DELETED_FUNCTION;
142 bool contains(key_type_ref K) const {
143 return Root ? Root->contains(K) : false;
146 bool operator==(const ImmutableMap &RHS) const {
147 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
150 bool operator!=(const ImmutableMap &RHS) const {
151 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
154 TreeTy *getRoot() const {
155 if (Root) { Root->retain(); }
159 TreeTy *getRootWithoutRetain() const {
163 void manualRetain() {
164 if (Root) Root->retain();
167 void manualRelease() {
168 if (Root) Root->release();
171 bool isEmpty() const { return !Root; }
173 //===--------------------------------------------------===//
174 // Foreach - A limited form of map iteration.
175 //===--------------------------------------------------===//
178 template <typename Callback>
181 void operator()(value_type_ref V) { C(V.first,V.second); }
184 template <typename Callback>
185 struct CBWrapperRef {
187 CBWrapperRef(Callback& c) : C(c) {}
189 void operator()(value_type_ref V) { C(V.first,V.second); }
193 template <typename Callback>
194 void foreach(Callback& C) {
196 CBWrapperRef<Callback> CB(C);
201 template <typename Callback>
204 CBWrapper<Callback> CB;
209 //===--------------------------------------------------===//
211 //===--------------------------------------------------===//
213 void verify() const { if (Root) Root->verify(); }
215 //===--------------------------------------------------===//
217 //===--------------------------------------------------===//
220 typename TreeTy::iterator itr;
223 iterator(TreeTy* t) : itr(t) {}
224 friend class ImmutableMap;
227 value_type_ref operator*() const { return itr->getValue(); }
228 value_type* operator->() const { return &itr->getValue(); }
230 key_type_ref getKey() const { return itr->getValue().first; }
231 data_type_ref getData() const { return itr->getValue().second; }
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; }
242 iterator begin() const { return iterator(Root); }
243 iterator end() const { return iterator(); }
245 data_type* lookup(key_type_ref K) const {
247 TreeTy* T = Root->find(K);
248 if (T) return &T->getValue().second;
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;
261 //===--------------------------------------------------===//
263 //===--------------------------------------------------===//
265 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
267 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
268 ID.AddPointer(M.Root);
271 inline void Profile(FoldingSetNodeID& ID) const {
272 return Profile(ID,*this);
276 // NOTE: This will possibly become the new implementation of ImmutableMap some day.
277 template <typename KeyT, typename ValT,
278 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
279 class ImmutableMapRef {
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;
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)),
302 if (Root) { Root->retain(); }
305 ImmutableMapRef(const ImmutableMapRef &X)
308 if (Root) { Root->retain(); }
311 ImmutableMapRef &operator=(const ImmutableMapRef &X) {
312 if (Root != X.Root) {
330 static inline ImmutableMapRef getEmptyMap(FactoryTy *F) {
331 return ImmutableMapRef(0, F);
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);
339 ImmutableMapRef remove(key_type_ref K) {
340 TreeTy *NewT = Factory->remove(Root, K);
341 return ImmutableMapRef(NewT, Factory);
344 bool contains(key_type_ref K) const {
345 return Root ? Root->contains(K) : false;
348 ImmutableMap<KeyT, ValT> asImmutableMap() const {
349 return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
352 bool operator==(const ImmutableMapRef &RHS) const {
353 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
356 bool operator!=(const ImmutableMapRef &RHS) const {
357 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
360 bool isEmpty() const { return !Root; }
362 //===--------------------------------------------------===//
364 //===--------------------------------------------------===//
366 void verify() const { if (Root) Root->verify(); }
368 //===--------------------------------------------------===//
370 //===--------------------------------------------------===//
373 typename TreeTy::iterator itr;
376 iterator(TreeTy* t) : itr(t) {}
377 friend class ImmutableMapRef;
380 value_type_ref operator*() const { return itr->getValue(); }
381 value_type* operator->() const { return &itr->getValue(); }
383 key_type_ref getKey() const { return itr->getValue().first; }
384 data_type_ref getData() const { return itr->getValue().second; }
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; }
395 iterator begin() const { return iterator(Root); }
396 iterator end() const { return iterator(); }
398 data_type* lookup(key_type_ref K) const {
400 TreeTy* T = Root->find(K);
401 if (T) return &T->getValue().second;
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;
414 //===--------------------------------------------------===//
416 //===--------------------------------------------------===//
418 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
420 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMapRef &M) {
421 ID.AddPointer(M.Root);
424 inline void Profile(FoldingSetNodeID& ID) const {
425 return Profile(ID, *this);
429 } // end namespace llvm