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