numeric 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. // <numeric> -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file include/numeric
  46. * This is a Standard C++ Library header.
  47. */
  48. #ifndef _GLIBCXX_NUMERIC
  49. #define _GLIBCXX_NUMERIC 1
  50. #pragma GCC system_header
  51. #include <bits/c++config.h>
  52. #include <bits/stl_iterator_base_types.h>
  53. #include <bits/stl_numeric.h>
  54. #ifdef _GLIBCXX_PARALLEL
  55. # include <parallel/numeric>
  56. #endif
  57. #if __cplusplus >= 201402L
  58. # include <type_traits>
  59. # include <bit>
  60. # include <ext/numeric_traits.h>
  61. #endif
  62. #if __cplusplus >= 201703L
  63. # include <bits/stl_function.h>
  64. #endif
  65. #if __cplusplus > 201703L
  66. # include <limits>
  67. #endif
  68. /**
  69. * @defgroup numerics Numerics
  70. *
  71. * Components for performing numeric operations. Includes support for
  72. * complex number types, random number generation, numeric (n-at-a-time)
  73. * arrays, generalized numeric algorithms, and mathematical special functions.
  74. */
  75. namespace std _GLIBCXX_VISIBILITY(default)
  76. {
  77. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  78. #if __cplusplus >= 201402L
  79. namespace __detail
  80. {
  81. // Like std::abs, but supports unsigned types and returns the specified type,
  82. // so |std::numeric_limits<_Tp>::min()| is OK if representable in _Res.
  83. template<typename _Res, typename _Tp>
  84. constexpr _Res
  85. __abs_r(_Tp __val)
  86. {
  87. static_assert(sizeof(_Res) >= sizeof(_Tp),
  88. "result type must be at least as wide as the input type");
  89. if (__val >= 0)
  90. return __val;
  91. #ifdef _GLIBCXX_ASSERTIONS
  92. if (!__is_constant_evaluated()) // overflow already detected in constexpr
  93. __glibcxx_assert(__val != __gnu_cxx::__int_traits<_Res>::__min);
  94. #endif
  95. return -static_cast<_Res>(__val);
  96. }
  97. template<typename> void __abs_r(bool) = delete;
  98. // GCD implementation, using Stein's algorithm
  99. template<typename _Tp>
  100. constexpr _Tp
  101. __gcd(_Tp __m, _Tp __n)
  102. {
  103. static_assert(is_unsigned<_Tp>::value, "type must be unsigned");
  104. if (__m == 0)
  105. return __n;
  106. if (__n == 0)
  107. return __m;
  108. const int __i = std::__countr_zero(__m);
  109. __m >>= __i;
  110. const int __j = std::__countr_zero(__n);
  111. __n >>= __j;
  112. const int __k = __i < __j ? __i : __j; // min(i, j)
  113. while (true)
  114. {
  115. if (__m > __n)
  116. {
  117. _Tp __tmp = __m;
  118. __m = __n;
  119. __n = __tmp;
  120. }
  121. __n -= __m;
  122. if (__n == 0)
  123. return __m << __k;
  124. __n >>= std::__countr_zero(__n);
  125. }
  126. }
  127. } // namespace __detail
  128. #if __cplusplus >= 201703L
  129. #define __cpp_lib_gcd_lcm 201606L
  130. // These were used in drafts of SD-6:
  131. #define __cpp_lib_gcd 201606L
  132. #define __cpp_lib_lcm 201606L
  133. /// Greatest common divisor
  134. template<typename _Mn, typename _Nn>
  135. constexpr common_type_t<_Mn, _Nn>
  136. gcd(_Mn __m, _Nn __n) noexcept
  137. {
  138. static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
  139. "std::gcd arguments must be integers");
  140. static_assert(_Mn(2) == 2 && _Nn(2) == 2,
  141. "std::gcd arguments must not be bool");
  142. using _Ct = common_type_t<_Mn, _Nn>;
  143. const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
  144. const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
  145. return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
  146. }
  147. /// Least common multiple
  148. template<typename _Mn, typename _Nn>
  149. constexpr common_type_t<_Mn, _Nn>
  150. lcm(_Mn __m, _Nn __n) noexcept
  151. {
  152. static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
  153. "std::lcm arguments must be integers");
  154. static_assert(_Mn(2) == 2 && _Nn(2) == 2,
  155. "std::lcm arguments must not be bool");
  156. using _Ct = common_type_t<_Mn, _Nn>;
  157. const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
  158. const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
  159. if (__m2 == 0 || __n2 == 0)
  160. return 0;
  161. _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
  162. if constexpr (is_signed_v<_Ct>)
  163. if (__is_constant_evaluated())
  164. return __r * __n2; // constant evaluation can detect overflow here.
  165. bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
  166. __glibcxx_assert(!__overflow);
  167. return __r;
  168. }
  169. #endif // C++17
  170. #endif // C++14
  171. #if __cplusplus > 201703L
  172. // midpoint
  173. # define __cpp_lib_interpolate 201902L
  174. template<typename _Tp>
  175. constexpr
  176. enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
  177. __not_<is_same<_Tp, bool>>>,
  178. _Tp>
  179. midpoint(_Tp __a, _Tp __b) noexcept
  180. {
  181. if constexpr (is_integral_v<_Tp>)
  182. {
  183. using _Up = make_unsigned_t<_Tp>;
  184. int __k = 1;
  185. _Up __m = __a;
  186. _Up __M = __b;
  187. if (__a > __b)
  188. {
  189. __k = -1;
  190. __m = __b;
  191. __M = __a;
  192. }
  193. return __a + __k * _Tp(_Up(__M - __m) / 2);
  194. }
  195. else // is_floating
  196. {
  197. constexpr _Tp __lo = numeric_limits<_Tp>::min() * 2;
  198. constexpr _Tp __hi = numeric_limits<_Tp>::max() / 2;
  199. const _Tp __abs_a = __a < 0 ? -__a : __a;
  200. const _Tp __abs_b = __b < 0 ? -__b : __b;
  201. if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
  202. return (__a + __b) / 2; // always correctly rounded
  203. if (__abs_a < __lo) // not safe to halve __a
  204. return __a + __b/2;
  205. if (__abs_b < __lo) // not safe to halve __b
  206. return __a/2 + __b;
  207. return __a/2 + __b/2; // otherwise correctly rounded
  208. }
  209. }
  210. template<typename _Tp>
  211. constexpr enable_if_t<is_object_v<_Tp>, _Tp*>
  212. midpoint(_Tp* __a, _Tp* __b) noexcept
  213. {
  214. static_assert( sizeof(_Tp) != 0, "type must be complete" );
  215. return __a + (__b - __a) / 2;
  216. }
  217. #endif // C++20
  218. #if __cplusplus >= 201703L
  219. #if __cplusplus > 201703L
  220. #define __cpp_lib_constexpr_numeric 201911L
  221. #endif
  222. /// @addtogroup numeric_ops
  223. /// @{
  224. /**
  225. * @brief Calculate reduction of values in a range.
  226. *
  227. * @param __first Start of range.
  228. * @param __last End of range.
  229. * @param __init Starting value to add other values to.
  230. * @param __binary_op A binary function object.
  231. * @return The final sum.
  232. *
  233. * Reduce the values in the range `[first,last)` using a binary operation.
  234. * The initial value is `init`. The values are not necessarily processed
  235. * in order.
  236. *
  237. * This algorithm is similar to `std::accumulate` but is not required to
  238. * perform the operations in order from first to last. For operations
  239. * that are commutative and associative the result will be the same as
  240. * for `std::accumulate`, but for other operations (such as floating point
  241. * arithmetic) the result can be different.
  242. */
  243. template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
  244. _GLIBCXX20_CONSTEXPR
  245. _Tp
  246. reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
  247. _BinaryOperation __binary_op)
  248. {
  249. using __ref = typename iterator_traits<_InputIterator>::reference;
  250. static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
  251. static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
  252. static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
  253. static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
  254. if constexpr (__is_random_access_iter<_InputIterator>::value)
  255. {
  256. while ((__last - __first) >= 4)
  257. {
  258. _Tp __v1 = __binary_op(__first[0], __first[1]);
  259. _Tp __v2 = __binary_op(__first[2], __first[3]);
  260. _Tp __v3 = __binary_op(__v1, __v2);
  261. __init = __binary_op(__init, __v3);
  262. __first += 4;
  263. }
  264. }
  265. for (; __first != __last; ++__first)
  266. __init = __binary_op(__init, *__first);
  267. return __init;
  268. }
  269. /**
  270. * @brief Calculate reduction of values in a range.
  271. *
  272. * @param __first Start of range.
  273. * @param __last End of range.
  274. * @param __init Starting value to add other values to.
  275. * @return The final sum.
  276. *
  277. * Reduce the values in the range `[first,last)` using addition.
  278. * Equivalent to calling `std::reduce(first, last, init, std::plus<>())`.
  279. */
  280. template<typename _InputIterator, typename _Tp>
  281. _GLIBCXX20_CONSTEXPR
  282. inline _Tp
  283. reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
  284. { return std::reduce(__first, __last, std::move(__init), plus<>()); }
  285. /**
  286. * @brief Calculate reduction of values in a range.
  287. *
  288. * @param __first Start of range.
  289. * @param __last End of range.
  290. * @return The final sum.
  291. *
  292. * Reduce the values in the range `[first,last)` using addition, with
  293. * an initial value of `T{}`, where `T` is the iterator's value type.
  294. * Equivalent to calling `std::reduce(first, last, T{}, std::plus<>())`.
  295. */
  296. template<typename _InputIterator>
  297. _GLIBCXX20_CONSTEXPR
  298. inline typename iterator_traits<_InputIterator>::value_type
  299. reduce(_InputIterator __first, _InputIterator __last)
  300. {
  301. using value_type = typename iterator_traits<_InputIterator>::value_type;
  302. return std::reduce(__first, __last, value_type{}, plus<>());
  303. }
  304. /**
  305. * @brief Combine elements from two ranges and reduce
  306. *
  307. * @param __first1 Start of first range.
  308. * @param __last1 End of first range.
  309. * @param __first2 Start of second range.
  310. * @param __init Starting value to add other values to.
  311. * @param __binary_op1 The function used to perform reduction.
  312. * @param __binary_op2 The function used to combine values from the ranges.
  313. * @return The final sum.
  314. *
  315. * Call `binary_op2(first1[n],first2[n])` for each `n` in `[0,last1-first1)`
  316. * and then use `binary_op1` to reduce the values returned by `binary_op2`
  317. * to a single value of type `T`.
  318. *
  319. * The range beginning at `first2` must contain at least `last1-first1`
  320. * elements.
  321. */
  322. template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
  323. typename _BinaryOperation1, typename _BinaryOperation2>
  324. _GLIBCXX20_CONSTEXPR
  325. _Tp
  326. transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
  327. _InputIterator2 __first2, _Tp __init,
  328. _BinaryOperation1 __binary_op1,
  329. _BinaryOperation2 __binary_op2)
  330. {
  331. if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
  332. __is_random_access_iter<_InputIterator2>>)
  333. {
  334. while ((__last1 - __first1) >= 4)
  335. {
  336. _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
  337. __binary_op2(__first1[1], __first2[1]));
  338. _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
  339. __binary_op2(__first1[3], __first2[3]));
  340. _Tp __v3 = __binary_op1(__v1, __v2);
  341. __init = __binary_op1(__init, __v3);
  342. __first1 += 4;
  343. __first2 += 4;
  344. }
  345. }
  346. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  347. __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
  348. return __init;
  349. }
  350. /**
  351. * @brief Combine elements from two ranges and reduce
  352. *
  353. * @param __first1 Start of first range.
  354. * @param __last1 End of first range.
  355. * @param __first2 Start of second range.
  356. * @param __init Starting value to add other values to.
  357. * @return The final sum.
  358. *
  359. * Call `first1[n]*first2[n]` for each `n` in `[0,last1-first1)` and then
  360. * use addition to sum those products to a single value of type `T`.
  361. *
  362. * The range beginning at `first2` must contain at least `last1-first1`
  363. * elements.
  364. */
  365. template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
  366. _GLIBCXX20_CONSTEXPR
  367. inline _Tp
  368. transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
  369. _InputIterator2 __first2, _Tp __init)
  370. {
  371. return std::transform_reduce(__first1, __last1, __first2,
  372. std::move(__init),
  373. plus<>(), multiplies<>());
  374. }
  375. /**
  376. * @brief Transform the elements of a range and reduce
  377. *
  378. * @param __first Start of range.
  379. * @param __last End of range.
  380. * @param __init Starting value to add other values to.
  381. * @param __binary_op The function used to perform reduction.
  382. * @param __unary_op The function used to transform values from the range.
  383. * @return The final sum.
  384. *
  385. * Call `unary_op(first[n])` for each `n` in `[0,last-first)` and then
  386. * use `binary_op` to reduce the values returned by `unary_op`
  387. * to a single value of type `T`.
  388. */
  389. template<typename _InputIterator, typename _Tp,
  390. typename _BinaryOperation, typename _UnaryOperation>
  391. _GLIBCXX20_CONSTEXPR
  392. _Tp
  393. transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
  394. _BinaryOperation __binary_op, _UnaryOperation __unary_op)
  395. {
  396. if constexpr (__is_random_access_iter<_InputIterator>::value)
  397. {
  398. while ((__last - __first) >= 4)
  399. {
  400. _Tp __v1 = __binary_op(__unary_op(__first[0]),
  401. __unary_op(__first[1]));
  402. _Tp __v2 = __binary_op(__unary_op(__first[2]),
  403. __unary_op(__first[3]));
  404. _Tp __v3 = __binary_op(__v1, __v2);
  405. __init = __binary_op(__init, __v3);
  406. __first += 4;
  407. }
  408. }
  409. for (; __first != __last; ++__first)
  410. __init = __binary_op(__init, __unary_op(*__first));
  411. return __init;
  412. }
  413. /** @brief Output the cumulative sum of one range to a second range
  414. *
  415. * @param __first Start of input range.
  416. * @param __last End of input range.
  417. * @param __result Start of output range.
  418. * @param __init Initial value.
  419. * @param __binary_op Function to perform summation.
  420. * @return The end of the output range.
  421. *
  422. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  423. * to the output range. Each element of the output range contains the
  424. * running total of all earlier elements (and the initial value),
  425. * using `binary_op` for summation.
  426. *
  427. * This function generates an "exclusive" scan, meaning the Nth element
  428. * of the output range is the sum of the first N-1 input elements,
  429. * so the Nth input element is not included.
  430. */
  431. template<typename _InputIterator, typename _OutputIterator, typename _Tp,
  432. typename _BinaryOperation>
  433. _GLIBCXX20_CONSTEXPR
  434. _OutputIterator
  435. exclusive_scan(_InputIterator __first, _InputIterator __last,
  436. _OutputIterator __result, _Tp __init,
  437. _BinaryOperation __binary_op)
  438. {
  439. while (__first != __last)
  440. {
  441. auto __v = __init;
  442. __init = __binary_op(__init, *__first);
  443. ++__first;
  444. *__result++ = std::move(__v);
  445. }
  446. return __result;
  447. }
  448. /** @brief Output the cumulative sum of one range to a second range
  449. *
  450. * @param __first Start of input range.
  451. * @param __last End of input range.
  452. * @param __result Start of output range.
  453. * @param __init Initial value.
  454. * @return The end of the output range.
  455. *
  456. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  457. * to the output range. Each element of the output range contains the
  458. * running total of all earlier elements (and the initial value),
  459. * using `std::plus<>` for summation.
  460. *
  461. * This function generates an "exclusive" scan, meaning the Nth element
  462. * of the output range is the sum of the first N-1 input elements,
  463. * so the Nth input element is not included.
  464. */
  465. template<typename _InputIterator, typename _OutputIterator, typename _Tp>
  466. _GLIBCXX20_CONSTEXPR
  467. inline _OutputIterator
  468. exclusive_scan(_InputIterator __first, _InputIterator __last,
  469. _OutputIterator __result, _Tp __init)
  470. {
  471. return std::exclusive_scan(__first, __last, __result, std::move(__init),
  472. plus<>());
  473. }
  474. /** @brief Output the cumulative sum of one range to a second range
  475. *
  476. * @param __first Start of input range.
  477. * @param __last End of input range.
  478. * @param __result Start of output range.
  479. * @param __binary_op Function to perform summation.
  480. * @param __init Initial value.
  481. * @return The end of the output range.
  482. *
  483. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  484. * to the output range. Each element of the output range contains the
  485. * running total of all earlier elements (and the initial value),
  486. * using `binary_op` for summation.
  487. *
  488. * This function generates an "inclusive" scan, meaning the Nth element
  489. * of the output range is the sum of the first N input elements,
  490. * so the Nth input element is included.
  491. */
  492. template<typename _InputIterator, typename _OutputIterator,
  493. typename _BinaryOperation, typename _Tp>
  494. _GLIBCXX20_CONSTEXPR
  495. _OutputIterator
  496. inclusive_scan(_InputIterator __first, _InputIterator __last,
  497. _OutputIterator __result, _BinaryOperation __binary_op,
  498. _Tp __init)
  499. {
  500. for (; __first != __last; ++__first)
  501. *__result++ = __init = __binary_op(__init, *__first);
  502. return __result;
  503. }
  504. /** @brief Output the cumulative sum of one range to a second range
  505. *
  506. * @param __first Start of input range.
  507. * @param __last End of input range.
  508. * @param __result Start of output range.
  509. * @param __binary_op Function to perform summation.
  510. * @return The end of the output range.
  511. *
  512. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  513. * to the output range. Each element of the output range contains the
  514. * running total of all earlier elements, using `binary_op` for summation.
  515. *
  516. * This function generates an "inclusive" scan, meaning the Nth element
  517. * of the output range is the sum of the first N input elements,
  518. * so the Nth input element is included.
  519. */
  520. template<typename _InputIterator, typename _OutputIterator,
  521. typename _BinaryOperation>
  522. _GLIBCXX20_CONSTEXPR
  523. _OutputIterator
  524. inclusive_scan(_InputIterator __first, _InputIterator __last,
  525. _OutputIterator __result, _BinaryOperation __binary_op)
  526. {
  527. if (__first != __last)
  528. {
  529. auto __init = *__first;
  530. *__result++ = __init;
  531. ++__first;
  532. if (__first != __last)
  533. __result = std::inclusive_scan(__first, __last, __result,
  534. __binary_op, std::move(__init));
  535. }
  536. return __result;
  537. }
  538. /** @brief Output the cumulative sum of one range to a second range
  539. *
  540. * @param __first Start of input range.
  541. * @param __last End of input range.
  542. * @param __result Start of output range.
  543. * @return The end of the output range.
  544. *
  545. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  546. * to the output range. Each element of the output range contains the
  547. * running total of all earlier elements, using `std::plus<>` for summation.
  548. *
  549. * This function generates an "inclusive" scan, meaning the Nth element
  550. * of the output range is the sum of the first N input elements,
  551. * so the Nth input element is included.
  552. */
  553. template<typename _InputIterator, typename _OutputIterator>
  554. _GLIBCXX20_CONSTEXPR
  555. inline _OutputIterator
  556. inclusive_scan(_InputIterator __first, _InputIterator __last,
  557. _OutputIterator __result)
  558. { return std::inclusive_scan(__first, __last, __result, plus<>()); }
  559. /** @brief Output the cumulative sum of one range to a second range
  560. *
  561. * @param __first Start of input range.
  562. * @param __last End of input range.
  563. * @param __result Start of output range.
  564. * @param __init Initial value.
  565. * @param __binary_op Function to perform summation.
  566. * @param __unary_op Function to transform elements of the input range.
  567. * @return The end of the output range.
  568. *
  569. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  570. * to the output range. Each element of the output range contains the
  571. * running total of all earlier elements (and the initial value),
  572. * using `__unary_op` to transform the input elements
  573. * and using `__binary_op` for summation.
  574. *
  575. * This function generates an "exclusive" scan, meaning the Nth element
  576. * of the output range is the sum of the first N-1 input elements,
  577. * so the Nth input element is not included.
  578. */
  579. template<typename _InputIterator, typename _OutputIterator, typename _Tp,
  580. typename _BinaryOperation, typename _UnaryOperation>
  581. _GLIBCXX20_CONSTEXPR
  582. _OutputIterator
  583. transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
  584. _OutputIterator __result, _Tp __init,
  585. _BinaryOperation __binary_op,
  586. _UnaryOperation __unary_op)
  587. {
  588. while (__first != __last)
  589. {
  590. auto __v = __init;
  591. __init = __binary_op(__init, __unary_op(*__first));
  592. ++__first;
  593. *__result++ = std::move(__v);
  594. }
  595. return __result;
  596. }
  597. /** @brief Output the cumulative sum of one range to a second range
  598. *
  599. * @param __first Start of input range.
  600. * @param __last End of input range.
  601. * @param __result Start of output range.
  602. * @param __binary_op Function to perform summation.
  603. * @param __unary_op Function to transform elements of the input range.
  604. * @param __init Initial value.
  605. * @return The end of the output range.
  606. *
  607. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  608. * to the output range. Each element of the output range contains the
  609. * running total of all earlier elements (and the initial value),
  610. * using `__unary_op` to transform the input elements
  611. * and using `__binary_op` for summation.
  612. *
  613. * This function generates an "inclusive" scan, meaning the Nth element
  614. * of the output range is the sum of the first N input elements,
  615. * so the Nth input element is included.
  616. */
  617. template<typename _InputIterator, typename _OutputIterator,
  618. typename _BinaryOperation, typename _UnaryOperation, typename _Tp>
  619. _GLIBCXX20_CONSTEXPR
  620. _OutputIterator
  621. transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
  622. _OutputIterator __result,
  623. _BinaryOperation __binary_op,
  624. _UnaryOperation __unary_op,
  625. _Tp __init)
  626. {
  627. for (; __first != __last; ++__first)
  628. *__result++ = __init = __binary_op(__init, __unary_op(*__first));
  629. return __result;
  630. }
  631. /** @brief Output the cumulative sum of one range to a second range
  632. *
  633. * @param __first Start of input range.
  634. * @param __last End of input range.
  635. * @param __result Start of output range.
  636. * @param __binary_op Function to perform summation.
  637. * @param __unary_op Function to transform elements of the input range.
  638. * @return The end of the output range.
  639. *
  640. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  641. * to the output range. Each element of the output range contains the
  642. * running total of all earlier elements,
  643. * using `__unary_op` to transform the input elements
  644. * and using `__binary_op` for summation.
  645. *
  646. * This function generates an "inclusive" scan, meaning the Nth element
  647. * of the output range is the sum of the first N input elements,
  648. * so the Nth input element is included.
  649. */
  650. template<typename _InputIterator, typename _OutputIterator,
  651. typename _BinaryOperation, typename _UnaryOperation>
  652. _GLIBCXX20_CONSTEXPR
  653. _OutputIterator
  654. transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
  655. _OutputIterator __result,
  656. _BinaryOperation __binary_op,
  657. _UnaryOperation __unary_op)
  658. {
  659. if (__first != __last)
  660. {
  661. auto __init = __unary_op(*__first);
  662. *__result++ = __init;
  663. ++__first;
  664. if (__first != __last)
  665. __result = std::transform_inclusive_scan(__first, __last, __result,
  666. __binary_op, __unary_op,
  667. std::move(__init));
  668. }
  669. return __result;
  670. }
  671. /// @} group numeric_ops
  672. #endif // C++17
  673. _GLIBCXX_END_NAMESPACE_VERSION
  674. } // namespace std
  675. #if __cplusplus >= 201703L
  676. // Parallel STL algorithms
  677. # if _PSTL_EXECUTION_POLICIES_DEFINED
  678. // If <execution> has already been included, pull in implementations
  679. # include <pstl/glue_numeric_impl.h>
  680. # else
  681. // Otherwise just pull in forward declarations
  682. # include <pstl/glue_numeric_defs.h>
  683. # define _PSTL_NUMERIC_FORWARD_DECLARED 1
  684. # endif
  685. // Feature test macro for parallel algorithms
  686. # define __cpp_lib_parallel_algorithm 201603L
  687. #endif // C++17
  688. #endif /* _GLIBCXX_NUMERIC */