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 | |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 56 | template <class M, class N> |
| 57 | constexpr common_type_t<M,N> gcd(M m, N n); // C++17 |
| 58 | |
| 59 | template <class M, class N> |
| 60 | constexpr common_type_t<M,N> lcm(M m, N n); // C++17 |
| 61 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | } // std |
| 63 | |
| 64 | */ |
| 65 | |
| 66 | #include <__config> |
| 67 | #include <iterator> |
| 68 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 69 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 71 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | |
| 73 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 74 | |
| 75 | template <class _InputIterator, class _Tp> |
| 76 | inline _LIBCPP_INLINE_VISIBILITY |
| 77 | _Tp |
| 78 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) |
| 79 | { |
| 80 | for (; __first != __last; ++__first) |
| 81 | __init = __init + *__first; |
| 82 | return __init; |
| 83 | } |
| 84 | |
| 85 | template <class _InputIterator, class _Tp, class _BinaryOperation> |
| 86 | inline _LIBCPP_INLINE_VISIBILITY |
| 87 | _Tp |
| 88 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) |
| 89 | { |
| 90 | for (; __first != __last; ++__first) |
| 91 | __init = __binary_op(__init, *__first); |
| 92 | return __init; |
| 93 | } |
| 94 | |
| 95 | template <class _InputIterator1, class _InputIterator2, class _Tp> |
| 96 | inline _LIBCPP_INLINE_VISIBILITY |
| 97 | _Tp |
| 98 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) |
| 99 | { |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 100 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 101 | __init = __init + *__first1 * *__first2; |
| 102 | return __init; |
| 103 | } |
| 104 | |
| 105 | template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> |
| 106 | inline _LIBCPP_INLINE_VISIBILITY |
| 107 | _Tp |
| 108 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, |
| 109 | _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) |
| 110 | { |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 111 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); |
| 113 | return __init; |
| 114 | } |
| 115 | |
| 116 | template <class _InputIterator, class _OutputIterator> |
| 117 | inline _LIBCPP_INLINE_VISIBILITY |
| 118 | _OutputIterator |
| 119 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 120 | { |
| 121 | if (__first != __last) |
| 122 | { |
| 123 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 124 | *__result = __t; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 125 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 | { |
| 127 | __t = __t + *__first; |
| 128 | *__result = __t; |
| 129 | } |
| 130 | } |
| 131 | return __result; |
| 132 | } |
| 133 | |
| 134 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 135 | inline _LIBCPP_INLINE_VISIBILITY |
| 136 | _OutputIterator |
| 137 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 138 | _BinaryOperation __binary_op) |
| 139 | { |
| 140 | if (__first != __last) |
| 141 | { |
| 142 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 143 | *__result = __t; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 144 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 145 | { |
| 146 | __t = __binary_op(__t, *__first); |
| 147 | *__result = __t; |
| 148 | } |
| 149 | } |
| 150 | return __result; |
| 151 | } |
| 152 | |
| 153 | template <class _InputIterator, class _OutputIterator> |
| 154 | inline _LIBCPP_INLINE_VISIBILITY |
| 155 | _OutputIterator |
| 156 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 157 | { |
| 158 | if (__first != __last) |
| 159 | { |
| 160 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 161 | *__result = __t1; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 162 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | { |
| 164 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 165 | *__result = __t2 - __t1; |
Howard Hinnant | 1c44f51 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 166 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | return __result; |
| 170 | } |
| 171 | |
| 172 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 173 | inline _LIBCPP_INLINE_VISIBILITY |
| 174 | _OutputIterator |
| 175 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 176 | _BinaryOperation __binary_op) |
| 177 | { |
| 178 | if (__first != __last) |
| 179 | { |
| 180 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 181 | *__result = __t1; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 182 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 183 | { |
| 184 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 185 | *__result = __binary_op(__t2, __t1); |
Howard Hinnant | 1c44f51 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 186 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | return __result; |
| 190 | } |
| 191 | |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 192 | template <class _ForwardIterator, class _Tp> |
| 193 | inline _LIBCPP_INLINE_VISIBILITY |
| 194 | void |
Howard Hinnant | 78b6828 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 195 | iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 196 | { |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 197 | for (; __first != __last; ++__first, (void) ++__value_) |
Howard Hinnant | 78b6828 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 198 | *__first = __value_; |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 201 | |
| 202 | #if _LIBCPP_STD_VER > 14 |
| 203 | template <typename _Tp, bool _IsSigned = is_signed<_Tp>::value> struct __abs; |
| 204 | |
| 205 | template <typename _Tp> |
| 206 | struct __abs<_Tp, true> { |
| 207 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 208 | _Tp operator()(_Tp __t) const noexcept { return __t >= 0 ? __t : -__t; } |
| 209 | }; |
| 210 | |
| 211 | template <typename _Tp> |
| 212 | struct __abs<_Tp, false> { |
| 213 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 214 | _Tp operator()(_Tp __t) const noexcept { return __t; } |
| 215 | }; |
| 216 | |
| 217 | |
| 218 | template<class _Tp> |
| 219 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 220 | _Tp __gcd(_Tp __m, _Tp __n) |
| 221 | { |
| 222 | static_assert((!is_signed<_Tp>::value), "" ); |
| 223 | return __n == 0 ? __m : __gcd<_Tp>(__n, __m % __n); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | template<class _Tp, class _Up> |
| 228 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 229 | common_type_t<_Tp,_Up> |
| 230 | gcd(_Tp __m, _Up __n) |
| 231 | { |
| 232 | static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); |
| 233 | using _Rp = common_type_t<_Tp,_Up>; |
| 234 | using _Wp = make_unsigned_t<_Rp>; |
| 235 | return static_cast<_Rp>(__gcd(static_cast<_Wp>(__abs<_Tp>()(__m)), |
| 236 | static_cast<_Wp>(__abs<_Up>()(__n)))); |
| 237 | } |
| 238 | |
| 239 | template<class _Tp, class _Up> |
| 240 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 241 | common_type_t<_Tp,_Up> |
| 242 | lcm(_Tp __m, _Up __n) |
| 243 | { |
| 244 | static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); |
| 245 | if (__m == 0 || __n == 0) |
| 246 | return 0; |
| 247 | |
| 248 | using _Rp = common_type_t<_Tp,_Up>; |
| 249 | _Rp __val1 = __abs<_Tp>()(__m) / gcd(__m,__n); |
| 250 | _Up __val2 = __abs<_Up>()(__n); |
| 251 | _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); |
| 252 | return __val1 * __val2; |
| 253 | } |
| 254 | |
| 255 | #endif /* _LIBCPP_STD_VER > 14 */ |
| 256 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | _LIBCPP_END_NAMESPACE_STD |
| 258 | |
| 259 | #endif // _LIBCPP_NUMERIC |