libstdc++: Generalized Numeric operations

libstdc++

Generalized Numeric operations
Algorithms
Collaboration diagram for Generalized Numeric operations:

## Functions
template<typename _InputIterator, typename _Tp>
constexpr _Tp | std::accumulate (_InputIterator __first, _InputIterator __last, _Tp __init)
template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
constexpr _Tp | std::accumulate (_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
template<typename _InputIterator, typename _OutputIterator>
constexpr _OutputIterator | std::adjacent_difference (_InputIterator __first, _InputIterator __last, _OutputIterator __result)
template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
constexpr _OutputIterator | std::adjacent_difference (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op)
template<typename _InputIterator, typename _OutputIterator, typename _Tp>
constexpr _OutputIterator | std::exclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init)
template<typename _InputIterator, typename _OutputIterator, typename _Tp, typename _BinaryOperation>
constexpr _OutputIterator | std::exclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOperation __binary_op)
template<typename _InputIterator, typename _OutputIterator>
constexpr _OutputIterator | std::inclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result)
template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
constexpr _OutputIterator | std::inclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op)
template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _Tp>
constexpr _OutputIterator | std::inclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, _Tp __init)
template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
constexpr _Tp | std::inner_product (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
template<typename _InputIterator1, typename _InputIterator2, typename _Tp, typename _BinaryOperation1, typename _BinaryOperation2>
constexpr _Tp | std::inner_product (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
template<typename _ForwardIterator, typename _Tp>
constexpr void | std::iota (_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
template<typename _InputIterator, typename _OutputIterator>
constexpr _OutputIterator | std::partial_sum (_InputIterator __first, _InputIterator __last, _OutputIterator __result)
template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
constexpr _OutputIterator | std::partial_sum (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op)
template<typename _InputIterator>
constexpr iterator_traits< _InputIterator >::value_type | std::reduce (_InputIterator __first, _InputIterator __last)
template<typename _InputIterator, typename _Tp>
constexpr _Tp | std::reduce (_InputIterator __first, _InputIterator __last, _Tp __init)
template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
constexpr _Tp | std::reduce (_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
template<typename _InputIterator, typename _OutputIterator, typename _Tp, typename _BinaryOperation, typename _UnaryOperation>
constexpr _OutputIterator | std::transform_exclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOperation __binary_op, _UnaryOperation __unary_op)
template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _UnaryOperation>
constexpr _OutputIterator | std::transform_inclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op)
template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _UnaryOperation, typename _Tp>
constexpr _OutputIterator | std::transform_inclusive_scan (_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op, _Tp __init)
template<typename _InputIterator, typename _Tp, typename _BinaryOperation, typename _UnaryOperation>
constexpr _Tp | std::transform_reduce (_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op, _UnaryOperation __unary_op)
template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
constexpr _Tp | std::transform_reduce (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
template<typename _InputIterator1, typename _InputIterator2, typename _Tp, typename _BinaryOperation1, typename _BinaryOperation2>
constexpr _Tp | std::transform_reduce (_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)

## Detailed Description

## Function Documentation

## ◆ accumulate() [1/2]

template<typename _InputIterator, typename _Tp>

_Tp std::accumulate | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _Tp | __init )
| inlineconstexpr

Accumulate values in a range.

Accumulates the values in the range [first,last) using operator+(). The initial value is init. The values are processed in order.

Parameters

  __first | Start of range.
  __last | End of range.
  __init | Starting value to add other values to.

Returns
  The final sum.

Definition at line 133 of file stl_numeric.h.

## ◆ accumulate() [2/2]

template<typename _InputIterator, typename _Tp, typename _BinaryOperation>

_Tp std::accumulate | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _Tp | __init,
|  | _BinaryOperation | __binary_op )
| inlineconstexpr

Accumulate values in a range with operation.

Accumulates the values in the range [first,last) using the function object __binary_op. The initial value is __init. The values are processed in order.

Parameters

  __first | Start of range.
  __last | End of range.
  __init | Starting value to add other values to.
  __binary_op | Function object to accumulate with.

Returns
  The final sum.

Definition at line 160 of file stl_numeric.h.

## ◆ adjacent_difference() [1/2]

template<typename _InputIterator, typename _OutputIterator>

_OutputIterator std::adjacent_difference | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result )
| constexpr

Return differences between adjacent values.

