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