Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / include / Support / STLExtras.h
1 //===- STLExtras.h - Useful functions when working with the STL -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains some templates that are useful if you are working with the
11 // STL at all.
12 //
13 // No library is required when using these functinons.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef SUPPORT_STLEXTRAS_H
18 #define SUPPORT_STLEXTRAS_H
19
20 #include <functional>
21 #include <utility> // for std::pair
22 #include "Support/iterator"
23
24 namespace llvm {
25
26 //===----------------------------------------------------------------------===//
27 //     Extra additions to <functional>
28 //===----------------------------------------------------------------------===//
29
30 // bind_obj - Often times you want to apply the member function of an object
31 // as a unary functor.  This macro is shorthand that makes it happen less
32 // verbosely.
33 //
34 // Example:
35 //  struct Summer { void accumulate(int x); }
36 //  vector<int> Numbers;
37 //  Summer MyS;
38 //  for_each(Numbers.begin(), Numbers.end(),
39 //           bind_obj(&MyS, &Summer::accumulate));
40 //
41 // TODO: When I get lots of extra time, convert this from an evil macro
42 //
43 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
44
45
46 // bitwise_or - This is a simple functor that applys operator| on its two 
47 // arguments to get a boolean result.
48 //
49 template<class Ty>
50 struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
51   bool operator()(const Ty& left, const Ty& right) const {
52     return left | right;
53   }
54 };
55
56 template<class Ty>
57 struct less_ptr : public std::binary_function<Ty, Ty, bool> {
58   bool operator()(const Ty* left, const Ty* right) const {
59     return *left < *right;
60   }
61 };
62
63 template<class Ty>
64 struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
65   bool operator()(const Ty* left, const Ty* right) const {
66     return *right < *left;
67   }
68 };
69
70 // deleter - Very very very simple method that is used to invoke operator
71 // delete on something.  It is used like this: 
72 //
73 //   for_each(V.begin(), B.end(), deleter<Interval>);
74 //
75 template <class T> 
76 static inline void deleter(T *Ptr) { 
77   delete Ptr; 
78 }
79
80
81
82 //===----------------------------------------------------------------------===//
83 //     Extra additions to <iterator>
84 //===----------------------------------------------------------------------===//
85
86 // mapped_iterator - This is a simple iterator adapter that causes a function to
87 // be dereferenced whenever operator* is invoked on the iterator.
88 //
89 template <class RootIt, class UnaryFunc>
90 class mapped_iterator {
91   RootIt current;
92   UnaryFunc Fn;
93 public:
94   typedef typename std::iterator_traits<RootIt>::iterator_category
95           iterator_category;
96   typedef typename std::iterator_traits<RootIt>::difference_type
97           difference_type;
98   typedef typename UnaryFunc::result_type value_type;
99
100   typedef void pointer;
101   //typedef typename UnaryFunc::result_type *pointer;
102   typedef void reference;        // Can't modify value returned by fn
103
104   typedef RootIt iterator_type;
105   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
106
107   inline RootIt &getCurrent() const { return current; }
108
109   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
110     : current(I), Fn(F) {}
111   inline mapped_iterator(const mapped_iterator &It)
112     : current(It.current), Fn(It.Fn) {}
113
114   inline value_type operator*() const {   // All this work to do this 
115     return Fn(*current);         // little change
116   }
117
118   _Self& operator++() { ++current; return *this; }
119   _Self& operator--() { --current; return *this; }
120   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
121   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
122   _Self  operator+    (difference_type n) const { return _Self(current + n); }
123   _Self& operator+=   (difference_type n) { current += n; return *this; }
124   _Self  operator-    (difference_type n) const { return _Self(current - n); }
125   _Self& operator-=   (difference_type n) { current -= n; return *this; }
126   reference operator[](difference_type n) const { return *(*this + n); }  
127
128   inline bool operator!=(const _Self &X) const { return !operator==(X); }
129   inline bool operator==(const _Self &X) const { return current == X.current; }
130   inline bool operator< (const _Self &X) const { return current <  X.current; }
131
132   inline difference_type operator-(const _Self &X) const {
133     return current - X.current;
134   }
135 };
136
137 template <class _Iterator, class Func>
138 inline mapped_iterator<_Iterator, Func> 
139 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
140           const mapped_iterator<_Iterator, Func>& X) {
141   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
142 }
143
144
145 // map_iterator - Provide a convenient way to create mapped_iterators, just like
146 // make_pair is useful for creating pairs...
147 //
148 template <class ItTy, class FuncTy>
149 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
150   return mapped_iterator<ItTy, FuncTy>(I, F);
151 }
152
153
154 // next/prior - These functions unlike std::advance do not modify the
155 // passed iterator but return a copy.
156 //
157 // next(myIt) returns copy of myIt incremented once
158 // next(myIt, n) returns copy of myIt incremented n times
159 // prior(myIt) returns copy of myIt decremented once
160 // prior(myIt, n) returns copy of myIt decremented n times
161
162 template <typename ItTy, typename Dist>
163 inline ItTy next(ItTy it, Dist n)
164 {
165   std::advance(it, n);
166   return it;
167 }
168
169 template <typename ItTy>
170 inline ItTy next(ItTy it)
171 {
172   std::advance(it, 1);
173   return it;
174 }
175
176 template <typename ItTy, typename Dist>
177 inline ItTy prior(ItTy it, Dist n)
178 {
179   std::advance(it, -n);
180   return it;
181 }
182
183 template <typename ItTy>
184 inline ItTy prior(ItTy it)
185 {
186   std::advance(it, -1);
187   return it;
188 }
189
190
191 //===----------------------------------------------------------------------===//
192 //     Extra additions to <algorithm>
193 //===----------------------------------------------------------------------===//
194
195 // apply_until - Apply a functor to a sequence continually, unless the
196 // functor returns true.  Return true if the functor returned true, return false
197 // if the functor never returned true.
198 //
199 template <class InputIt, class Function>
200 bool apply_until(InputIt First, InputIt Last, Function Func) {
201   for ( ; First != Last; ++First)
202     if (Func(*First)) return true;
203   return false;
204 }
205
206
207 // reduce - Reduce a sequence values into a single value, given an initial
208 // value and an operator.
209 //
210 template <class InputIt, class Function, class ValueType>
211 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
212   for ( ; First != Last; ++First)
213     Value = Func(*First, Value);
214   return Value;
215 }
216
217 #if 1   // This is likely to be more efficient
218
219 // reduce_apply - Reduce the result of applying a function to each value in a
220 // sequence, given an initial value, an operator, a function, and a sequence.
221 //
222 template <class InputIt, class Function, class ValueType, class TransFunc>
223 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
224                               ValueType Value, TransFunc XForm) {
225   for ( ; First != Last; ++First)
226     Value = Func(XForm(*First), Value);
227   return Value;
228 }
229
230 #else  // This is arguably more elegant
231
232 // reduce_apply - Reduce the result of applying a function to each value in a
233 // sequence, given an initial value, an operator, a function, and a sequence.
234 //
235 template <class InputIt, class Function, class ValueType, class TransFunc>
236 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
237                                ValueType Value, TransFunc XForm) {
238   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
239                 Func, Value);
240 }
241 #endif
242
243
244 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
245 // to each value in a sequence.  All of the bools returned by the mapped
246 // function are bitwise or'd together, and the result is returned.
247 //
248 template <class InputIt, class Function>
249 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
250   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
251 }
252
253
254 // map - This function maps the specified input sequence into the specified
255 // output iterator, applying a unary function in between.
256 //
257 template <class InIt, class OutIt, class Functor>
258 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
259   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
260 }
261
262
263 //===----------------------------------------------------------------------===//
264 //     Extra additions to <utility>
265 //===----------------------------------------------------------------------===//
266
267 // tie - this function ties two objects and returns a temporary object
268 // that is assignable from a std::pair. This can be used to make code
269 // more readable when using values returned from functions bundled in
270 // a std::pair. Since an example is worth 1000 words:
271 //
272 // typedef std::map<int, int> Int2IntMap;
273 // 
274 // Int2IntMap myMap;
275 // Int2IntMap::iterator where;
276 // bool inserted;
277 // tie(where, inserted) = myMap.insert(std::make_pair(123,456));
278 //
279 // if (inserted)
280 //   // do stuff
281 // else
282 //   // do other stuff
283
284 namespace
285 {
286   template <typename T1, typename T2>
287   struct tier {
288     typedef T1 &first_type;
289     typedef T2 &second_type;
290
291     first_type first;
292     second_type second;
293
294     tier(first_type f, second_type s) : first(f), second(s) { }
295     tier& operator=(const std::pair<T1, T2>& p) {
296       first = p.first;
297       second = p.second;
298       return *this;
299     }
300   };
301 }
302
303 template <typename T1, typename T2>
304 inline tier<T1, T2> tie(T1& f, T2& s) {
305   return tier<T1, T2>(f, s);
306 }
307
308 } // End llvm namespace
309
310 #endif