012d002427311dea868f27fbb0afa9c4a9dfbd73
[oota-llvm.git] / include / Support / STLExtras.h
1 //===- STLExtras.h - Useful functions when working with the STL -*- C++ -*-===//
2 //
3 // This file contains some templates that are useful if you are working with the
4 // STL at all.
5 //
6 // No library is required when using these functinons.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef SUPPORT_STLEXTRAS_H
11 #define SUPPORT_STLEXTRAS_H
12
13 #include <functional>
14 #include "Support/iterator"
15
16 //===----------------------------------------------------------------------===//
17 //     Extra additions to <functional>
18 //===----------------------------------------------------------------------===//
19
20 // bind_obj - Often times you want to apply the member function of an object
21 // as a unary functor.  This macro is shorthand that makes it happen less
22 // verbosely.
23 //
24 // Example:
25 //  struct Summer { void accumulate(int x); }
26 //  vector<int> Numbers;
27 //  Summer MyS;
28 //  for_each(Numbers.begin(), Numbers.end(),
29 //           bind_obj(&MyS, &Summer::accumulate));
30 //
31 // TODO: When I get lots of extra time, convert this from an evil macro
32 //
33 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
34
35
36 // bitwise_or - This is a simple functor that applys operator| on its two 
37 // arguments to get a boolean result.
38 //
39 template<class Ty>
40 struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
41   bool operator()(const Ty& left, const Ty& right) const {
42     return left | right;
43   }
44 };
45
46
47 // deleter - Very very very simple method that is used to invoke operator
48 // delete on something.  It is used like this: 
49 //
50 //   for_each(V.begin(), B.end(), deleter<Interval>);
51 //
52 template <class T> 
53 static inline void deleter(T *Ptr) { 
54   delete Ptr; 
55 }
56
57
58
59 //===----------------------------------------------------------------------===//
60 //     Extra additions to <iterator>
61 //===----------------------------------------------------------------------===//
62
63 // mapped_iterator - This is a simple iterator adapter that causes a function to
64 // be dereferenced whenever operator* is invoked on the iterator.
65 //
66 // It turns out that this is disturbingly similar to boost::transform_iterator
67 //
68 #if 1
69 template <class RootIt, class UnaryFunc>
70 class mapped_iterator {
71   RootIt current;
72   UnaryFunc Fn;
73 public:
74   typedef typename std::iterator_traits<RootIt>::iterator_category
75           iterator_category;
76   typedef typename std::iterator_traits<RootIt>::difference_type
77           difference_type;
78   typedef typename UnaryFunc::result_type value_type;
79
80   typedef void pointer;
81   //typedef typename UnaryFunc::result_type *pointer;
82   typedef void reference;        // Can't modify value returned by fn
83
84   typedef RootIt iterator_type;
85   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
86
87   inline RootIt &getCurrent() const { return current; }
88
89   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
90     : current(I), Fn(F) {}
91   inline mapped_iterator(const mapped_iterator &It)
92     : current(It.current), Fn(It.Fn) {}
93
94   inline value_type operator*() const {   // All this work to do this 
95     return Fn(*current);         // little change
96   }
97
98   _Self& operator++() { ++current; return *this; }
99   _Self& operator--() { --current; return *this; }
100   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
101   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
102   _Self  operator+    (difference_type n) const { return _Self(current + n); }
103   _Self& operator+=   (difference_type n) { current += n; return *this; }
104   _Self  operator-    (difference_type n) const { return _Self(current - n); }
105   _Self& operator-=   (difference_type n) { current -= n; return *this; }
106   reference operator[](difference_type n) const { return *(*this + n); }  
107
108   inline bool operator!=(const _Self &X) const { return !operator==(X); }
109   inline bool operator==(const _Self &X) const { return current == X.current; }
110   inline bool operator< (const _Self &X) const { return current <  X.current; }
111
112   inline difference_type operator-(const _Self &X) const {
113     return current - X.current;
114   }
115 };
116
117 template <class _Iterator, class Func>
118 inline mapped_iterator<_Iterator, Func> 
119 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
120           const mapped_iterator<_Iterator, Func>& X) {
121   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
122 }
123
124 #else
125
126 // This fails to work, because some iterators are not classes, for example
127 // vector iterators are commonly value_type **'s
128 template <class RootIt, class UnaryFunc>
129 class mapped_iterator : public RootIt {
130   UnaryFunc Fn;
131 public:
132   typedef typename UnaryFunc::result_type value_type;
133   typedef typename UnaryFunc::result_type *pointer;
134   typedef void reference;        // Can't modify value returned by fn
135
136   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
137   typedef RootIt super;
138   inline explicit mapped_iterator(const RootIt &I) : super(I) {}
139   inline mapped_iterator(const super &It) : super(It) {}
140
141   inline value_type operator*() const {     // All this work to do 
142     return Fn(super::operator*());   // this little thing
143   }
144 };
145 #endif
146
147 // map_iterator - Provide a convenient way to create mapped_iterators, just like
148 // make_pair is useful for creating pairs...
149 //
150 template <class ItTy, class FuncTy>
151 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
152   return mapped_iterator<ItTy, FuncTy>(I, F);
153 }
154
155
156 //===----------------------------------------------------------------------===//
157 //     Extra additions to <algorithm>
158 //===----------------------------------------------------------------------===//
159
160 // apply_until - Apply a functor to a sequence continually, unless the
161 // functor returns true.  Return true if the functor returned true, return false
162 // if the functor never returned true.
163 //
164 template <class InputIt, class Function>
165 bool apply_until(InputIt First, InputIt Last, Function Func) {
166   for ( ; First != Last; ++First)
167     if (Func(*First)) return true;
168   return false;
169 }
170
171
172 // reduce - Reduce a sequence values into a single value, given an initial
173 // value and an operator.
174 //
175 template <class InputIt, class Function, class ValueType>
176 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
177   for ( ; First != Last; ++First)
178     Value = Func(*First, Value);
179   return Value;
180 }
181
182 #if 1   // This is likely to be more efficient
183
184 // reduce_apply - Reduce the result of applying a function to each value in a
185 // sequence, given an initial value, an operator, a function, and a sequence.
186 //
187 template <class InputIt, class Function, class ValueType, class TransFunc>
188 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
189                               ValueType Value, TransFunc XForm) {
190   for ( ; First != Last; ++First)
191     Value = Func(XForm(*First), Value);
192   return Value;
193 }
194
195 #else  // This is arguably more elegant
196
197 // reduce_apply - Reduce the result of applying a function to each value in a
198 // sequence, given an initial value, an operator, a function, and a sequence.
199 //
200 template <class InputIt, class Function, class ValueType, class TransFunc>
201 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
202                                ValueType Value, TransFunc XForm) {
203   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
204                 Func, Value);
205 }
206 #endif
207
208
209 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
210 // to each value in a sequence.  All of the bools returned by the mapped
211 // function are bitwise or'd together, and the result is returned.
212 //
213 template <class InputIt, class Function>
214 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
215   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
216 }
217
218
219 // map - This function maps the specified input sequence into the specified
220 // output iterator, applying a unary function in between.
221 //
222 template <class InIt, class OutIt, class Functor>
223 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
224   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
225 }
226 #endif