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);
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 Canonicalize;
102 Factory(bool canonicalize = true)
103 : Canonicalize(canonicalize) {}
105 Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
106 : F(Alloc), Canonicalize(canonicalize) {}
108 ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
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);
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);
120 typename TreeTy::Factory *getTreeFactory() const {
121 return const_cast<typename TreeTy::Factory *>(&F);
125 Factory(const Factory& RHS) LLVM_DELETED_FUNCTION;
126 void operator=(const Factory& RHS) LLVM_DELETED_FUNCTION;
129 bool contains(key_type_ref K) const {
130 return Root ? Root->contains(K) : false;
133 bool operator==(const ImmutableMap &RHS) const {
134 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
137 bool operator!=(const ImmutableMap &RHS) const {
138 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
141 TreeTy *getRoot() const {
142 if (Root) { Root->retain(); }
146 TreeTy *getRootWithoutRetain() const {
150 void manualRetain() {
151 if (Root) Root->retain();
154 void manualRelease() {
155 if (Root) Root->release();
158 bool isEmpty() const { return !Root; }
160 //===--------------------------------------------------===//
161 // Foreach - A limited form of map iteration.
162 //===--------------------------------------------------===//
165 template <typename Callback>
168 void operator()(value_type_ref V) { C(V.first,V.second); }
171 template <typename Callback>
172 struct CBWrapperRef {
174 CBWrapperRef(Callback& c) : C(c) {}
176 void operator()(value_type_ref V) { C(V.first,V.second); }
180 template <typename Callback>
181 void foreach(Callback& C) {
183 CBWrapperRef<Callback> CB(C);
188 template <typename Callback>
191 CBWrapper<Callback> CB;
196 //===--------------------------------------------------===//
198 //===--------------------------------------------------===//
200 void verify() const { if (Root) Root->verify(); }
202 //===--------------------------------------------------===//
204 //===--------------------------------------------------===//
207 typename TreeTy::iterator itr;
210 iterator(TreeTy* t) : itr(t) {}
211 friend class ImmutableMap;
214 typedef ptrdiff_t difference_type;
215 typedef typename ImmutableMap<KeyT,ValT,ValInfo>::value_type value_type;
216 typedef typename ImmutableMap<KeyT,ValT,ValInfo>::value_type_ref reference;
217 typedef typename iterator::value_type *pointer;
218 typedef std::bidirectional_iterator_tag iterator_category;
220 typename iterator::reference operator*() const { return itr->getValue(); }
221 typename iterator::pointer operator->() const { return &itr->getValue(); }
223 key_type_ref getKey() const { return itr->getValue().first; }
224 data_type_ref getData() const { return itr->getValue().second; }
226 iterator& operator++() { ++itr; return *this; }
227 iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
228 iterator& operator--() { --itr; return *this; }
229 iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
231 bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
232 bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
235 iterator begin() const { return iterator(Root); }
236 iterator end() const { return iterator(); }
238 data_type* lookup(key_type_ref K) const {
240 TreeTy* T = Root->find(K);
241 if (T) return &T->getValue().second;
247 /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
248 /// which key is the highest in the ordering of keys in the map. This
249 /// method returns NULL if the map is empty.
250 value_type* getMaxElement() const {
251 return Root ? &(Root->getMaxElement()->getValue()) : nullptr;
254 //===--------------------------------------------------===//
256 //===--------------------------------------------------===//
258 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
260 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
261 ID.AddPointer(M.Root);
264 inline void Profile(FoldingSetNodeID& ID) const {
265 return Profile(ID,*this);
269 // NOTE: This will possibly become the new implementation of ImmutableMap some day.
270 template <typename KeyT, typename ValT,
271 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
272 class ImmutableMapRef {
274 typedef typename ValInfo::value_type value_type;
275 typedef typename ValInfo::value_type_ref value_type_ref;
276 typedef typename ValInfo::key_type key_type;
277 typedef typename ValInfo::key_type_ref key_type_ref;
278 typedef typename ValInfo::data_type data_type;
279 typedef typename ValInfo::data_type_ref data_type_ref;
280 typedef ImutAVLTree<ValInfo> TreeTy;
281 typedef typename TreeTy::Factory FactoryTy;
288 /// Constructs a map from a pointer to a tree root. In general one
289 /// should use a Factory object to create maps instead of directly
290 /// invoking the constructor, but there are cases where make this
291 /// constructor public is useful.
292 explicit ImmutableMapRef(const TreeTy* R, FactoryTy *F)
293 : Root(const_cast<TreeTy*>(R)),
295 if (Root) { Root->retain(); }
298 explicit ImmutableMapRef(const ImmutableMap<KeyT, ValT> &X,
299 typename ImmutableMap<KeyT, ValT>::Factory &F)
300 : Root(X.getRootWithoutRetain()),
301 Factory(F.getTreeFactory()) {
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 void manualRetain() {
335 if (Root) Root->retain();
338 void manualRelease() {
339 if (Root) Root->release();
342 ImmutableMapRef add(key_type_ref K, data_type_ref D) const {
343 TreeTy *NewT = Factory->add(Root, std::pair<key_type, data_type>(K, D));
344 return ImmutableMapRef(NewT, Factory);
347 ImmutableMapRef remove(key_type_ref K) const {
348 TreeTy *NewT = Factory->remove(Root, K);
349 return ImmutableMapRef(NewT, Factory);
352 bool contains(key_type_ref K) const {
353 return Root ? Root->contains(K) : false;
356 ImmutableMap<KeyT, ValT> asImmutableMap() const {
357 return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
360 bool operator==(const ImmutableMapRef &RHS) const {
361 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
364 bool operator!=(const ImmutableMapRef &RHS) const {
365 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
368 bool isEmpty() const { return !Root; }
370 //===--------------------------------------------------===//
372 //===--------------------------------------------------===//
374 void verify() const { if (Root) Root->verify(); }
376 //===--------------------------------------------------===//
378 //===--------------------------------------------------===//
381 typename TreeTy::iterator itr;
384 iterator(TreeTy* t) : itr(t) {}
385 friend class ImmutableMapRef;
388 value_type_ref operator*() const { return itr->getValue(); }
389 value_type* operator->() const { return &itr->getValue(); }
391 key_type_ref getKey() const { return itr->getValue().first; }
392 data_type_ref getData() const { return itr->getValue().second; }
395 iterator& operator++() { ++itr; return *this; }
396 iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
397 iterator& operator--() { --itr; return *this; }
398 iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
399 bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
400 bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
403 iterator begin() const { return iterator(Root); }
404 iterator end() const { return iterator(); }
406 data_type* lookup(key_type_ref K) const {
408 TreeTy* T = Root->find(K);
409 if (T) return &T->getValue().second;
415 /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
416 /// which key is the highest in the ordering of keys in the map. This
417 /// method returns NULL if the map is empty.
418 value_type* getMaxElement() const {
419 return Root ? &(Root->getMaxElement()->getValue()) : 0;
422 //===--------------------------------------------------===//
424 //===--------------------------------------------------===//
426 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
428 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMapRef &M) {
429 ID.AddPointer(M.Root);
432 inline void Profile(FoldingSetNodeID& ID) const {
433 return Profile(ID, *this);
437 } // end namespace llvm