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