Computes the difference between adjacent values in the range [first,last) using operator-() and writes the result to `__result`.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Output sums.

Returns
  Iterator pointing just beyond the values written to result.

Definition at line 336 of file stl_numeric.h.

## ◆ adjacent_difference() [2/2]

template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>

_OutputIterator std::adjacent_difference | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _BinaryOperation | __binary_op )
| constexpr

Return differences between adjacent values.

Computes the difference between adjacent values in the range [__first,__last) using the function object `__binary_op` and writes the result to `__result`.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Output sum.
  __binary_op | Function object.

Returns
  Iterator pointing just beyond the values written to result.

Definition at line 379 of file stl_numeric.h.

## ◆ exclusive_scan() [1/2]

template<typename _InputIterator, typename _OutputIterator, typename _Tp>

_OutputIterator std::exclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _Tp | __init )
| inlineconstexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __init | Initial value.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements (and the initial value), using std::plus<> for summation.

This function generates an "exclusive" scan, meaning the Nth element of the output range is the sum of the first N-1 input elements, so the Nth input element is not included.

Definition at line 523 of file numeric.

References exclusive_scan(), and move().

## ◆ exclusive_scan() [2/2]

template<typename _InputIterator, typename _OutputIterator, typename _Tp, typename _BinaryOperation>

_OutputIterator std::exclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _Tp | __init,
|  | _BinaryOperation | __binary_op )
| constexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __init | Initial value.
  __binary_op | Function to perform summation.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements (and the initial value), using binary_op for summation.

This function generates an "exclusive" scan, meaning the Nth element of the output range is the sum of the first N-1 input elements, so the Nth input element is not included.

Definition at line 489 of file numeric.

References move().

Referenced by exclusive_scan().

## ◆ inclusive_scan() [1/3]

template<typename _InputIterator, typename _OutputIterator>

_OutputIterator std::inclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result )
| inlineconstexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements, using std::plus<> for summation.

This function generates an "inclusive" scan, meaning the Nth element of the output range is the sum of the first N input elements, so the Nth input element is included.

Definition at line 614 of file numeric.

References inclusive_scan().

## ◆ inclusive_scan() [2/3]

template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>

_OutputIterator std::inclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _BinaryOperation | __binary_op )
| constexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __binary_op | Function to perform summation.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements, using binary_op for summation.

This function generates an "inclusive" scan, meaning the Nth element of the output range is the sum of the first N input elements, so the Nth input element is included.

Definition at line 581 of file numeric.

References inclusive_scan(), and move().

## ◆ inclusive_scan() [3/3]

template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _Tp>

_OutputIterator std::inclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _BinaryOperation | __binary_op,
|  | _Tp | __init )
| constexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __binary_op | Function to perform summation.
  __init | Initial value.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements (and the initial value), using binary_op for summation.

This function generates an "inclusive" scan, meaning the Nth element of the output range is the sum of the first N input elements, so the Nth input element is included.

Definition at line 552 of file numeric.

Referenced by inclusive_scan(), and inclusive_scan().

## ◆ inner_product() [1/2]

template<typename _InputIterator1, typename _InputIterator2, typename _Tp>

_Tp std::inner_product | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _Tp | __init )
| inlineconstexpr

Compute inner product of two ranges.

Starting with an initial value of `__init`, multiplies successive elements from the two ranges and adds each product into the accumulated value using operator+(). The values in the ranges are processed in order.

Parameters

  __first1 | Start of range 1.
  __last1 | End of range 1.
  __first2 | Start of range 2.
  __init | Starting value to add other values to.

Returns
  The final inner product.

Definition at line 189 of file stl_numeric.h.

## ◆ inner_product() [2/2]

template<typename _InputIterator1, typename _InputIterator2, typename _Tp, typename _BinaryOperation1, typename _BinaryOperation2>

_Tp std::inner_product | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _Tp | __init,
|  | _BinaryOperation1 | __binary_op1,
|  | _BinaryOperation2 | __binary_op2 )
| inlineconstexpr

Compute inner product of two ranges.

Starting with an initial value of `__init`, applies `__binary_op2` to successive elements from the two ranges and accumulates each result into the accumulated value using `__binary_op1`. The values in the ranges are processed in order.

