numeric 25 KB

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