Store flags in bitfields instead of masking them into the pointer for the left child...
[oota-llvm.git] / include / llvm / ADT / ImmutableMap.h
1 //===--- ImmutableMap.h - Immutable (functional) map interface --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ImmutableMap class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_IMMAP_H
15 #define LLVM_ADT_IMMAP_H
16
17 #include "llvm/ADT/ImmutableSet.h"
18
19 namespace llvm {
20
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;
32
33   static inline key_type_ref KeyOfValue(value_type_ref V) {
34     return V.first;
35   }
36
37   static inline data_type_ref DataOfValue(value_type_ref V) {
38     return V.second;
39   }
40
41   static inline bool isEqual(key_type_ref L, key_type_ref R) {
42     return ImutContainerInfo<T>::isEqual(L,R);
43   }
44   static inline bool isLess(key_type_ref L, key_type_ref R) {
45     return ImutContainerInfo<T>::isLess(L,R);
46   }
47
48   static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
49     return ImutContainerInfo<S>::isEqual(L,R);
50   }
51
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);
55   }
56 };
57
58
59 template <typename KeyT, typename ValT,
60           typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
61 class ImmutableMap {
62 public:
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;
70
71 private:
72   TreeTy* Root;
73
74 public:
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
81   class Factory {
82     typename TreeTy::Factory F;
83     const bool Canonicalize;
84
85   public:
86     Factory(bool canonicalize = true)
87       : Canonicalize(canonicalize) {}
88     
89     Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
90       : F(Alloc), Canonicalize(canonicalize) {}
91
92     ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
93
94     ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
95       TreeTy *T = F.Add(Old.Root, std::make_pair<key_type,data_type>(K,D));
96       return ImmutableMap(Canonicalize ? F.GetCanonicalTree(T): T);
97     }
98
99     ImmutableMap Remove(ImmutableMap Old, key_type_ref K) {
100       TreeTy *T = F.Remove(Old.Root,K);
101       return ImmutableMap(Canonicalize ? F.GetCanonicalTree(T): T);
102     }
103
104   private:
105     Factory(const Factory& RHS); // DO NOT IMPLEMENT
106     void operator=(const Factory& RHS); // DO NOT IMPLEMENT
107   };
108
109   friend class Factory;
110
111   bool contains(key_type_ref K) const {
112     return Root ? Root->contains(K) : false;
113   }
114
115
116   bool operator==(ImmutableMap RHS) const {
117     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
118   }
119
120   bool operator!=(ImmutableMap RHS) const {
121     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
122   }
123
124   TreeTy* getRoot() const { return Root; }
125
126   bool isEmpty() const { return !Root; }
127
128   //===--------------------------------------------------===//
129   // Foreach - A limited form of map iteration.
130   //===--------------------------------------------------===//
131
132 private:
133   template <typename Callback>
134   struct CBWrapper {
135     Callback C;
136     void operator()(value_type_ref V) { C(V.first,V.second); }
137   };
138
139   template <typename Callback>
140   struct CBWrapperRef {
141     Callback &C;
142     CBWrapperRef(Callback& c) : C(c) {}
143
144     void operator()(value_type_ref V) { C(V.first,V.second); }
145   };
146
147 public:
148   template <typename Callback>
149   void foreach(Callback& C) {
150     if (Root) {
151       CBWrapperRef<Callback> CB(C);
152       Root->foreach(CB);
153     }
154   }
155
156   template <typename Callback>
157   void foreach() {
158     if (Root) {
159       CBWrapper<Callback> CB;
160       Root->foreach(CB);
161     }
162   }
163
164   //===--------------------------------------------------===//
165   // For testing.
166   //===--------------------------------------------------===//
167
168   void verify() const { if (Root) Root->verify(); }
169
170   //===--------------------------------------------------===//
171   // Iterators.
172   //===--------------------------------------------------===//
173
174   class iterator {
175     typename TreeTy::iterator itr;
176
177     iterator() {}
178     iterator(TreeTy* t) : itr(t) {}
179     friend class ImmutableMap;
180
181   public:
182     value_type_ref operator*() const { return itr->getValue(); }
183     value_type*    operator->() const { return &itr->getValue(); }
184
185     key_type_ref getKey() const { return itr->getValue().first; }
186     data_type_ref getData() const { return itr->getValue().second; }
187
188
189     iterator& operator++() { ++itr; return *this; }
190     iterator  operator++(int) { iterator tmp(*this); ++itr; return tmp; }
191     iterator& operator--() { --itr; return *this; }
192     iterator  operator--(int) { iterator tmp(*this); --itr; return tmp; }
193     bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
194     bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
195   };
196
197   iterator begin() const { return iterator(Root); }
198   iterator end() const { return iterator(); }
199
200   data_type* lookup(key_type_ref K) const {
201     if (Root) {
202       TreeTy* T = Root->find(K);
203       if (T) return &T->getValue().second;
204     }
205
206     return 0;
207   }
208   
209   /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
210   ///  which key is the highest in the ordering of keys in the map.  This
211   ///  method returns NULL if the map is empty.
212   value_type* getMaxElement() const {
213     return Root ? &(Root->getMaxElement()->getValue()) : 0;
214   }
215
216   //===--------------------------------------------------===//
217   // Utility methods.
218   //===--------------------------------------------------===//
219
220   unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
221
222   static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
223     ID.AddPointer(M.Root);
224   }
225
226   inline void Profile(FoldingSetNodeID& ID) const {
227     return Profile(ID,*this);
228   }
229 };
230
231 } // end namespace llvm
232
233 #endif