numeric 25 KB

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