Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- numeric ---------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 53 | template <class ForwardIterator, class T> |
| 54 | void iota(ForwardIterator first, ForwardIterator last, T value); |
| 55 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | } // std |
| 57 | |
| 58 | */ |
| 59 | |
| 60 | #include <__config> |
| 61 | #include <iterator> |
| 62 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 63 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 65 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | |
| 67 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 68 | |
| 69 | template <class _InputIterator, class _Tp> |
| 70 | inline _LIBCPP_INLINE_VISIBILITY |
| 71 | _Tp |
| 72 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) |
| 73 | { |
| 74 | for (; __first != __last; ++__first) |
| 75 | __init = __init + *__first; |
| 76 | return __init; |
| 77 | } |
| 78 | |
| 79 | template <class _InputIterator, class _Tp, class _BinaryOperation> |
| 80 | inline _LIBCPP_INLINE_VISIBILITY |
| 81 | _Tp |
| 82 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) |
| 83 | { |
| 84 | for (; __first != __last; ++__first) |
| 85 | __init = __binary_op(__init, *__first); |
| 86 | return __init; |
| 87 | } |
| 88 | |
| 89 | template <class _InputIterator1, class _InputIterator2, class _Tp> |
| 90 | inline _LIBCPP_INLINE_VISIBILITY |
| 91 | _Tp |
| 92 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) |
| 93 | { |
| 94 | for (; __first1 != __last1; ++__first1, ++__first2) |
| 95 | __init = __init + *__first1 * *__first2; |
| 96 | return __init; |
| 97 | } |
| 98 | |
| 99 | template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> |
| 100 | inline _LIBCPP_INLINE_VISIBILITY |
| 101 | _Tp |
| 102 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, |
| 103 | _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) |
| 104 | { |
| 105 | for (; __first1 != __last1; ++__first1, ++__first2) |
| 106 | __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); |
| 107 | return __init; |
| 108 | } |
| 109 | |
| 110 | template <class _InputIterator, class _OutputIterator> |
| 111 | inline _LIBCPP_INLINE_VISIBILITY |
| 112 | _OutputIterator |
| 113 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 114 | { |
| 115 | if (__first != __last) |
| 116 | { |
| 117 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 118 | *__result = __t; |
| 119 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 120 | { |
| 121 | __t = __t + *__first; |
| 122 | *__result = __t; |
| 123 | } |
| 124 | } |
| 125 | return __result; |
| 126 | } |
| 127 | |
| 128 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 129 | inline _LIBCPP_INLINE_VISIBILITY |
| 130 | _OutputIterator |
| 131 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 132 | _BinaryOperation __binary_op) |
| 133 | { |
| 134 | if (__first != __last) |
| 135 | { |
| 136 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 137 | *__result = __t; |
| 138 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 139 | { |
| 140 | __t = __binary_op(__t, *__first); |
| 141 | *__result = __t; |
| 142 | } |
| 143 | } |
| 144 | return __result; |
| 145 | } |
| 146 | |
| 147 | template <class _InputIterator, class _OutputIterator> |
| 148 | inline _LIBCPP_INLINE_VISIBILITY |
| 149 | _OutputIterator |
| 150 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 151 | { |
| 152 | if (__first != __last) |
| 153 | { |
| 154 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 155 | *__result = __t1; |
| 156 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 157 | { |
| 158 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 159 | *__result = __t2 - __t1; |
Howard Hinnant | 1c44f51 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 160 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | return __result; |
| 164 | } |
| 165 | |
| 166 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 167 | inline _LIBCPP_INLINE_VISIBILITY |
| 168 | _OutputIterator |
| 169 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 170 | _BinaryOperation __binary_op) |
| 171 | { |
| 172 | if (__first != __last) |
| 173 | { |
| 174 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 175 | *__result = __t1; |
| 176 | for (++__first, ++__result; __first != __last; ++__first, ++__result) |
| 177 | { |
| 178 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 179 | *__result = __binary_op(__t2, __t1); |
Howard Hinnant | 1c44f51 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 180 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | return __result; |
| 184 | } |
| 185 | |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 186 | template <class _ForwardIterator, class _Tp> |
| 187 | inline _LIBCPP_INLINE_VISIBILITY |
| 188 | void |
Howard Hinnant | 78b6828 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 189 | iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 190 | { |
Howard Hinnant | 78b6828 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 191 | for (; __first != __last; ++__first, ++__value_) |
| 192 | *__first = __value_; |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 195 | _LIBCPP_END_NAMESPACE_STD |
| 196 | |
| 197 | #endif // _LIBCPP_NUMERIC |