Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- numeric ---------------------------------===// |
| 3 | // |
| 4 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_NUMERIC |
| 12 | #define _LIBCPP_NUMERIC |
| 13 | |
| 14 | /* |
| 15 | numeric synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class InputIterator, class T> |
| 21 | T |
| 22 | accumulate(InputIterator first, InputIterator last, T init); |
| 23 | |
| 24 | template <class InputIterator, class T, class BinaryOperation> |
| 25 | T |
| 26 | accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); |
| 27 | |
| 28 | template <class InputIterator1, class InputIterator2, class T> |
| 29 | T |
| 30 | inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init); |
| 31 | |
| 32 | template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> |
| 33 | T |
| 34 | inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, |
| 35 | T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); |
| 36 | |
| 37 | template <class InputIterator, class OutputIterator> |
| 38 | OutputIterator |
| 39 | partial_sum(InputIterator first, InputIterator last, OutputIterator result); |
| 40 | |
| 41 | template <class InputIterator, class OutputIterator, class BinaryOperation> |
| 42 | OutputIterator |
| 43 | partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); |
| 44 | |
| 45 | template <class InputIterator, class OutputIterator> |
| 46 | OutputIterator |
| 47 | adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); |
| 48 | |
| 49 | template <class InputIterator, class OutputIterator, class BinaryOperation> |
| 50 | OutputIterator |
| 51 | adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); |
| 52 | |
| 53 | } // std |
| 54 | |
| 55 | */ |
| 56 | |
| 57 | #include <__config> |
| 58 | #include <iterator> |
| 59 | |
| 60 | #pragma GCC system_header |
| 61 | |
| 62 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 63 | |
| 64 | template <class _InputIterator, class _Tp> |
| 65 | inline _LIBCPP_INLINE_VISIBILITY |
| 66 | _Tp |
| 67 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) |
| 68 | { |
| 69 | for (; __first != __last; ++__first) |
| 70 | __init = __init + *__first; |
| 71 | return __init; |
| 72 | } |
| 73 | |
| 74 | template <class _InputIterator, class _Tp, class _BinaryOperation> |
| 75 | inline _LIBCPP_INLINE_VISIBILITY |
| 76 | _Tp |
| 77 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) |
| 78 | { |
| 79 | for (; __first != __last; ++__first) |
| 80 | __init = __binary_op(__init, *__first); |
| 81 | return __init; |
| 82 | } |
| 83 | |
| 84 | template <class _InputIterator1, class _InputIterator2, class _Tp> |
| 85 | inline _LIBCPP_INLINE_VISIBILITY |
| 86 | _Tp |
| 87 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) |
| 88 | { |
| 89 | for (; __first1 != __last1; ++__first1, ++__first2) |
| 90 | __init = __init + *__first1 * *__first2; |
| 91 | return __init; |
| 92 | } |
| 93 | |
| 94 | template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> |
| 95 | inline _LIBCPP_INLINE_VISIBILITY |
| 96 | _Tp |
| 97 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, |
| 98 | _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) |
| 99 | { |
| 100 | for (; __first1 != __last1; ++__first1, ++__first2) |
| 101 | __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); |
| 102 | return __init; |
| 103 | } |
| 104 | |
| 105 | template <class _InputIterator, class _OutputIterator> |
| 106 | inline _LIBCPP_INLINE_VISIBILITY |
| 107 | _OutputIterator |
| 108 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 109 | { |
| 110 | if (__first != __last) |
| 111 | { |
| 112 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 113 | *__result = __t; |
| 114 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 115 | { |
| 116 | __t = __t + *__first; |
| 117 | *__result = __t; |
| 118 | } |
| 119 | } |
| 120 | return __result; |
| 121 | } |
| 122 | |
| 123 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 124 | inline _LIBCPP_INLINE_VISIBILITY |
| 125 | _OutputIterator |
| 126 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 127 | _BinaryOperation __binary_op) |
| 128 | { |
| 129 | if (__first != __last) |
| 130 | { |
| 131 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 132 | *__result = __t; |
| 133 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 134 | { |
| 135 | __t = __binary_op(__t, *__first); |
| 136 | *__result = __t; |
| 137 | } |
| 138 | } |
| 139 | return __result; |
| 140 | } |
| 141 | |
| 142 | template <class _InputIterator, class _OutputIterator> |
| 143 | inline _LIBCPP_INLINE_VISIBILITY |
| 144 | _OutputIterator |
| 145 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 146 | { |
| 147 | if (__first != __last) |
| 148 | { |
| 149 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 150 | *__result = __t1; |
| 151 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 152 | { |
| 153 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 154 | *__result = __t2 - __t1; |
| 155 | __t1 = __t2; |
| 156 | } |
| 157 | } |
| 158 | return __result; |
| 159 | } |
| 160 | |
| 161 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 162 | inline _LIBCPP_INLINE_VISIBILITY |
| 163 | _OutputIterator |
| 164 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 165 | _BinaryOperation __binary_op) |
| 166 | { |
| 167 | if (__first != __last) |
| 168 | { |
| 169 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 170 | *__result = __t1; |
| 171 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 172 | { |
| 173 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 174 | *__result = __binary_op(__t2, __t1); |
| 175 | __t1 = __t2; |
| 176 | } |
| 177 | } |
| 178 | return __result; |
| 179 | } |
| 180 | |
| 181 | _LIBCPP_END_NAMESPACE_STD |
| 182 | |
| 183 | #endif // _LIBCPP_NUMERIC |