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 and
22 /// 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(TreeTy* R) : Root(R) {}
80 explicit ImmutableMap(const TreeTy* R) : Root(const_cast<TreeTy*>(R)) {}
83 typename TreeTy::Factory F;
88 Factory(BumpPtrAllocator& Alloc)
91 ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
93 ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
94 return ImmutableMap(F.Add(Old.Root,
95 std::make_pair<key_type,data_type>(K,D)));
98 ImmutableMap Remove(ImmutableMap Old, key_type_ref K) {
99 return ImmutableMap(F.Remove(Old.Root,K));
103 Factory(const Factory& RHS) {};
104 void operator=(const Factory& RHS) {};
107 friend class Factory;
109 bool contains(key_type_ref K) const {
110 return Root ? Root->contains(K) : false;
114 bool operator==(ImmutableMap RHS) const {
115 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
118 bool operator!=(ImmutableMap RHS) const {
119 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
122 TreeTy* getRoot() const { return Root; }
124 bool isEmpty() const { return !Root; }
126 //===--------------------------------------------------===//
127 // Foreach - A limited form of map iteration.
128 //===--------------------------------------------------===//
131 template <typename Callback>
134 void operator()(value_type_ref V) { C(V.first,V.second); }
137 template <typename Callback>
138 struct CBWrapperRef {
140 CBWrapperRef(Callback& c) : C(c) {}
142 void operator()(value_type_ref V) { C(V.first,V.second); }
146 template <typename Callback>
147 void foreach(Callback& C) {
149 CBWrapperRef<Callback> CB(C);
154 template <typename Callback>
157 CBWrapper<Callback> CB;
162 //===--------------------------------------------------===//
164 //===--------------------------------------------------===//
166 void verify() const { if (Root) Root->verify(); }
168 //===--------------------------------------------------===//
170 //===--------------------------------------------------===//
173 typename TreeTy::iterator itr;
176 iterator(TreeTy* t) : itr(t) {}
177 friend class ImmutableMap;
180 value_type_ref operator*() const { return itr->getValue(); }
181 value_type* operator->() const { return &itr->getValue(); }
183 key_type_ref getKey() const { return itr->getValue().first; }
184 data_type_ref getData() const { return itr->getValue().second; }
187 iterator& operator++() { ++itr; return *this; }
188 iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
189 iterator& operator--() { --itr; return *this; }
190 iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
191 bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
192 bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
195 iterator begin() const { return iterator(Root); }
196 iterator end() const { return iterator(); }
198 data_type* lookup(key_type_ref K) const {
200 TreeTy* T = Root->find(K);
201 if (T) return &T->getValue().second;
207 //===--------------------------------------------------===//
209 //===--------------------------------------------------===//
211 inline unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
213 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
214 ID.AddPointer(M.Root);
217 inline void Profile(FoldingSetNodeID& ID) const {
218 return Profile(ID,*this);
222 } // end namespace llvm