You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

716 lines
25KB

  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 value_type = typename iterator_traits<_InputIterator>::value_type;
  228. static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
  229. static_assert(is_convertible_v<value_type, _Tp>);
  230. if constexpr (__is_random_access_iter<_InputIterator>::value)
  231. {
  232. while ((__last - __first) >= 4)
  233. {
  234. _Tp __v1 = __binary_op(__first[0], __first[1]);
  235. _Tp __v2 = __binary_op(__first[2], __first[3]);
  236. _Tp __v3 = __binary_op(__v1, __v2);
  237. __init = __binary_op(__init, __v3);
  238. __first += 4;
  239. }
  240. }
  241. for (; __first != __last; ++__first)
  242. __init = __binary_op(__init, *__first);
  243. return __init;
  244. }
  245. /**
  246. * @brief Calculate reduction of values in a range.
  247. *
  248. * @param __first Start of range.
  249. * @param __last End of range.
  250. * @param __init Starting value to add other values to.
  251. * @return The final sum.
  252. *
  253. * Reduce the values in the range `[first,last)` using addition.
  254. * Equivalent to calling `std::reduce(first, last, init, std::plus<>())`.
  255. */
  256. template<typename _InputIterator, typename _Tp>
  257. _GLIBCXX20_CONSTEXPR
  258. inline _Tp
  259. reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
  260. { return std::reduce(__first, __last, std::move(__init), plus<>()); }
  261. /**
  262. * @brief Calculate reduction of values in a range.
  263. *
  264. * @param __first Start of range.
  265. * @param __last End of range.
  266. * @return The final sum.
  267. *
  268. * Reduce the values in the range `[first,last)` using addition, with
  269. * an initial value of `T{}`, where `T` is the iterator's value type.
  270. * Equivalent to calling `std::reduce(first, last, T{}, std::plus<>())`.
  271. */
  272. template<typename _InputIterator>
  273. _GLIBCXX20_CONSTEXPR
  274. inline typename iterator_traits<_InputIterator>::value_type
  275. reduce(_InputIterator __first, _InputIterator __last)
  276. {
  277. using value_type = typename iterator_traits<_InputIterator>::value_type;
  278. return std::reduce(__first, __last, value_type{}, plus<>());
  279. }
  280. /**
  281. * @brief Combine elements from two ranges and reduce
  282. *
  283. * @param __first1 Start of first range.
  284. * @param __last1 End of first range.
  285. * @param __first2 Start of second range.
  286. * @param __init Starting value to add other values to.
  287. * @param __binary_op1 The function used to perform reduction.
  288. * @param __binary_op2 The function used to combine values from the ranges.
  289. * @return The final sum.
  290. *
  291. * Call `binary_op2(first1[n],first2[n])` for each `n` in `[0,last1-first1)`
  292. * and then use `binary_op1` to reduce the values returned by `binary_op2`
  293. * to a single value of type `T`.
  294. *
  295. * The range beginning at `first2` must contain at least `last1-first1`
  296. * elements.
  297. */
  298. template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
  299. typename _BinaryOperation1, typename _BinaryOperation2>
  300. _GLIBCXX20_CONSTEXPR
  301. _Tp
  302. transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
  303. _InputIterator2 __first2, _Tp __init,
  304. _BinaryOperation1 __binary_op1,
  305. _BinaryOperation2 __binary_op2)
  306. {
  307. if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
  308. __is_random_access_iter<_InputIterator2>>)
  309. {
  310. while ((__last1 - __first1) >= 4)
  311. {
  312. _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
  313. __binary_op2(__first1[1], __first2[1]));
  314. _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
  315. __binary_op2(__first1[3], __first2[3]));
  316. _Tp __v3 = __binary_op1(__v1, __v2);
  317. __init = __binary_op1(__init, __v3);
  318. __first1 += 4;
  319. __first2 += 4;
  320. }
  321. }
  322. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  323. __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
  324. return __init;
  325. }
  326. /**
  327. * @brief Combine elements from two ranges and reduce
  328. *
  329. * @param __first1 Start of first range.
  330. * @param __last1 End of first range.
  331. * @param __first2 Start of second range.
  332. * @param __init Starting value to add other values to.
  333. * @return The final sum.
  334. *
  335. * Call `first1[n]*first2[n]` for each `n` in `[0,last1-first1)` and then
  336. * use addition to sum those products to a single value of type `T`.
  337. *
  338. * The range beginning at `first2` must contain at least `last1-first1`
  339. * elements.
  340. */
  341. template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
  342. _GLIBCXX20_CONSTEXPR
  343. inline _Tp
  344. transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
  345. _InputIterator2 __first2, _Tp __init)
  346. {
  347. return std::transform_reduce(__first1, __last1, __first2,
  348. std::move(__init),
  349. plus<>(), multiplies<>());
  350. }
  351. /**
  352. * @brief Transform the elements of a range and reduce
  353. *
  354. * @param __first Start of range.
  355. * @param __last End of range.
  356. * @param __init Starting value to add other values to.
  357. * @param __binary_op The function used to perform reduction.
  358. * @param __unary_op The function used to transform values from the range.
  359. * @return The final sum.
  360. *
  361. * Call `unary_op(first[n])` for each `n` in `[0,last-first)` and then
  362. * use `binary_op` to reduce the values returned by `unary_op`
  363. * to a single value of type `T`.
  364. */
  365. template<typename _InputIterator, typename _Tp,
  366. typename _BinaryOperation, typename _UnaryOperation>
  367. _GLIBCXX20_CONSTEXPR
  368. _Tp
  369. transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
  370. _BinaryOperation __binary_op, _UnaryOperation __unary_op)
  371. {
  372. if constexpr (__is_random_access_iter<_InputIterator>::value)
  373. {
  374. while ((__last - __first) >= 4)
  375. {
  376. _Tp __v1 = __binary_op(__unary_op(__first[0]),
  377. __unary_op(__first[1]));
  378. _Tp __v2 = __binary_op(__unary_op(__first[2]),
  379. __unary_op(__first[3]));
  380. _Tp __v3 = __binary_op(__v1, __v2);
  381. __init = __binary_op(__init, __v3);
  382. __first += 4;
  383. }
  384. }
  385. for (; __first != __last; ++__first)
  386. __init = __binary_op(__init, __unary_op(*__first));
  387. return __init;
  388. }
  389. /** @brief Output the cumulative sum of one range to a second range
  390. *
  391. * @param __first Start of input range.
  392. * @param __last End of input range.
  393. * @param __result Start of output range.
  394. * @param __init Initial value.
  395. * @param __binary_op Function to perform summation.
  396. * @return The end of the output range.
  397. *
  398. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  399. * to the output range. Each element of the output range contains the
  400. * running total of all earlier elements (and the initial value),
  401. * using `binary_op` for summation.
  402. *
  403. * This function generates an "exclusive" scan, meaning the Nth element
  404. * of the output range is the sum of the first N-1 input elements,
  405. * so the Nth input element is not included.
  406. */
  407. template<typename _InputIterator, typename _OutputIterator, typename _Tp,
  408. typename _BinaryOperation>
  409. _GLIBCXX20_CONSTEXPR
  410. _OutputIterator
  411. exclusive_scan(_InputIterator __first, _InputIterator __last,
  412. _OutputIterator __result, _Tp __init,
  413. _BinaryOperation __binary_op)
  414. {
  415. while (__first != __last)
  416. {
  417. auto __v = __init;
  418. __init = __binary_op(__init, *__first);
  419. ++__first;
  420. *__result++ = std::move(__v);
  421. }
  422. return __result;
  423. }
  424. /** @brief Output the cumulative sum of one range to a second range
  425. *
  426. * @param __first Start of input range.
  427. * @param __last End of input range.
  428. * @param __result Start of output range.
  429. * @param __init Initial value.
  430. * @return The end of the output range.
  431. *
  432. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  433. * to the output range. Each element of the output range contains the
  434. * running total of all earlier elements (and the initial value),
  435. * using `std::plus<>` for summation.
  436. *
  437. * This function generates an "exclusive" scan, meaning the Nth element
  438. * of the output range is the sum of the first N-1 input elements,
  439. * so the Nth input element is not included.
  440. */
  441. template<typename _InputIterator, typename _OutputIterator, typename _Tp>
  442. _GLIBCXX20_CONSTEXPR
  443. inline _OutputIterator
  444. exclusive_scan(_InputIterator __first, _InputIterator __last,
  445. _OutputIterator __result, _Tp __init)
  446. {
  447. return std::exclusive_scan(__first, __last, __result, std::move(__init),
  448. plus<>());
  449. }
  450. /** @brief Output the cumulative sum of one range to a second range
  451. *
  452. * @param __first Start of input range.
  453. * @param __last End of input range.
  454. * @param __result Start of output range.
  455. * @param __binary_op Function to perform summation.
  456. * @param __init Initial value.
  457. * @return The end of the output range.
  458. *
  459. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  460. * to the output range. Each element of the output range contains the
  461. * running total of all earlier elements (and the initial value),
  462. * using `binary_op` for summation.
  463. *
  464. * This function generates an "inclusive" scan, meaning the Nth element
  465. * of the output range is the sum of the first N input elements,
  466. * so the Nth input element is included.
  467. */
  468. template<typename _InputIterator, typename _OutputIterator,
  469. typename _BinaryOperation, typename _Tp>
  470. _GLIBCXX20_CONSTEXPR
  471. _OutputIterator
  472. inclusive_scan(_InputIterator __first, _InputIterator __last,
  473. _OutputIterator __result, _BinaryOperation __binary_op,
  474. _Tp __init)
  475. {
  476. for (; __first != __last; ++__first)
  477. *__result++ = __init = __binary_op(__init, *__first);
  478. return __result;
  479. }
  480. /** @brief Output the cumulative sum of one range to a second range
  481. *
  482. * @param __first Start of input range.
  483. * @param __last End of input range.
  484. * @param __result Start of output range.
  485. * @param __binary_op Function to perform summation.
  486. * @return The end of the output range.
  487. *
  488. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  489. * to the output range. Each element of the output range contains the
  490. * running total of all earlier elements, using `binary_op` for summation.
  491. *
  492. * This function generates an "inclusive" scan, meaning the Nth element
  493. * of the output range is the sum of the first N input elements,
  494. * so the Nth input element is included.
  495. */
  496. template<typename _InputIterator, typename _OutputIterator,
  497. typename _BinaryOperation>
  498. _GLIBCXX20_CONSTEXPR
  499. _OutputIterator
  500. inclusive_scan(_InputIterator __first, _InputIterator __last,
  501. _OutputIterator __result, _BinaryOperation __binary_op)
  502. {
  503. if (__first != __last)
  504. {
  505. auto __init = *__first;
  506. *__result++ = __init;
  507. ++__first;
  508. if (__first != __last)
  509. __result = std::inclusive_scan(__first, __last, __result,
  510. __binary_op, std::move(__init));
  511. }
  512. return __result;
  513. }
  514. /** @brief Output the cumulative sum of one range to a second range
  515. *
  516. * @param __first Start of input range.
  517. * @param __last End of input range.
  518. * @param __result Start of output range.
  519. * @return The end of the output range.
  520. *
  521. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  522. * to the output range. Each element of the output range contains the
  523. * running total of all earlier elements, using `std::plus<>` for summation.
  524. *
  525. * This function generates an "inclusive" scan, meaning the Nth element
  526. * of the output range is the sum of the first N input elements,
  527. * so the Nth input element is included.
  528. */
  529. template<typename _InputIterator, typename _OutputIterator>
  530. _GLIBCXX20_CONSTEXPR
  531. inline _OutputIterator
  532. inclusive_scan(_InputIterator __first, _InputIterator __last,
  533. _OutputIterator __result)
  534. { return std::inclusive_scan(__first, __last, __result, plus<>()); }
  535. /** @brief Output the cumulative sum of one range to a second range
  536. *
  537. * @param __first Start of input range.
  538. * @param __last End of input range.
  539. * @param __result Start of output range.
  540. * @param __init Initial value.
  541. * @param __binary_op Function to perform summation.
  542. * @param __unary_op Function to transform elements of the input 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 (and the initial value),
  548. * using `__unary_op` to transform the input elements
  549. * and using `__binary_op` for summation.
  550. *
  551. * This function generates an "exclusive" scan, meaning the Nth element
  552. * of the output range is the sum of the first N-1 input elements,
  553. * so the Nth input element is not included.
  554. */
  555. template<typename _InputIterator, typename _OutputIterator, typename _Tp,
  556. typename _BinaryOperation, typename _UnaryOperation>
  557. _GLIBCXX20_CONSTEXPR
  558. _OutputIterator
  559. transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
  560. _OutputIterator __result, _Tp __init,
  561. _BinaryOperation __binary_op,
  562. _UnaryOperation __unary_op)
  563. {
  564. while (__first != __last)
  565. {
  566. auto __v = __init;
  567. __init = __binary_op(__init, __unary_op(*__first));
  568. ++__first;
  569. *__result++ = std::move(__v);
  570. }
  571. return __result;
  572. }
  573. /** @brief Output the cumulative sum of one range to a second range
  574. *
  575. * @param __first Start of input range.
  576. * @param __last End of input range.
  577. * @param __result Start of output range.
  578. * @param __binary_op Function to perform summation.
  579. * @param __unary_op Function to transform elements of the input range.
  580. * @param __init Initial value.
  581. * @return The end of the output range.
  582. *
  583. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  584. * to the output range. Each element of the output range contains the
  585. * running total of all earlier elements (and the initial value),
  586. * using `__unary_op` to transform the input elements
  587. * and using `__binary_op` for summation.
  588. *
  589. * This function generates an "inclusive" scan, meaning the Nth element
  590. * of the output range is the sum of the first N input elements,
  591. * so the Nth input element is included.
  592. */
  593. template<typename _InputIterator, typename _OutputIterator,
  594. typename _BinaryOperation, typename _UnaryOperation, typename _Tp>
  595. _GLIBCXX20_CONSTEXPR
  596. _OutputIterator
  597. transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
  598. _OutputIterator __result,
  599. _BinaryOperation __binary_op,
  600. _UnaryOperation __unary_op,
  601. _Tp __init)
  602. {
  603. for (; __first != __last; ++__first)
  604. *__result++ = __init = __binary_op(__init, __unary_op(*__first));
  605. return __result;
  606. }
  607. /** @brief Output the cumulative sum of one range to a second range
  608. *
  609. * @param __first Start of input range.
  610. * @param __last End of input range.
  611. * @param __result Start of output range.
  612. * @param __binary_op Function to perform summation.
  613. * @param __unary_op Function to transform elements of the input range.
  614. * @return The end of the output range.
  615. *
  616. * Write the cumulative sum (aka prefix sum, aka scan) of the input range
  617. * to the output range. Each element of the output range contains the
  618. * running total of all earlier elements,
  619. * using `__unary_op` to transform the input elements
  620. * and using `__binary_op` for summation.
  621. *
  622. * This function generates an "inclusive" scan, meaning the Nth element
  623. * of the output range is the sum of the first N input elements,
  624. * so the Nth input element is included.
  625. */
  626. template<typename _InputIterator, typename _OutputIterator,
  627. typename _BinaryOperation, typename _UnaryOperation>
  628. _GLIBCXX20_CONSTEXPR
  629. _OutputIterator
  630. transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
  631. _OutputIterator __result,
  632. _BinaryOperation __binary_op,
  633. _UnaryOperation __unary_op)
  634. {
  635. if (__first != __last)
  636. {
  637. auto __init = __unary_op(*__first);
  638. *__result++ = __init;
  639. ++__first;
  640. if (__first != __last)
  641. __result = std::transform_inclusive_scan(__first, __last, __result,
  642. __binary_op, __unary_op,
  643. std::move(__init));
  644. }
  645. return __result;
  646. }
  647. // @} group numeric_ops
  648. _GLIBCXX_END_NAMESPACE_VERSION
  649. } // namespace std
  650. // Parallel STL algorithms
  651. # if _PSTL_EXECUTION_POLICIES_DEFINED
  652. // If <execution> has already been included, pull in implementations
  653. # include <pstl/glue_numeric_impl.h>
  654. # else
  655. // Otherwise just pull in forward declarations
  656. # include <pstl/glue_numeric_defs.h>
  657. # define _PSTL_NUMERIC_FORWARD_DECLARED 1
  658. # endif
  659. // Feature test macro for parallel algorithms
  660. # define __cpp_lib_parallel_algorithm 201603L
  661. #endif // C++17
  662. #endif /* _GLIBCXX_NUMERIC */