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/FoldingSet.h"
18 #include "llvm/ADT/ImmutableSet.h"
19 #include "llvm/Support/Allocator.h"
24 /// ImutKeyValueInfo -Traits class used by ImmutableMap. While both the first
25 /// and second elements in a pair are used to generate profile information,
26 /// only the first element (the key) is used by isEqual and isLess.
27 template <typename T, typename S>
28 struct ImutKeyValueInfo {
29 using value_type = const std::pair<T,S>;
30 using value_type_ref = const value_type&;
31 using key_type = const T;
32 using key_type_ref = const T&;
33 using data_type = const S;
34 using data_type_ref = const S&;
36 static inline key_type_ref KeyOfValue(value_type_ref V) {
40 static inline data_type_ref DataOfValue(value_type_ref V) {
44 static inline bool isEqual(key_type_ref L, key_type_ref R) {
45 return ImutContainerInfo<T>::isEqual(L,R);
47 static inline bool isLess(key_type_ref L, key_type_ref R) {
48 return ImutContainerInfo<T>::isLess(L,R);
51 static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
52 return ImutContainerInfo<S>::isEqual(L,R);
55 static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
56 ImutContainerInfo<T>::Profile(ID, V.first);
57 ImutContainerInfo<S>::Profile(ID, V.second);
61 template <typename KeyT, typename ValT,
62 typename ValInfo = ImutKeyValueInfo<KeyT,ValT>>
65 using value_type = typename ValInfo::value_type;
66 using value_type_ref = typename ValInfo::value_type_ref;
67 using key_type = typename ValInfo::key_type;
68 using key_type_ref = typename ValInfo::key_type_ref;
69 using data_type = typename ValInfo::data_type;
70 using data_type_ref = typename ValInfo::data_type_ref;
71 using TreeTy = ImutAVLTree<ValInfo>;
77 /// Constructs a map from a pointer to a tree root. In general one
78 /// should use a Factory object to create maps instead of directly
79 /// invoking the constructor, but there are cases where make this
80 /// constructor public is useful.
81 explicit ImmutableMap(const TreeTy* R) : Root(const_cast<TreeTy*>(R)) {
82 if (Root) { Root->retain(); }
85 ImmutableMap(const ImmutableMap &X) : Root(X.Root) {
86 if (Root) { Root->retain(); }
90 if (Root) { Root->release(); }
93 ImmutableMap &operator=(const ImmutableMap &X) {
95 if (X.Root) { X.Root->retain(); }
96 if (Root) { Root->release(); }
103 typename TreeTy::Factory F;
104 const bool Canonicalize;
107 Factory(bool canonicalize = true) : Canonicalize(canonicalize) {}
109 Factory(BumpPtrAllocator &Alloc, bool canonicalize = true)
110 : F(Alloc), Canonicalize(canonicalize) {}
112 Factory(const Factory &) = delete;
113 Factory &operator=(const Factory &) = delete;
115 ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
117 LLVM_NODISCARD ImmutableMap add(ImmutableMap Old, key_type_ref K,
119 TreeTy *T = F.add(Old.Root, std::pair<key_type,data_type>(K,D));
120 return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
123 LLVM_NODISCARD ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
124 TreeTy *T = F.remove(Old.Root,K);
125 return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
128 typename TreeTy::Factory *getTreeFactory() const {
129 return const_cast<typename TreeTy::Factory *>(&F);
133 bool contains(key_type_ref K) const {
134 return Root ? Root->contains(K) : false;
137 bool operator==(const ImmutableMap &RHS) const {
138 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
141 bool operator!=(const ImmutableMap &RHS) const {
142 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
145 TreeTy *getRoot() const {
146 if (Root) { Root->retain(); }
150 TreeTy *getRootWithoutRetain() const { return Root; }
152 void manualRetain() {
153 if (Root) Root->retain();
156 void manualRelease() {
157 if (Root) Root->release();
160 bool isEmpty() const { return !Root; }
162 //===--------------------------------------------------===//
163 // Foreach - A limited form of map iteration.
164 //===--------------------------------------------------===//
167 template <typename Callback>
171 void operator()(value_type_ref V) { C(V.first,V.second); }
174 template <typename Callback>
175 struct CBWrapperRef {
178 CBWrapperRef(Callback& c) : C(c) {}
180 void operator()(value_type_ref V) { C(V.first,V.second); }
184 template <typename Callback>
185 void foreach(Callback& C) {
187 CBWrapperRef<Callback> CB(C);
192 template <typename Callback>
195 CBWrapper<Callback> CB;
200 //===--------------------------------------------------===//
202 //===--------------------------------------------------===//
204 void verify() const { if (Root) Root->verify(); }
206 //===--------------------------------------------------===//
208 //===--------------------------------------------------===//
210 class iterator : public ImutAVLValueIterator<ImmutableMap> {
211 friend class ImmutableMap;
213 iterator() = default;
214 explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
217 key_type_ref getKey() const { return (*this)->first; }
218 data_type_ref getData() const { return (*this)->second; }
221 iterator begin() const { return iterator(Root); }
222 iterator end() const { return iterator(); }
224 data_type* lookup(key_type_ref K) const {
226 TreeTy* T = Root->find(K);
227 if (T) return &T->getValue().second;
233 /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
234 /// which key is the highest in the ordering of keys in the map. This
235 /// method returns NULL if the map is empty.
236 value_type* getMaxElement() const {
237 return Root ? &(Root->getMaxElement()->getValue()) : nullptr;
240 //===--------------------------------------------------===//
242 //===--------------------------------------------------===//
244 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
246 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
247 ID.AddPointer(M.Root);
250 inline void Profile(FoldingSetNodeID& ID) const {
251 return Profile(ID,*this);
255 // NOTE: This will possibly become the new implementation of ImmutableMap some day.
256 template <typename KeyT, typename ValT,
257 typename ValInfo = ImutKeyValueInfo<KeyT,ValT>>
258 class ImmutableMapRef {
260 using value_type = typename ValInfo::value_type;
261 using value_type_ref = typename ValInfo::value_type_ref;
262 using key_type = typename ValInfo::key_type;
263 using key_type_ref = typename ValInfo::key_type_ref;
264 using data_type = typename ValInfo::data_type;
265 using data_type_ref = typename ValInfo::data_type_ref;
266 using TreeTy = ImutAVLTree<ValInfo>;
267 using FactoryTy = typename TreeTy::Factory;
274 /// Constructs a map from a pointer to a tree root. In general one
275 /// should use a Factory object to create maps instead of directly
276 /// invoking the constructor, but there are cases where make this
277 /// constructor public is useful.
278 explicit ImmutableMapRef(const TreeTy *R, FactoryTy *F)
279 : Root(const_cast<TreeTy *>(R)), Factory(F) {
285 explicit ImmutableMapRef(const ImmutableMap<KeyT, ValT> &X,
286 typename ImmutableMap<KeyT, ValT>::Factory &F)
287 : Root(X.getRootWithoutRetain()),
288 Factory(F.getTreeFactory()) {
289 if (Root) { Root->retain(); }
292 ImmutableMapRef(const ImmutableMapRef &X) : Root(X.Root), Factory(X.Factory) {
303 ImmutableMapRef &operator=(const ImmutableMapRef &X) {
304 if (Root != X.Root) {
317 static inline ImmutableMapRef getEmptyMap(FactoryTy *F) {
318 return ImmutableMapRef(0, F);
321 void manualRetain() {
322 if (Root) Root->retain();
325 void manualRelease() {
326 if (Root) Root->release();
329 ImmutableMapRef add(key_type_ref K, data_type_ref D) const {
330 TreeTy *NewT = Factory->add(Root, std::pair<key_type, data_type>(K, D));
331 return ImmutableMapRef(NewT, Factory);
334 ImmutableMapRef remove(key_type_ref K) const {
335 TreeTy *NewT = Factory->remove(Root, K);
336 return ImmutableMapRef(NewT, Factory);
339 bool contains(key_type_ref K) const {
340 return Root ? Root->contains(K) : false;
343 ImmutableMap<KeyT, ValT> asImmutableMap() const {
344 return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
347 bool operator==(const ImmutableMapRef &RHS) const {
348 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
351 bool operator!=(const ImmutableMapRef &RHS) const {
352 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
355 bool isEmpty() const { return !Root; }
357 //===--------------------------------------------------===//
359 //===--------------------------------------------------===//
361 void verify() const {
366 //===--------------------------------------------------===//
368 //===--------------------------------------------------===//
370 class iterator : public ImutAVLValueIterator<ImmutableMapRef> {
371 friend class ImmutableMapRef;
373 iterator() = default;
374 explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
377 key_type_ref getKey() const { return (*this)->first; }
378 data_type_ref getData() const { return (*this)->second; }
381 iterator begin() const { return iterator(Root); }
382 iterator end() const { return iterator(); }
384 data_type *lookup(key_type_ref K) const {
386 TreeTy* T = Root->find(K);
387 if (T) return &T->getValue().second;
393 /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
394 /// which key is the highest in the ordering of keys in the map. This
395 /// method returns NULL if the map is empty.
396 value_type* getMaxElement() const {
397 return Root ? &(Root->getMaxElement()->getValue()) : 0;
400 //===--------------------------------------------------===//
402 //===--------------------------------------------------===//
404 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
406 static inline void Profile(FoldingSetNodeID &ID, const ImmutableMapRef &M) {
407 ID.AddPointer(M.Root);
410 inline void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
413 } // end namespace llvm
415 #endif // LLVM_ADT_IMMUTABLEMAP_H