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