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