1 //===-- STLExtras.h - Useful functions when working with the STL -*- C++ -*--=//
3 // This file contains some templates that are useful if you are working with the
6 // No library is required when using these functinons.
8 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_STLEXTRAS_H
11 #define SUPPORT_STLEXTRAS_H
14 #include "Support/iterator"
16 //===----------------------------------------------------------------------===//
17 // Extra additions to <functional>
18 //===----------------------------------------------------------------------===//
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
25 // struct Summer { void accumulate(int x); }
26 // vector<int> Numbers;
28 // for_each(Numbers.begin(), Numbers.end(),
29 // bind_obj(&MyS, &Summer::accumulate));
31 // TODO: When I get lots of extra time, convert this from an evil macro
33 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
36 // bitwise_or - This is a simple functor that applys operator| on its two
37 // arguments to get a boolean result.
40 struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
41 bool operator()(const Ty& left, const Ty& right) const {
47 // deleter - Very very very simple method that is used to invoke operator
48 // delete on something. It is used like this:
50 // for_each(V.begin(), B.end(), deleter<Interval>);
53 static inline void deleter(T *Ptr) {
59 //===----------------------------------------------------------------------===//
60 // Extra additions to <iterator>
61 //===----------------------------------------------------------------------===//
63 // mapped_iterator - This is a simple iterator adapter that causes a function to
64 // be dereferenced whenever operator* is invoked on the iterator.
66 // It turns out that this is disturbingly similar to boost::transform_iterator
69 template <class RootIt, class UnaryFunc>
70 class mapped_iterator {
74 typedef typename std::iterator_traits<RootIt>::iterator_category
76 typedef typename std::iterator_traits<RootIt>::difference_type
78 typedef typename UnaryFunc::result_type value_type;
81 //typedef typename UnaryFunc::result_type *pointer;
82 typedef void reference; // Can't modify value returned by fn
84 typedef RootIt iterator_type;
85 typedef mapped_iterator<RootIt, UnaryFunc> _Self;
87 inline RootIt &getCurrent() const { return current; }
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) {}
94 inline value_type operator*() const { // All this work to do this
95 return Fn(*current); // little change
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); }
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; }
112 inline difference_type operator-(const _Self &X) const {
113 return current - X.current;
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);
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 {
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
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) {}
141 inline value_type operator*() const { // All this work to do
142 return Fn(super::operator*()); // this little thing
147 // map_iterator - Provide a convenient way to create mapped_iterators, just like
148 // make_pair is useful for creating pairs...
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);
156 //===----------------------------------------------------------------------===//
157 // Extra additions to <algorithm>
158 //===----------------------------------------------------------------------===//
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.
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;
172 // reduce - Reduce a sequence values into a single value, given an initial
173 // value and an operator.
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);
182 #if 1 // This is likely to be more efficient
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.
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);
195 #else // This is arguably more elegant
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.
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),
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.
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);
219 // map - This function maps the specified input sequence into the specified
220 // output iterator, applying a unary function in between.
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);