Parameters

  __first1 | Start of range 1.
  __last1 | End of range 1.
  __first2 | Start of range 2.
  __init | Starting value to add other values to.
  __binary_op1 | Function object to accumulate with.
  __binary_op2 | Function object to apply to pairs of input values.

Returns
  The final inner product.

Definition at line 222 of file stl_numeric.h.

## ◆ iota()

template<typename _ForwardIterator, typename _Tp>

void std::iota | ( | _ForwardIterator | __first,
|  | _ForwardIterator | __last,
|  | _Tp | __value )
| constexpr

Create a range of sequentially increasing values.

For each element in the range ``[first,last) assigns `value` and increments `value` as if by `++value`.

Parameters

  __first | Start of range.
  __last | End of range.
  __value | Starting value.

Definition at line 87 of file stl_numeric.h.

## ◆ partial_sum() [1/2]

template<typename _InputIterator, typename _OutputIterator>

_OutputIterator std::partial_sum | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result )
| constexpr

Return list of partial sums.

Accumulates the values in the range [first,last) using the `+` operator. As each successive input value is added into the total, that partial sum is written to `__result`. Therefore, the first value in `__result` is the first value of the input, the second value in `__result` is the sum of the first and second input values, and so on.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Output sum.

Returns
  Iterator pointing just beyond the values written to __result.

Definition at line 255 of file stl_numeric.h.

Referenced by __gnu_parallel::__parallel_random_shuffle_drs_pu(), and __gnu_parallel::__sequential_random_shuffle().

## ◆ partial_sum() [2/2]

template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>

_OutputIterator std::partial_sum | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _BinaryOperation | __binary_op )
| constexpr

Return list of partial sums.

Accumulates the values in the range [first,last) using `__binary_op`. As each successive input value is added into the total, that partial sum is written to `__result`. Therefore, the first value in `__result` is the first value of the input, the second value in `__result` is the sum of the first and second input values, and so on.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Output sum.
  __binary_op | Function object.

Returns
  Iterator pointing just beyond the values written to __result.

Definition at line 297 of file stl_numeric.h.

## ◆ reduce() [1/3]

template<typename _InputIterator>

iterator_traits< _InputIterator >::value_type std::reduce | ( | _InputIterator | __first,
|  | _InputIterator | __last )
| inlineconstexpr

Calculate reduction of values in a range.

Parameters

  __first | Start of range.
  __last | End of range.

Returns
  The final sum.

Reduce the values in the range [first,last) using addition, with an initial value of T{}, where T is the iterator's value type. Equivalent to calling std::reduce(first, last, T{}, std::plus<>()).

Definition at line 349 of file numeric.

References reduce().

## ◆ reduce() [2/3]

template<typename _InputIterator, typename _Tp>

_Tp std::reduce | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _Tp | __init )
| inlineconstexpr

Calculate reduction of values in a range.

Parameters

  __first | Start of range.
  __last | End of range.
  __init | Starting value to add other values to.

Returns
  The final sum.

Reduce the values in the range [first,last) using addition. Equivalent to calling std::reduce(first, last, init, std::plus<>()).

Definition at line 332 of file numeric.

References move(), and reduce().

## ◆ reduce() [3/3]

template<typename _InputIterator, typename _Tp, typename _BinaryOperation>

_Tp std::reduce | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _Tp | __init,
|  | _BinaryOperation | __binary_op )
| constexpr

Calculate reduction of values in a range.

Parameters

  __first | Start of range.
  __last | End of range.
  __init | Starting value to add other values to.
  __binary_op | A binary function object.

Returns
  The final sum.

Reduce the values in the range [first,last) using a binary operation. The initial value is init. The values are not necessarily processed in order.

This algorithm is similar to std::accumulate but is not required to perform the operations in order from first to last. For operations that are commutative and associative the result will be the same as for std::accumulate, but for other operations (such as floating point arithmetic) the result can be different.

Definition at line 294 of file numeric.

Referenced by reduce(), and reduce().

## ◆ transform_exclusive_scan()

template<typename _InputIterator, typename _OutputIterator, typename _Tp, typename _BinaryOperation, typename _UnaryOperation>

_OutputIterator std::transform_exclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _Tp | __init,
|  | _BinaryOperation | __binary_op,
|  | _UnaryOperation | __unary_op )
| constexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __init | Initial value.
  __binary_op | Function to perform summation.
  __unary_op | Function to transform elements of the input range.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements (and the initial value), using __unary_op to transform the input elements and using __binary_op for summation.

