Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- numeric ---------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_NUMERIC |
| 11 | #define _LIBCPP_NUMERIC |
| 12 | |
| 13 | /* |
| 14 | numeric synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | template <class InputIterator, class T> |
| 20 | T |
| 21 | accumulate(InputIterator first, InputIterator last, T init); |
| 22 | |
| 23 | template <class InputIterator, class T, class BinaryOperation> |
| 24 | T |
| 25 | accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); |
| 26 | |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 27 | template<class InputIterator> |
| 28 | typename iterator_traits<InputIterator>::value_type |
| 29 | reduce(InputIterator first, InputIterator last); // C++17 |
| 30 | |
| 31 | template<class InputIterator, class T> |
| 32 | T |
| 33 | reduce(InputIterator first, InputIterator last, T init); // C++17 |
| 34 | |
| 35 | template<class InputIterator, class T, class BinaryOperation> |
| 36 | T |
| 37 | reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17 |
| 38 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | template <class InputIterator1, class InputIterator2, class T> |
| 40 | T |
| 41 | inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init); |
| 42 | |
| 43 | template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> |
| 44 | T |
| 45 | inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, |
| 46 | T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); |
| 47 | |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 48 | |
| 49 | template<class InputIterator1, class InputIterator2, class T> |
| 50 | T |
| 51 | transform_reduce(InputIterator1 first1, InputIterator1 last1, |
| 52 | InputIterator2 first2, T init); // C++17 |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 53 | |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 54 | template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> |
| 55 | T |
| 56 | transform_reduce(InputIterator1 first1, InputIterator1 last1, |
| 57 | InputIterator2 first2, T init, |
| 58 | BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17 |
| 59 | |
| 60 | template<class InputIterator, class T, class BinaryOperation, class UnaryOperation> |
| 61 | T |
| 62 | transform_reduce(InputIterator first, InputIterator last, T init, |
| 63 | BinaryOperation binary_op, UnaryOperation unary_op); // C++17 |
| 64 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | template <class InputIterator, class OutputIterator> |
| 66 | OutputIterator |
| 67 | partial_sum(InputIterator first, InputIterator last, OutputIterator result); |
| 68 | |
| 69 | template <class InputIterator, class OutputIterator, class BinaryOperation> |
| 70 | OutputIterator |
| 71 | partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); |
| 72 | |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 73 | template<class InputIterator, class OutputIterator, class T> |
| 74 | OutputIterator |
| 75 | exclusive_scan(InputIterator first, InputIterator last, |
| 76 | OutputIterator result, T init); // C++17 |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 77 | |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 78 | template<class InputIterator, class OutputIterator, class T, class BinaryOperation> |
| 79 | OutputIterator |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 80 | exclusive_scan(InputIterator first, InputIterator last, |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 81 | OutputIterator result, T init, BinaryOperation binary_op); // C++17 |
| 82 | |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 83 | template<class InputIterator, class OutputIterator> |
| 84 | OutputIterator |
| 85 | inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17 |
| 86 | |
| 87 | template<class InputIterator, class OutputIterator, class BinaryOperation> |
| 88 | OutputIterator |
| 89 | inclusive_scan(InputIterator first, InputIterator last, |
| 90 | OutputIterator result, BinaryOperation binary_op); // C++17 |
| 91 | |
| 92 | template<class InputIterator, class OutputIterator, class BinaryOperation, class T> |
| 93 | OutputIterator |
| 94 | inclusive_scan(InputIterator first, InputIterator last, |
| 95 | OutputIterator result, BinaryOperation binary_op, T init); // C++17 |
| 96 | |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 97 | template<class InputIterator, class OutputIterator, class T, |
| 98 | class BinaryOperation, class UnaryOperation> |
| 99 | OutputIterator |
| 100 | transform_exclusive_scan(InputIterator first, InputIterator last, |
| 101 | OutputIterator result, T init, |
| 102 | BinaryOperation binary_op, UnaryOperation unary_op); // C++17 |
| 103 | |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 104 | template<class InputIterator, class OutputIterator, |
| 105 | class BinaryOperation, class UnaryOperation> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 106 | OutputIterator |
| 107 | transform_inclusive_scan(InputIterator first, InputIterator last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 108 | OutputIterator result, |
| 109 | BinaryOperation binary_op, UnaryOperation unary_op); // C++17 |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 110 | |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 111 | template<class InputIterator, class OutputIterator, |
| 112 | class BinaryOperation, class UnaryOperation, class T> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 113 | OutputIterator |
| 114 | transform_inclusive_scan(InputIterator first, InputIterator last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 115 | OutputIterator result, |
| 116 | BinaryOperation binary_op, UnaryOperation unary_op, |
| 117 | T init); // C++17 |
| 118 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | template <class InputIterator, class OutputIterator> |
| 120 | OutputIterator |
| 121 | adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); |
| 122 | |
| 123 | template <class InputIterator, class OutputIterator, class BinaryOperation> |
| 124 | OutputIterator |
| 125 | adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); |
| 126 | |
Howard Hinnant | f45797f | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 127 | template <class ForwardIterator, class T> |
| 128 | void iota(ForwardIterator first, ForwardIterator last, T value); |
| 129 | |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 130 | template <class M, class N> |
| 131 | constexpr common_type_t<M,N> gcd(M m, N n); // C++17 |
| 132 | |
| 133 | template <class M, class N> |
| 134 | constexpr common_type_t<M,N> lcm(M m, N n); // C++17 |
| 135 | |
Marshall Clow | a5f2c8a | 2019-03-14 16:25:55 +0000 | [diff] [blame] | 136 | integer midpoint(integer a, integer b); // C++20 |
| 137 | pointer midpoint(pointer a, pointer b); // C++20 |
| 138 | floating_point midpoint(floating_point a, floating_point b); // C++20 |
| 139 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 140 | } // std |
| 141 | |
| 142 | */ |
| 143 | |
| 144 | #include <__config> |
| 145 | #include <iterator> |
Eric Fiselier | e7af1d8 | 2017-02-08 00:14:13 +0000 | [diff] [blame] | 146 | #include <limits> // for numeric_limits |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 147 | #include <functional> |
Marshall Clow | 174901e | 2019-04-25 12:11:43 +0000 | [diff] [blame] | 148 | #include <cmath> // for isnormal |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 149 | #include <version> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 150 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 151 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 152 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 153 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 155 | _LIBCPP_PUSH_MACROS |
| 156 | #include <__undef_macros> |
| 157 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 158 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 159 | |
| 160 | template <class _InputIterator, class _Tp> |
| 161 | inline _LIBCPP_INLINE_VISIBILITY |
| 162 | _Tp |
| 163 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) |
| 164 | { |
| 165 | for (; __first != __last; ++__first) |
| 166 | __init = __init + *__first; |
| 167 | return __init; |
| 168 | } |
| 169 | |
| 170 | template <class _InputIterator, class _Tp, class _BinaryOperation> |
| 171 | inline _LIBCPP_INLINE_VISIBILITY |
| 172 | _Tp |
| 173 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) |
| 174 | { |
| 175 | for (; __first != __last; ++__first) |
| 176 | __init = __binary_op(__init, *__first); |
| 177 | return __init; |
| 178 | } |
| 179 | |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 180 | #if _LIBCPP_STD_VER > 14 |
| 181 | template <class _InputIterator, class _Tp, class _BinaryOp> |
| 182 | inline _LIBCPP_INLINE_VISIBILITY |
| 183 | _Tp |
| 184 | reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) |
| 185 | { |
| 186 | for (; __first != __last; ++__first) |
| 187 | __init = __b(__init, *__first); |
| 188 | return __init; |
| 189 | } |
| 190 | |
| 191 | template <class _InputIterator, class _Tp> |
| 192 | inline _LIBCPP_INLINE_VISIBILITY |
| 193 | _Tp |
| 194 | reduce(_InputIterator __first, _InputIterator __last, _Tp __init) |
| 195 | { |
| 196 | return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>()); |
| 197 | } |
| 198 | |
| 199 | template <class _InputIterator> |
| 200 | inline _LIBCPP_INLINE_VISIBILITY |
| 201 | typename iterator_traits<_InputIterator>::value_type |
| 202 | reduce(_InputIterator __first, _InputIterator __last) |
| 203 | { |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 204 | return _VSTD::reduce(__first, __last, |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 205 | typename iterator_traits<_InputIterator>::value_type{}); |
| 206 | } |
| 207 | #endif |
| 208 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 209 | template <class _InputIterator1, class _InputIterator2, class _Tp> |
| 210 | inline _LIBCPP_INLINE_VISIBILITY |
| 211 | _Tp |
| 212 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) |
| 213 | { |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 214 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 215 | __init = __init + *__first1 * *__first2; |
| 216 | return __init; |
| 217 | } |
| 218 | |
| 219 | template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> |
| 220 | inline _LIBCPP_INLINE_VISIBILITY |
| 221 | _Tp |
| 222 | inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, |
| 223 | _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) |
| 224 | { |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 225 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 226 | __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); |
| 227 | return __init; |
| 228 | } |
| 229 | |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 230 | #if _LIBCPP_STD_VER > 14 |
| 231 | template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp> |
| 232 | inline _LIBCPP_INLINE_VISIBILITY |
| 233 | _Tp |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 234 | transform_reduce(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 235 | _Tp __init, _BinaryOp __b, _UnaryOp __u) |
| 236 | { |
| 237 | for (; __first != __last; ++__first) |
| 238 | __init = __b(__init, __u(*__first)); |
| 239 | return __init; |
| 240 | } |
| 241 | |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 242 | template <class _InputIterator1, class _InputIterator2, |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 243 | class _Tp, class _BinaryOp1, class _BinaryOp2> |
| 244 | inline _LIBCPP_INLINE_VISIBILITY |
| 245 | _Tp |
| 246 | transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, |
| 247 | _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2) |
| 248 | { |
| 249 | for (; __first1 != __last1; ++__first1, (void) ++__first2) |
| 250 | __init = __b1(__init, __b2(*__first1, *__first2)); |
| 251 | return __init; |
| 252 | } |
| 253 | |
| 254 | template <class _InputIterator1, class _InputIterator2, class _Tp> |
| 255 | inline _LIBCPP_INLINE_VISIBILITY |
| 256 | _Tp |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 257 | transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 258 | _InputIterator2 __first2, _Tp __init) |
| 259 | { |
Billy Robert O'Neal III | 497250a | 2018-01-05 01:31:57 +0000 | [diff] [blame] | 260 | return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init), |
Marshall Clow | 3b7cad2 | 2017-06-14 04:48:45 +0000 | [diff] [blame] | 261 | _VSTD::plus<>(), _VSTD::multiplies<>()); |
| 262 | } |
| 263 | #endif |
| 264 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 265 | template <class _InputIterator, class _OutputIterator> |
| 266 | inline _LIBCPP_INLINE_VISIBILITY |
| 267 | _OutputIterator |
| 268 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 269 | { |
| 270 | if (__first != __last) |
| 271 | { |
| 272 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 273 | *__result = __t; |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 274 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 275 | { |
| 276 | __t = __t + *__first; |
| 277 | *__result = __t; |
| 278 | } |
| 279 | } |
| 280 | return __result; |
| 281 | } |
| 282 | |
| 283 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 284 | inline _LIBCPP_INLINE_VISIBILITY |
| 285 | _OutputIterator |
| 286 | partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 287 | _BinaryOperation __binary_op) |
| 288 | { |
| 289 | if (__first != __last) |
| 290 | { |
| 291 | typename iterator_traits<_InputIterator>::value_type __t(*__first); |
| 292 | *__result = __t; |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 293 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 294 | { |
| 295 | __t = __binary_op(__t, *__first); |
| 296 | *__result = __t; |
| 297 | } |
| 298 | } |
| 299 | return __result; |
| 300 | } |
| 301 | |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 302 | #if _LIBCPP_STD_VER > 14 |
| 303 | template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> |
| 304 | inline _LIBCPP_INLINE_VISIBILITY |
| 305 | _OutputIterator |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 306 | exclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 307 | _OutputIterator __result, _Tp __init, _BinaryOp __b) |
| 308 | { |
| 309 | if (__first != __last) |
| 310 | { |
| 311 | _Tp __saved = __init; |
| 312 | do |
| 313 | { |
| 314 | __init = __b(__init, *__first); |
| 315 | *__result = __saved; |
| 316 | __saved = __init; |
| 317 | ++__result; |
| 318 | } while (++__first != __last); |
| 319 | } |
| 320 | return __result; |
| 321 | } |
| 322 | |
| 323 | template <class _InputIterator, class _OutputIterator, class _Tp> |
| 324 | inline _LIBCPP_INLINE_VISIBILITY |
| 325 | _OutputIterator |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 326 | exclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 327 | _OutputIterator __result, _Tp __init) |
| 328 | { |
| 329 | return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>()); |
| 330 | } |
| 331 | |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 332 | template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 333 | _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 334 | _OutputIterator __result, _BinaryOp __b, _Tp __init) |
| 335 | { |
| 336 | for (; __first != __last; ++__first, (void) ++__result) { |
| 337 | __init = __b(__init, *__first); |
| 338 | *__result = __init; |
| 339 | } |
| 340 | return __result; |
| 341 | } |
| 342 | |
| 343 | template <class _InputIterator, class _OutputIterator, class _BinaryOp> |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 344 | _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 345 | _OutputIterator __result, _BinaryOp __b) |
| 346 | { |
| 347 | if (__first != __last) { |
| 348 | typename std::iterator_traits<_InputIterator>::value_type __init = *__first; |
| 349 | *__result++ = __init; |
| 350 | if (++__first != __last) |
| 351 | return _VSTD::inclusive_scan(__first, __last, __result, __b, __init); |
| 352 | } |
| 353 | |
| 354 | return __result; |
| 355 | } |
| 356 | |
| 357 | template <class _InputIterator, class _OutputIterator> |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 358 | _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 359 | _OutputIterator __result) |
| 360 | { |
| 361 | return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>()); |
| 362 | } |
| 363 | |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 364 | template <class _InputIterator, class _OutputIterator, class _Tp, |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 365 | class _BinaryOp, class _UnaryOp> |
| 366 | inline _LIBCPP_INLINE_VISIBILITY |
| 367 | _OutputIterator |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 368 | transform_exclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 369 | _OutputIterator __result, _Tp __init, |
| 370 | _BinaryOp __b, _UnaryOp __u) |
| 371 | { |
| 372 | if (__first != __last) |
| 373 | { |
| 374 | _Tp __saved = __init; |
| 375 | do |
| 376 | { |
| 377 | __init = __b(__init, __u(*__first)); |
| 378 | *__result = __saved; |
| 379 | __saved = __init; |
| 380 | ++__result; |
| 381 | } while (++__first != __last); |
| 382 | } |
| 383 | return __result; |
| 384 | } |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 385 | |
| 386 | template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp> |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 387 | _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 388 | _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init) |
| 389 | { |
| 390 | for (; __first != __last; ++__first, (void) ++__result) { |
| 391 | __init = __b(__init, __u(*__first)); |
| 392 | *__result = __init; |
| 393 | } |
| 394 | |
| 395 | return __result; |
| 396 | } |
| 397 | |
| 398 | template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp> |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 399 | _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 400 | _OutputIterator __result, _BinaryOp __b, _UnaryOp __u) |
| 401 | { |
| 402 | if (__first != __last) { |
| 403 | typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first); |
| 404 | *__result++ = __init; |
| 405 | if (++__first != __last) |
| 406 | return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init); |
| 407 | } |
Billy Robert O'Neal III | 876ecf9 | 2018-01-05 01:31:55 +0000 | [diff] [blame] | 408 | |
Marshall Clow | 4c56144 | 2017-06-23 05:12:42 +0000 | [diff] [blame] | 409 | return __result; |
| 410 | } |
Marshall Clow | 0ce2499 | 2017-06-10 02:22:13 +0000 | [diff] [blame] | 411 | #endif |
| 412 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 413 | template <class _InputIterator, class _OutputIterator> |
| 414 | inline _LIBCPP_INLINE_VISIBILITY |
| 415 | _OutputIterator |
| 416 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) |
| 417 | { |
| 418 | if (__first != __last) |
| 419 | { |
| 420 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 421 | *__result = __t1; |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 422 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | { |
| 424 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 425 | *__result = __t2 - __t1; |
Howard Hinnant | 528ceb7 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 426 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | return __result; |
| 430 | } |
| 431 | |
| 432 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
| 433 | inline _LIBCPP_INLINE_VISIBILITY |
| 434 | _OutputIterator |
| 435 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, |
| 436 | _BinaryOperation __binary_op) |
| 437 | { |
| 438 | if (__first != __last) |
| 439 | { |
| 440 | typename iterator_traits<_InputIterator>::value_type __t1(*__first); |
| 441 | *__result = __t1; |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 442 | for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 443 | { |
| 444 | typename iterator_traits<_InputIterator>::value_type __t2(*__first); |
| 445 | *__result = __binary_op(__t2, __t1); |
Howard Hinnant | 528ceb7 | 2013-08-22 18:02:34 +0000 | [diff] [blame] | 446 | __t1 = _VSTD::move(__t2); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | return __result; |
| 450 | } |
| 451 | |
Howard Hinnant | f45797f | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 452 | template <class _ForwardIterator, class _Tp> |
| 453 | inline _LIBCPP_INLINE_VISIBILITY |
| 454 | void |
Howard Hinnant | bf07402 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 455 | iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) |
Howard Hinnant | f45797f | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 456 | { |
Eric Fiselier | a09a3b4 | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 457 | for (; __first != __last; ++__first, (void) ++__value_) |
Howard Hinnant | bf07402 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 458 | *__first = __value_; |
Howard Hinnant | f45797f | 2010-05-26 18:53:44 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 461 | |
| 462 | #if _LIBCPP_STD_VER > 14 |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 463 | template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs; |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 464 | |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 465 | template <typename _Result, typename _Source> |
| 466 | struct __abs<_Result, _Source, true> { |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 467 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 468 | _Result operator()(_Source __t) const noexcept |
| 469 | { |
| 470 | if (__t >= 0) return __t; |
| 471 | if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t); |
| 472 | return -__t; |
| 473 | } |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 476 | template <typename _Result, typename _Source> |
| 477 | struct __abs<_Result, _Source, false> { |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 478 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 479 | _Result operator()(_Source __t) const noexcept { return __t; } |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | |
| 483 | template<class _Tp> |
Eric Fiselier | ed86f1f | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 484 | _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 485 | _Tp __gcd(_Tp __m, _Tp __n) |
| 486 | { |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 487 | static_assert((!is_signed<_Tp>::value), ""); |
Eric Fiselier | ed86f1f | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 488 | return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n); |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | template<class _Tp, class _Up> |
| 493 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 494 | common_type_t<_Tp,_Up> |
| 495 | gcd(_Tp __m, _Up __n) |
| 496 | { |
| 497 | static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); |
Marshall Clow | 78dbe46 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 498 | static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" ); |
| 499 | static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" ); |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 500 | using _Rp = common_type_t<_Tp,_Up>; |
| 501 | using _Wp = make_unsigned_t<_Rp>; |
Eric Fiselier | ed86f1f | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 502 | return static_cast<_Rp>(_VSTD::__gcd( |
| 503 | static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)), |
| 504 | static_cast<_Wp>(__abs<_Rp, _Up>()(__n)))); |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | template<class _Tp, class _Up> |
| 508 | _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY |
| 509 | common_type_t<_Tp,_Up> |
| 510 | lcm(_Tp __m, _Up __n) |
| 511 | { |
| 512 | static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); |
Marshall Clow | 78dbe46 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 513 | static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" ); |
| 514 | static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" ); |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 515 | if (__m == 0 || __n == 0) |
| 516 | return 0; |
| 517 | |
| 518 | using _Rp = common_type_t<_Tp,_Up>; |
Eric Fiselier | ed86f1f | 2017-05-09 00:00:00 +0000 | [diff] [blame] | 519 | _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n); |
Marshall Clow | f931672 | 2017-02-10 20:49:08 +0000 | [diff] [blame] | 520 | _Rp __val2 = __abs<_Rp, _Up>()(__n); |
Marshall Clow | b8bfc2c | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 521 | _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); |
| 522 | return __val1 * __val2; |
| 523 | } |
| 524 | |
| 525 | #endif /* _LIBCPP_STD_VER > 14 */ |
| 526 | |
Marshall Clow | a5f2c8a | 2019-03-14 16:25:55 +0000 | [diff] [blame] | 527 | #if _LIBCPP_STD_VER > 17 |
| 528 | template <class _Tp> |
| 529 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 530 | enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp>, _Tp> |
| 531 | midpoint(_Tp __a, _Tp __b) noexcept |
| 532 | _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK |
| 533 | { |
| 534 | using _Up = std::make_unsigned_t<_Tp>; |
| 535 | |
| 536 | int __sign = 1; |
| 537 | _Up __m = __a; |
| 538 | _Up __M = __b; |
| 539 | if (__a > __b) |
| 540 | { |
| 541 | __sign = -1; |
| 542 | __m = __b; |
| 543 | __M = __a; |
| 544 | } |
| 545 | return __a + __sign * _Tp(_Up(__M-__m) >> 1); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | template <class _TPtr> |
| 550 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 551 | enable_if_t<is_pointer_v<_TPtr>, _TPtr> |
| 552 | midpoint(_TPtr __a, _TPtr __b) noexcept |
| 553 | { |
| 554 | return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a); |
| 555 | } |
Marshall Clow | 174901e | 2019-04-25 12:11:43 +0000 | [diff] [blame] | 556 | |
| 557 | |
| 558 | template <typename _Tp> |
Marshall Clow | 2dc0ef1 | 2019-05-07 16:07:24 +0000 | [diff] [blame^] | 559 | constexpr int __sign(_Tp __val) { |
Marshall Clow | 174901e | 2019-04-25 12:11:43 +0000 | [diff] [blame] | 560 | return (_Tp(0) < __val) - (__val < _Tp(0)); |
| 561 | } |
| 562 | |
| 563 | template <class _Fp> |
| 564 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 565 | enable_if_t<is_floating_point_v<_Fp>, _Fp> |
| 566 | midpoint(_Fp __a, _Fp __b) noexcept |
| 567 | { |
| 568 | return isnormal(__a) && isnormal(__b) |
| 569 | && ((__sign(__a) != __sign(__b)) || ((numeric_limits<_Fp>::max() - abs(__a)) < abs(__b))) |
| 570 | ? __a / 2 + __b / 2 |
| 571 | : (__a + __b) / 2; |
| 572 | } |
| 573 | #endif // _LIBCPP_STD_VER > 17 |
Marshall Clow | a5f2c8a | 2019-03-14 16:25:55 +0000 | [diff] [blame] | 574 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 575 | _LIBCPP_END_NAMESPACE_STD |
| 576 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 577 | _LIBCPP_POP_MACROS |
| 578 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 579 | #endif // _LIBCPP_NUMERIC |