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 LLVM_SUPPORT_STL_EXTRAS_H
11 #define LLVM_SUPPORT_STL_EXTRAS_H
15 //===----------------------------------------------------------------------===//
16 // Extra additions to <functional>
17 //===----------------------------------------------------------------------===//
19 // bind_obj - Often times you want to apply the member function of an object
20 // as a unary functor. This macro is shorthand that makes it happen less
24 // struct Summer { void accumulate(int x); }
25 // vector<int> Numbers;
27 // for_each(Numbers.begin(), Numbers.end(),
28 // bind_obj(&MyS, &Summer::accumulate));
30 // TODO: When I get lots of extra time, convert this from an evil macro
32 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
35 // bitwise_or - This is a simple functor that applys operator| on its two
36 // arguments to get a boolean result.
39 struct bitwise_or : public binary_function<Ty, Ty, bool> {
40 bool operator()(const Ty& left, const Ty& right) const {
46 // deleter - Very very very simple method that is used to invoke operator
47 // delete on something. It is used like this:
49 // for_each(V.begin(), B.end(), deleter<cfg::Interval>);
52 static inline void deleter(T *Ptr) {
58 //===----------------------------------------------------------------------===//
59 // Extra additions to <iterator>
60 //===----------------------------------------------------------------------===//
62 // mapped_iterator - This is a simple iterator adapter that causes a function to
63 // be dereferenced whenever operator* is invoked on the iterator.
65 // It turns out that this is disturbingly similar to boost::transform_iterator
68 template <class RootIt, class UnaryFunc>
69 class mapped_iterator {
73 typedef typename iterator_traits<RootIt>::iterator_category
75 typedef typename iterator_traits<RootIt>::difference_type
77 typedef typename UnaryFunc::result_type value_type;
78 typedef typename UnaryFunc::result_type *pointer;
79 typedef void reference; // Can't modify value returned by fn
81 typedef RootIt iterator_type;
82 typedef mapped_iterator<RootIt, UnaryFunc> _Self;
84 inline RootIt &getCurrent() const { return current; }
86 inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
87 : current(I), Fn(F) {}
88 inline mapped_iterator(const mapped_iterator &It)
89 : current(It.current), Fn(It.Fn) {}
91 inline value_type operator*() const { // All this work to do this
92 return Fn(*current); // little change
95 _Self& operator++() { ++current; return *this; }
96 _Self& operator--() { --current; return *this; }
97 _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
98 _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; }
99 _Self operator+ (difference_type n) const { return _Self(current + n); }
100 _Self& operator+= (difference_type n) { current += n; return *this; }
101 _Self operator- (difference_type n) const { return _Self(current - n); }
102 _Self& operator-= (difference_type n) { current -= n; return *this; }
103 reference operator[](difference_type n) const { return *(*this + n); }
105 inline bool operator==(const _Self &X) const { return current == X.current; }
106 inline bool operator< (const _Self &X) const { return current < X.current; }
108 inline difference_type operator-(const _Self &X) const {
109 return current - X.current;
113 template <class _Iterator, class Func>
114 inline mapped_iterator<_Iterator, Func>
115 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
116 const mapped_iterator<_Iterator, Func>& X) {
117 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
122 // This fails to work, because some iterators are not classes, for example
123 // vector iterators are commonly value_type **'s
124 template <class RootIt, class UnaryFunc>
125 class mapped_iterator : public RootIt {
128 typedef typename UnaryFunc::result_type value_type;
129 typedef typename UnaryFunc::result_type *pointer;
130 typedef void reference; // Can't modify value returned by fn
132 typedef mapped_iterator<RootIt, UnaryFunc> _Self;
133 typedef RootIt super;
134 inline explicit mapped_iterator(const RootIt &I) : super(I) {}
135 inline mapped_iterator(const super &It) : super(It) {}
137 inline value_type operator*() const { // All this work to do
138 return Fn(super::operator*()); // this little thing
143 // map_iterator - Provide a convenient way to create mapped_iterators, just like
144 // make_pair is useful for creating pairs...
146 template <class ItTy, class FuncTy>
147 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
148 return mapped_iterator<ItTy, FuncTy>(I, F);
152 //===----------------------------------------------------------------------===//
153 // Extra additions to <algorithm>
154 //===----------------------------------------------------------------------===//
156 // apply_until - Apply a functor to a sequence continually, unless the
157 // functor returns true. Return true if the functor returned true, return false
158 // if the functor never returned true.
160 template <class InputIt, class Function>
161 bool apply_until(InputIt First, InputIt Last, Function Func) {
162 for ( ; First != Last; ++First)
163 if (Func(*First)) return true;
168 // reduce - Reduce a sequence values into a single value, given an initial
169 // value and an operator.
171 template <class InputIt, class Function, class ValueType>
172 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
173 for ( ; First != Last; ++First)
174 Value = Func(*First, Value);
178 #if 1 // This is likely to be more efficient
180 // reduce_apply - Reduce the result of applying a function to each value in a
181 // sequence, given an initial value, an operator, a function, and a sequence.
183 template <class InputIt, class Function, class ValueType, class TransFunc>
184 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func,
185 ValueType Value, TransFunc XForm) {
186 for ( ; First != Last; ++First)
187 Value = Func(XForm(*First), Value);
191 #else // This is arguably more elegant
193 // reduce_apply - Reduce the result of applying a function to each value in a
194 // sequence, given an initial value, an operator, a function, and a sequence.
196 template <class InputIt, class Function, class ValueType, class TransFunc>
197 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func,
198 ValueType Value, TransFunc XForm) {
199 return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
205 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
206 // to each value in a sequence. All of the bools returned by the mapped
207 // function are bitwise or'd together, and the result is returned.
209 template <class InputIt, class Function>
210 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
211 return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
215 // map - This function maps the specified input sequence into the specified
216 // output iterator, applying a unary function in between.
218 template <class InIt, class OutIt, class Functor>
219 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
220 return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);