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 | |
Marshall Clow | fb97c44 | 2017-06-10 02:22:13 +0000 | [diff] [blame^] | 45 | template<class InputIterator, class OutputIterator, class T> |
| 46 | OutputIterator |
| 47 | exclusive_scan(InputIterator first, InputIterator last, |
| 48 | OutputIterator result, T init); // C++17 |
| 49 | |
| 50 | template<class InputIterator, class OutputIterator, class T, class BinaryOperation> |
| 51 | OutputIterator |
| 52 | exclusive_scan(InputIterator first, InputIterator last, |
| 53 | OutputIterator result, T init, BinaryOperation binary_op); // C++17 |
| 54 | |
| 55 | template<class InputIterator, class OutputIterator, class T, |
| 56 | class BinaryOperation, class UnaryOperation> |
| 57 | OutputIterator |
| 58 | transform_exclusive_scan(InputIterator first, InputIterator last, |
| 59 | OutputIterator result, T init, |
| 60 | BinaryOperation binary_op, UnaryOperation unary_op); // C++17 |
| 61 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | template <class InputIterator, class OutputIterator> |
| 63 | OutputIterator |
| 64 | adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); |
| 65 | |
| 66 | template <class InputIterator, class OutputIterator, class BinaryOperation> |
| 67 | OutputIterator |
| 68 | adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); |
| 69 | |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 70 | template <class ForwardIterator, class T> |
| 71 | void iota(ForwardIterator first, ForwardIterator last, T value); |
| 72 | |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 73 | template <class M, class N> |
| 74 | constexpr common_type_t<M,N> gcd(M m, N n); // C++17 |
| 75 | |
| 76 | template <class M, class N> |
| 77 | constexpr common_type_t<M,N> lcm(M m, N n); // C++17 |
| 78 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 79 | } // std |
| 80 | |
| 81 | */ |
| 82 | |
| 83 | #include <__config> |
| 84 | #include <iterator> |
Eric Fiselier | 34da9b5 | 2017-02-08 00:14:13 +0000 | [diff] [blame] | 85 | #include <limits> // for numeric_limits |
Marshall Clow | fb97c44 | 2017-06-10 02:22:13 +0000 | [diff] [blame^] | 86 | #include <functional> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 88 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 90 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 92 | _LIBCPP_PUSH_MACROS |
| 93 | #include <__undef_macros> |
| 94 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 95 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 96 | |
| 97 | template <class _InputIterator, class _Tp> |
| 98 | inline _LIBCPP_INLINE_VISIBILITY |
| 99 | _Tp |
| 100 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) |
| 101 | { |
| 102 | for (; __first != __last; ++__first) |
| 103 | __init = __init + *__first; |
| 104 | return __init; |
| 105 | } |
| 106 | |
| 107 | template <class _InputIterator, class _Tp, class _BinaryOperation> |
| 108 | inline _LIBCPP_INLINE_VISIBILITY |
| 109 | _Tp |
| 110 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) |
| 111 | { |
| 112 | for (; __first != __last; ++__first) |
| 113 | __init = __binary_op(__init, *__first); |
| 114 | return __init; |
| 115 | } |
| 116 | |
| 117 | template <class _InputIterator1, class _InputIterator2, class _Tp> |
| 118 | inline _LIBCPP_INLINE_VISIBILITY |
| 119 | _Tp |
| 120 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) |
| 121 | { |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 122 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | __init = __init + *__first1 * *__first2; |
| 124 | return __init; |
| 125 | } |
| 126 | |
| 127 | template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> |
| 128 | inline _LIBCPP_INLINE_VISIBILITY |
| 129 | _Tp |
| 130 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, |
| 131 | _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) |
| 132 | { |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 133 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); |
| 135 | return __init; |
| 136 | } |
| 137 | |
| 138 | template <class _InputIterator, class _OutputIterator> |
| 139 | inline _LIBCPP_INLINE_VISIBILITY |
| 140 | _OutputIterator |
| 141 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 142 | { |
| 143 | if (__first != __last) |
| 144 | { |
| 145 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 146 | *__result = __t; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 147 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 148 | { |
| 149 | __t = __t + *__first; |
| 150 | *__result = __t; |
| 151 | } |
| 152 | } |
| 153 | return __result; |
| 154 | } |
| 155 | |
| 156 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 157 | inline _LIBCPP_INLINE_VISIBILITY |
| 158 | _OutputIterator |
| 159 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 160 | _BinaryOperation __binary_op) |
| 161 | { |
| 162 | if (__first != __last) |
| 163 | { |
| 164 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 165 | *__result = __t; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 166 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | { |
| 168 | __t = __binary_op(__t, *__first); |
| 169 | *__result = __t; |
| 170 | } |
| 171 | } |
| 172 | return __result; |
| 173 | } |
| 174 | |
Marshall Clow | fb97c44 | 2017-06-10 02:22:13 +0000 | [diff] [blame^] | 175 | #if _LIBCPP_STD_VER > 14 |
| 176 | template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> |
| 177 | inline _LIBCPP_INLINE_VISIBILITY |
| 178 | _OutputIterator |
| 179 | exclusive_scan(_InputIterator __first, _InputIterator __last, |
| 180 | _OutputIterator __result, _Tp __init, _BinaryOp __b) |
| 181 | { |
| 182 | if (__first != __last) |
| 183 | { |
| 184 | _Tp __saved = __init; |
| 185 | do |
| 186 | { |
| 187 | __init = __b(__init, *__first); |
| 188 | *__result = __saved; |
| 189 | __saved = __init; |
| 190 | ++__result; |
| 191 | } while (++__first != __last); |
| 192 | } |
| 193 | return __result; |
| 194 | } |
| 195 | |
| 196 | template <class _InputIterator, class _OutputIterator, class _Tp> |
| 197 | inline _LIBCPP_INLINE_VISIBILITY |
| 198 | _OutputIterator |
| 199 | exclusive_scan(_InputIterator __first, _InputIterator __last, |
| 200 | _OutputIterator __result, _Tp __init) |
| 201 | { |
| 202 | return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>()); |
| 203 | } |
| 204 | |
| 205 | template <class _InputIterator, class _OutputIterator, class _Tp, |
| 206 | class _BinaryOp, class _UnaryOp> |
| 207 | inline _LIBCPP_INLINE_VISIBILITY |
| 208 | _OutputIterator |
| 209 | transform_exclusive_scan(_InputIterator __first, _InputIterator __last, |
| 210 | _OutputIterator __result, _Tp __init, |
| 211 | _BinaryOp __b, _UnaryOp __u) |
| 212 | { |
| 213 | if (__first != __last) |
| 214 | { |
| 215 | _Tp __saved = __init; |
| 216 | do |
| 217 | { |
| 218 | __init = __b(__init, __u(*__first)); |
| 219 | *__result = __saved; |
| 220 | __saved = __init; |
| 221 | ++__result; |
| 222 | } while (++__first != __last); |
| 223 | } |
| 224 | return __result; |
| 225 | } |
| 226 | #endif |
| 227 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 228 | template <class _InputIterator, class _OutputIterator> |
| 229 | inline _LIBCPP_INLINE_VISIBILITY |
| 230 | _OutputIterator |
| 231 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 232 | { |
| 233 | if (__first != __last) |
| 234 | { |
| 235 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 236 | *__result = __t1; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 237 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 238 | { |
| 239 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 240 | *__result = __t2 - __t1; |
Howard Hinnant | 1c44f51 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 241 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | return __result; |
| 245 | } |
| 246 | |
| 247 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 248 | inline _LIBCPP_INLINE_VISIBILITY |
| 249 | _OutputIterator |
| 250 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 251 | _BinaryOperation __binary_op) |
| 252 | { |
| 253 | if (__first != __last) |
| 254 | { |
| 255 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 256 | *__result = __t1; |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 257 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 258 | { |
| 259 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 260 | *__result = __binary_op(__t2, __t1); |
Howard Hinnant | 1c44f51 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 261 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | return __result; |
| 265 | } |
| 266 | |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 267 | template <class _ForwardIterator, class _Tp> |
| 268 | inline _LIBCPP_INLINE_VISIBILITY |
| 269 | void |
Howard Hinnant | 78b6828 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 270 | iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 271 | { |
Eric Fiselier | b991975 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 272 | for (; __first != __last; ++__first, (void) ++__value_) |
Howard Hinnant | 78b6828 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 273 | *__first = __value_; |
Howard Hinnant | ebef745 | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 276 | |
| 277 | #if _LIBCPP_STD_VER > 14 |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 278 | template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs; |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 279 | |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 280 | template <typename _Result, typename _Source> |
| 281 | struct __abs<_Result, _Source, true> { |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 282 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 283 | _Result operator()(_Source __t) const noexcept |
| 284 | { |
| 285 | if (__t >= 0) return __t; |
| 286 | if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t); |
| 287 | return -__t; |
| 288 | } |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 291 | template <typename _Result, typename _Source> |
| 292 | struct __abs<_Result, _Source, false> { |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 293 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 294 | _Result operator()(_Source __t) const noexcept { return __t; } |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | |
| 298 | template<class _Tp> |
Eric Fiselier | 9197008 | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 299 | _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 300 | _Tp __gcd(_Tp __m, _Tp __n) |
| 301 | { |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 302 | static_assert((!is_signed<_Tp>::value), ""); |
Eric Fiselier | 9197008 | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 303 | return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n); |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | template<class _Tp, class _Up> |
| 308 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 309 | common_type_t<_Tp,_Up> |
| 310 | gcd(_Tp __m, _Up __n) |
| 311 | { |
| 312 | static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); |
Marshall Clow | db7fa11 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 313 | static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" ); |
| 314 | static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" ); |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 315 | using _Rp = common_type_t<_Tp,_Up>; |
| 316 | using _Wp = make_unsigned_t<_Rp>; |
Eric Fiselier | 9197008 | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 317 | return static_cast<_Rp>(_VSTD::__gcd( |
| 318 | static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)), |
| 319 | static_cast<_Wp>(__abs<_Rp, _Up>()(__n)))); |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | template<class _Tp, class _Up> |
| 323 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 324 | common_type_t<_Tp,_Up> |
| 325 | lcm(_Tp __m, _Up __n) |
| 326 | { |
| 327 | static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); |
Marshall Clow | db7fa11 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 328 | static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" ); |
| 329 | static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" ); |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 330 | if (__m == 0 || __n == 0) |
| 331 | return 0; |
| 332 | |
| 333 | using _Rp = common_type_t<_Tp,_Up>; |
Eric Fiselier | 9197008 | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 334 | _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n); |
Marshall Clow | ebf66a3 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 335 | _Rp __val2 = __abs<_Rp, _Up>()(__n); |
Marshall Clow | 1c1e91d | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 336 | _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); |
| 337 | return __val1 * __val2; |
| 338 | } |
| 339 | |
| 340 | #endif /* _LIBCPP_STD_VER > 14 */ |
| 341 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 342 | _LIBCPP_END_NAMESPACE_STD |
| 343 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 344 | _LIBCPP_POP_MACROS |
| 345 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 346 | #endif // _LIBCPP_NUMERIC |