This function generates an "exclusive" scan, meaning the Nth element of the output range is the sum of the first N-1 input elements, so the Nth input element is not included.

Definition at line 642 of file numeric.

References move().

## ◆ transform_inclusive_scan() [1/2]

template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _UnaryOperation>

_OutputIterator std::transform_inclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _BinaryOperation | __binary_op,
|  | _UnaryOperation | __unary_op )
| constexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __binary_op | Function to perform summation.
  __unary_op | Function to transform elements of the input range.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements, using __unary_op to transform the input elements and using __binary_op for summation.

This function generates an "inclusive" scan, meaning the Nth element of the output range is the sum of the first N input elements, so the Nth input element is included.

Definition at line 715 of file numeric.

References move(), and transform_inclusive_scan().

## ◆ transform_inclusive_scan() [2/2]

template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation, typename _UnaryOperation, typename _Tp>

_OutputIterator std::transform_inclusive_scan | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _OutputIterator | __result,
|  | _BinaryOperation | __binary_op,
|  | _UnaryOperation | __unary_op,
|  | _Tp | __init )
| constexpr

Output the cumulative sum of one range to a second range.

Parameters

  __first | Start of input range.
  __last | End of input range.
  __result | Start of output range.
  __binary_op | Function to perform summation.
  __unary_op | Function to transform elements of the input range.
  __init | Initial value.

Returns
  The end of the output range.

Write the cumulative sum (aka prefix sum, aka scan) of the input range to the output range. Each element of the output range contains the running total of all earlier elements (and the initial value), using __unary_op to transform the input elements and using __binary_op for summation.

This function generates an "inclusive" scan, meaning the Nth element of the output range is the sum of the first N input elements, so the Nth input element is included.

Definition at line 681 of file numeric.

Referenced by transform_inclusive_scan().

## ◆ transform_reduce() [1/3]

template<typename _InputIterator, typename _Tp, typename _BinaryOperation, typename _UnaryOperation>

_Tp std::transform_reduce | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | _Tp | __init,
|  | _BinaryOperation | __binary_op,
|  | _UnaryOperation | __unary_op )
| constexpr

Transform the elements of a range and reduce.

Parameters

  __first | Start of range.
  __last | End of range.
  __init | Starting value to add other values to.
  __binary_op | The function used to perform reduction.
  __unary_op | The function used to transform values from the range.

Returns
  The final sum.

Call unary_op(first[n]) for each n in [0,last-first) and then use binary_op to reduce the values returned by unary_op to a single value of type T.

Definition at line 446 of file numeric.

## ◆ transform_reduce() [2/3]

template<typename _InputIterator1, typename _InputIterator2, typename _Tp>

_Tp std::transform_reduce | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _Tp | __init )
| inlineconstexpr

Combine elements from two ranges and reduce.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __init | Starting value to add other values to.

Returns
  The final sum.

Call first1[n]*first2[n] for each n in [0,last1-first1) and then use addition to sum those products to a single value of type T.

The range beginning at first2 must contain at least last1-first1 elements.

Definition at line 420 of file numeric.

References move(), and transform_reduce().

## ◆ transform_reduce() [3/3]

template<typename _InputIterator1, typename _InputIterator2, typename _Tp, typename _BinaryOperation1, typename _BinaryOperation2>

_Tp std::transform_reduce | ( | _InputIterator1 | __first1,
|  | _InputIterator1 | __last1,
|  | _InputIterator2 | __first2,
|  | _Tp | __init,
|  | _BinaryOperation1 | __binary_op1,
|  | _BinaryOperation2 | __binary_op2 )
| constexpr

Combine elements from two ranges and reduce.

Parameters

  __first1 | Start of first range.
  __last1 | End of first range.
  __first2 | Start of second range.
  __init | Starting value to add other values to.
  __binary_op1 | The function used to perform reduction.
  __binary_op2 | The function used to combine values from the ranges.

Returns
  The final sum.

Call binary_op2(first1[n],first2[n]) for each n in [0,last1-first1) and then use binary_op1 to reduce the values returned by binary_op2 to a single value of type T.

The range beginning at first2 must contain at least last1-first1 elements.

Definition at line 377 of file numeric.

Referenced by transform_reduce().