numeric 25 KB

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