Changed profiling method for ImmutableMap to once again just use its
[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 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;
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(TreeTy* R) : Root(R) {}
80   
81   class Factory {
82     typename TreeTy::Factory F;
83     
84   public:
85     Factory() {}
86     
87     ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
88     
89     ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
90       return ImmutableMap(F.Add(Old.Root,
91                                 std::make_pair<key_type,data_type>(K,D)));
92     }
93     
94     ImmutableMap Remove(ImmutableMap Old, key_type_ref K) {
95       return ImmutableMap(F.Remove(Old.Root,K));
96     }        
97     
98   private:
99     Factory(const Factory& RHS) {};
100     void operator=(const Factory& RHS) {};    
101   };
102   
103   friend class Factory;  
104   
105   bool contains(key_type_ref K) const {
106     return Root ? Root->contains(K) : false;
107   }
108
109   
110   bool operator==(ImmutableMap RHS) const {
111     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
112   }
113   
114   bool operator!=(ImmutableMap RHS) const {
115     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
116   }
117   
118   TreeTy* getRoot() const { return Root; }
119   
120   bool isEmpty() const { return !Root; }
121
122   //===--------------------------------------------------===//    
123   // Foreach - A limited form of map iteration.
124   //===--------------------------------------------------===//
125
126 private:
127   template <typename Callback>
128   struct CBWrapper {
129     Callback C;
130     void operator()(value_type_ref V) { C(V.first,V.second); }    
131   };  
132   
133   template <typename Callback>
134   struct CBWrapperRef {
135     Callback &C;
136     CBWrapperRef(Callback& c) : C(c) {}
137     
138     void operator()(value_type_ref V) { C(V.first,V.second); }    
139   };
140   
141 public:  
142   template <typename Callback>
143   void foreach(Callback& C) { 
144     if (Root) { 
145       CBWrapperRef<Callback> CB(C);
146       Root->foreach(CB);
147     }
148   }
149   
150   template <typename Callback>
151   void foreach() { 
152     if (Root) {
153       CBWrapper<Callback> CB;
154       Root->foreach(CB);
155     }
156   }
157   
158   //===--------------------------------------------------===//    
159   // For testing.
160   //===--------------------------------------------------===//  
161   
162   void verify() const { if (Root) Root->verify(); }
163   
164   //===--------------------------------------------------===//    
165   // Iterators.
166   //===--------------------------------------------------===//  
167   
168   class iterator {
169     typename TreeTy::iterator itr;
170     
171     iterator() {}
172     iterator(TreeTy* t) : itr(t) {}
173     friend class ImmutableMap;
174
175   public:
176     inline value_type_ref operator*() const { return itr->getValue(); }
177     inline key_type_ref getKey() const { return itr->getValue().first; }
178     inline data_type_ref getData() const { return itr->getValue().second; }
179     
180     inline iterator& operator++() { ++itr; return *this; }
181     inline iterator  operator++(int) { iterator tmp(*this); ++itr; return tmp; }
182     inline iterator& operator--() { --itr; return *this; }
183     inline iterator  operator--(int) { iterator tmp(*this); --itr; return tmp; }
184     inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
185     inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }        
186   };
187   
188   iterator begin() const { return iterator(Root); }
189   iterator end() const { return iterator(); }  
190   
191   TreeTy* SlimFind(key_type_ref K) const {
192     if (Root) {
193       TreeTy* T = Root->find(K);
194       if (T) return T;
195     }
196     
197     return NULL;
198   }
199   
200   // FIXME: Add 'find' that returns an iterator instead of a TreeTy*.
201   
202   //===--------------------------------------------------===//    
203   // Utility methods.
204   //===--------------------------------------------------===//  
205   
206   inline unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
207
208   static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
209     ID.AddPointer(M.Root);
210   }
211   
212   inline void Profile(FoldingSetNodeID& ID) const {
213     return Profile(ID,*this);
214   }  
215 };
216   
217 } // end namespace llvm
218
219 #endif