Skip to content

Commit

Permalink
Merge pull request Bhupesh-V#524 from claudioMontanari/feature/inclus…
Browse files Browse the repository at this point in the history
…ive_scan

Feature/inclusive scan
  • Loading branch information
Bhupesh-V authored Oct 10, 2020
2 parents b484014 + 474a67c commit 44b50df
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
37 changes: 37 additions & 0 deletions algorithm/inclusive_scan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# inclusive_scan

**Description :** Inclusive Scan computes a binary associative operation (`std::plus<>()` by default) on the elements from `first` (included) to `last` (excluded) and writes the results in the range starting from `d_first`; if provided an initial value will be used. Available in C++17 and above.

**Example** :

```cpp
// Compute prefix index sum of all the values in v
std::cout << "\n\nPrefix index sum of v: ";
std::inclusive_scan(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout, " "));

// Compute prefix index sum of all the values in v with an initial value of 5
std::cout << "\n\nPrefix index sum of v (with initial value 5): ";
std::inclusive_scan(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout, " "),
std::plus<>{},
5);

// Compute prefix index sum of the first 3 values in v
std::cout << "\n\nPrefix index sum for first 3 elements of v: ";
std::inclusive_scan(v.begin(),
v.begin() + 3,
std::ostream_iterator<int>(std::cout, " "));

// Compute prefix index product of all the values in v
std::cout << "\n\nPrefix index product of v: ";
std::inclusive_scan(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout, " "),
std::multiplies<>{},
1);
```
**[See Sample code](../snippets/algorithm/inclusive_scan.cpp)**<br>
**[Run Code](https://rextester.com/UVR33146)**
51 changes: 51 additions & 0 deletions snippets/algorithm/inclusive_scan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Author : Claudio Montanari
Date : 26/06/1995
Time : 15:30
Description : Computes inclusive prefix binary operation on a range (sum by default).
*/

#include <iostream>
#include <vector>
#include <numeric>
#include <iterator>

int main() {
std::vector<int> v{ 1, 2, 3, 4, 3, 10 };

std::cout << "Content of v: ";
for (auto i : v) {
std::cout << i << " ";
}

// Compute prefix index sum of all the values in v
std::cout << "\n\nPrefix index sum of v: ";
std::inclusive_scan(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout, " "));

// Compute prefix index sum of all the values in v with an initial value of 5
std::cout << "\n\nPrefix index sum of v (with initial value 5): ";
std::inclusive_scan(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout, " "),
std::plus<>{},
5);

// Compute prefix index sum of the first 3 values in v
std::cout << "\n\nPrefix index sum for first 3 elements of v: ";
std::inclusive_scan(v.begin(),
v.begin() + 3,
std::ostream_iterator<int>(std::cout, " "));

// Compute prefix index product of all the values in v
std::cout << "\n\nPrefix index product of v: ";
std::inclusive_scan(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout, " "),
std::multiplies<>{},
1);

std::cout << std::endl;
}

0 comments on commit 44b50df

Please sign in to comment.