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