Added iterator and profiling (i.e. FoldingSetNodeID) support to ImmutableMap.
[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 bool isEqual(key_type_ref L, key_type_ref R) {
38     return ImutContainerInfo<T>::isEqual(L,R);
39   }
40   
41   static inline bool isLess(key_type_ref L, key_type_ref R) {
42     return ImutContainerInfo<T>::isLess(L,R);
43   }
44   
45   static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
46     ImutContainerInfo<T>::Profile(ID, V.first);
47     ImutContainerInfo<S>::Profile(ID, V.second);
48   }
49 };  
50
51   
52 template <typename KeyT, typename ValT, 
53           typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
54 class ImmutableMap {
55   typedef typename ValInfo::value_type      value_type;
56   typedef typename ValInfo::value_type_ref  value_type_ref;
57   typedef typename ValInfo::key_type        key_type;
58   typedef typename ValInfo::key_type_ref    key_type_ref;
59   typedef typename ValInfo::data_type       data_type;
60   typedef typename ValInfo::data_type_ref   data_type_ref;
61   
62 private:  
63   typedef ImutAVLTree<ValInfo> TreeTy;
64   TreeTy* Root;
65   
66   ImmutableMap(TreeTy* R) : Root(R) {}
67   
68 public:
69   
70   class Factory {
71     typename TreeTy::Factory F;
72     
73   public:
74     Factory() {}
75     
76     ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
77     
78     ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
79       return ImmutableMap(F.Add(Old.Root,std::make_pair<key_type,data_type>(K,D)));
80     }
81     
82     ImmutableMap Remove(ImmutableMap Old, key_type_ref K) {
83       return ImmutableMap(F.Remove(Old.Root,K));
84     }        
85     
86   private:
87     Factory(const Factory& RHS) {};
88     void operator=(const Factory& RHS) {};    
89   };
90   
91   friend class Factory;  
92   
93   bool contains(key_type_ref K) const {
94     return Root ? Root->contains(K) : false;
95   }
96   
97   data_type* find(key_type_ref K) const {
98     if (Root) {
99       TreeTy* T = Root->find(K);
100       if (T) return &T->getValue().second;
101     }
102     
103     return NULL;
104   }
105   
106   bool operator==(ImmutableMap RHS) const {
107     return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
108   }
109   
110   bool operator!=(ImmutableMap RHS) const {
111     return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
112   }
113   
114   bool isEmpty() const { return !Root; }
115
116   //===--------------------------------------------------===//    
117   // Foreach - A limited form of map iteration.
118   //===--------------------------------------------------===//
119
120 private:
121   template <typename Callback>
122   struct CBWrapper {
123     Callback C;
124     void operator()(value_type_ref V) { C(V.first,V.second); }    
125   };  
126   
127   template <typename Callback>
128   struct CBWrapperRef {
129     Callback &C;
130     CBWrapperRef(Callback& c) : C(c) {}
131     
132     void operator()(value_type_ref V) { C(V.first,V.second); }    
133   };
134   
135 public:  
136   template <typename Callback>
137   void foreach(Callback& C) { 
138     if (Root) { 
139       CBWrapperRef<Callback> CB(C);
140       Root->foreach(CB);
141     }
142   }
143   
144   template <typename Callback>
145   void foreach() { 
146     if (Root) {
147       CBWrapper<Callback> CB;
148       Root->foreach(CB);
149     }
150   }
151   
152   //===--------------------------------------------------===//    
153   // For testing.
154   //===--------------------------------------------------===//  
155   
156   void verify() const { if (Root) Root->verify(); }
157   
158   //===--------------------------------------------------===//    
159   // Iterators.
160   //===--------------------------------------------------===//  
161   
162   class iterator {
163     typename TreeTy::iterator itr;
164     
165     iterator() {}
166     iterator(TreeTy* t) : itr(t) {}
167     friend class ImmutableSet<ValT,ValInfo>;
168
169   public:
170     inline value_type_ref operator*() const { return itr->getValue(); }
171     inline key_type_ref getKey() const { return itr->getValue().first; }
172     inline data_type_ref getData() const { return itr->getValue().second; }
173     
174     inline iterator& operator++() { ++itr; return *this; }
175     inline iterator  operator++(int) { iterator tmp(*this); ++itr; return tmp; }
176     inline iterator& operator--() { --itr; return *this; }
177     inline iterator  operator--(int) { iterator tmp(*this); --itr; return tmp; }
178     inline bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
179     inline bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }        
180   };
181   
182   iterator begin() const { return iterator(Root); }
183   iterator end() const { return iterator(); }  
184   
185   //===--------------------------------------------------===//    
186   // Utility methods.
187   //===--------------------------------------------------===//  
188   
189   inline unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
190
191   static inline void Profile(const ImmutableMap& M, FoldingSetNodeID& ID) { 
192     ID.AddPointer(M.Root);
193   }
194   
195   inline void Profile(FoldingSetNodeID& ID) const {
196     return Profile(*this,ID);
197   }  
198 };
199   
200 } // end namespace llvm
201
202 #endif