In this post, we are going to explore the std::transforminclusivescan function, including its syntax, parameters, usage examples, and advantages.
What is the std::transform_inclusive_scan function?
Except for a unary operation applied initially to each input element, the transforminclusivescan function is an intrinsic C++ feature that behaves much like inclusive_scan.
Its purpose is to apply unaryop to alter each element from the initial one to the final one. Following this, binaryop is applied within a defined range to perform an inclusive prefix sum operation. The summation process for the i-th element encompasses the inclusive range specified for the i-th input element.
Syntax:
It has the following syntax:
template < class InputItrator, class OutputItrator,
class BinaryOperation, class UnaryOperation >
OutputItrator transform_inclusive_scan( InputItrator first,
InputItrator last,
OutputItrator d_first,
BinaryOperation binary_op,
UnaryOperation unary_op
);
Parameters:
- First and last: The range of an items sum is defined by the beginning and last elements.
- d_first: Here, the destination range starts.
- Unary_op: Every element in the input range will undergo the specified operation.
- Binary_op: This operation is to be applied to the outcomes of other binary and unary operations and check whether or not init(initial val) will be supplied.
- InputItrator: The Inputiterator class is one type of iterator that can read from the pointed-to element. Once it is increased, the validity of the single-pass procedure is lost for all subsequent copies.
- OutputItrator: An iterator that can be written to the indicated element is the OutputIterator.
Type requirements:
Example:
Let's consider a scenario to demonstrate the std::transforminclusivescan function in C++.
#include <iostream>
#include <vector>
using namespace std;
namespace jtpInputIterator {
template <class InputItrator, class OutputItrator,
class BinaryOperation, class UnaryOperation>
OutputItrator transform_inclusive_scan(InputItrator first,InputItrator last,OutputItrator d_first,BinaryOperation binary_op,UnaryOperation unary_op)
{
*d_first = unary_op(*first);
first++;
d_first++;
for (auto it = first; it != last; it++) {
*d_first = binary_op(unary_op(*it), *(d_first - 1));
d_first++;
}
return d_first;
}
}
int main()
{
vector<int> InputVector{ 11, 22, 33, 44, 55, 66, 77, 88 };
vector<int> OutputVector(8);
jtpInputIterator::transform_inclusive_scan(InputVector.begin(),
InputVector.end(), OutputVector.begin(),
[](auto xx, auto yy) {
return xx + yy;
},
[](auto xx) {
return xx * xx;
});
for (auto item : OutputVector) {
cout << item << " ";
}
cout << std::endl;
return 0;
}
Output:
Benefits of transform_inclusive_scan:
A method named std::transforminclusivescan was introduced in the C++17 header. This function performs a parallel inclusive scan on the input sequence using a binary associative operation specified by the provided binary functor. During the scan, each element in the sequence, along with the current element, undergoes the binary operation, resulting in the computation and storage of partial results in the output sequence.
The following are some advantages of C++'s std::transforminclusivescan:
- Parallel Execution: The ability to use std::transforminclusivescan to carry out the operation in parallel is one of its main advantages. Using several cores or threads for computing might result in noticeable performance gains, particularly for large datasets.
- Functional Programming Style: It enables us to describe the binary operation as a functor in a functional programming style. Keeping the operation and scanning logic separate encourages code clarity and reusability.
- Flexibility: Any associative function can be used as the binary operation, allowing for a wide range of computations to be done. It covers both user-defined custom operations and standard arithmetic operations like addition and multiplication.
- Inclusive Scan: Exclusive scan excludes the present element from the partial findings, whereas inclusive scan includes it. It is helpful for many algorithms that require us to collect values, including the one we are now using.
- Combining Standard Library with Integration: Since std::transforminclusivescan is a component of the C++ standard library, it functions well with other standard library data structures and algorithms. By doing this, compatibility and interoperability with current codebases are guaranteed.
- Performance Optimization: Standard library implementations of algorithms frequently include performance optimizations adapted to various hardware architectures and usage patterns. These optimizations can be utilized using std::transforminclusivescan, leading to effective execution.
Conclusion:
In summary, the std::transforminclusivescan function provides numerous benefits in terms of performance, adaptability, and code clarity by providing a straightforward and efficient approach to performing parallel inclusive scans on sequences in C++.