| 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 | // | 
|  | 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 |  | 
| 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 |  | 
|  | 63 | #pragma GCC system_header | 
|  | 64 |  | 
|  | 65 | _LIBCPP_BEGIN_NAMESPACE_STD | 
|  | 66 |  | 
|  | 67 | template <class _InputIterator, class _Tp> | 
|  | 68 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 69 | _Tp | 
|  | 70 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) | 
|  | 71 | { | 
|  | 72 | for (; __first != __last; ++__first) | 
|  | 73 | __init = __init + *__first; | 
|  | 74 | return __init; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | template <class _InputIterator, class _Tp, class _BinaryOperation> | 
|  | 78 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 79 | _Tp | 
|  | 80 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) | 
|  | 81 | { | 
|  | 82 | for (; __first != __last; ++__first) | 
|  | 83 | __init = __binary_op(__init, *__first); | 
|  | 84 | return __init; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | template <class _InputIterator1, class _InputIterator2, class _Tp> | 
|  | 88 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 89 | _Tp | 
|  | 90 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) | 
|  | 91 | { | 
|  | 92 | for (; __first1 != __last1; ++__first1, ++__first2) | 
|  | 93 | __init = __init + *__first1 * *__first2; | 
|  | 94 | return __init; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> | 
|  | 98 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 99 | _Tp | 
|  | 100 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, | 
|  | 101 | _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) | 
|  | 102 | { | 
|  | 103 | for (; __first1 != __last1; ++__first1, ++__first2) | 
|  | 104 | __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); | 
|  | 105 | return __init; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | template <class _InputIterator, class _OutputIterator> | 
|  | 109 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 110 | _OutputIterator | 
|  | 111 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) | 
|  | 112 | { | 
|  | 113 | if (__first != __last) | 
|  | 114 | { | 
|  | 115 | typename iterator_traits<_InputIterator>::value_type __t(*__first); | 
|  | 116 | *__result = __t; | 
|  | 117 | for (++__first, ++__result; __first != __last; ++__first, ++__result) | 
|  | 118 | { | 
|  | 119 | __t = __t + *__first; | 
|  | 120 | *__result = __t; | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 | return __result; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> | 
|  | 127 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 128 | _OutputIterator | 
|  | 129 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, | 
|  | 130 | _BinaryOperation __binary_op) | 
|  | 131 | { | 
|  | 132 | if (__first != __last) | 
|  | 133 | { | 
|  | 134 | typename iterator_traits<_InputIterator>::value_type __t(*__first); | 
|  | 135 | *__result = __t; | 
|  | 136 | for (++__first, ++__result; __first != __last; ++__first, ++__result) | 
|  | 137 | { | 
|  | 138 | __t = __binary_op(__t, *__first); | 
|  | 139 | *__result = __t; | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 | return __result; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | template <class _InputIterator, class _OutputIterator> | 
|  | 146 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 147 | _OutputIterator | 
|  | 148 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) | 
|  | 149 | { | 
|  | 150 | if (__first != __last) | 
|  | 151 | { | 
|  | 152 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); | 
|  | 153 | *__result = __t1; | 
|  | 154 | for (++__first, ++__result; __first != __last; ++__first, ++__result) | 
|  | 155 | { | 
|  | 156 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); | 
|  | 157 | *__result = __t2 - __t1; | 
|  | 158 | __t1 = __t2; | 
|  | 159 | } | 
|  | 160 | } | 
|  | 161 | return __result; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> | 
|  | 165 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 166 | _OutputIterator | 
|  | 167 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, | 
|  | 168 | _BinaryOperation __binary_op) | 
|  | 169 | { | 
|  | 170 | if (__first != __last) | 
|  | 171 | { | 
|  | 172 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); | 
|  | 173 | *__result = __t1; | 
|  | 174 | for (++__first, ++__result; __first != __last; ++__first, ++__result) | 
|  | 175 | { | 
|  | 176 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); | 
|  | 177 | *__result = __binary_op(__t2, __t1); | 
|  | 178 | __t1 = __t2; | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 | return __result; | 
|  | 182 | } | 
|  | 183 |  | 
| Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame^] | 184 | template <class _ForwardIterator, class _Tp> | 
|  | 185 | inline _LIBCPP_INLINE_VISIBILITY | 
|  | 186 | void | 
|  | 187 | iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value) | 
|  | 188 | { | 
|  | 189 | for (; __first != __last; ++__first, ++__value) | 
|  | 190 | *__first = __value; | 
|  | 191 | } | 
|  | 192 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | _LIBCPP_END_NAMESPACE_STD | 
|  | 194 |  | 
|  | 195 | #endif  // _LIBCPP_NUMERIC |