From faa5f55ef2442fb66f9370f52e1b5dc6feb4b9a4 Mon Sep 17 00:00:00 2001 From: Claudio Date: Sat, 26 Sep 2020 10:35:09 -0500 Subject: [PATCH 1/3] Add inclusive_scan description and code snippet --- algorithm/inclusive_scan.md | 39 ++++++++++++++++++++ snippets/algorithm/inclusive_scan.cpp | 51 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 algorithm/inclusive_scan.md create mode 100644 snippets/algorithm/inclusive_scan.cpp diff --git a/algorithm/inclusive_scan.md b/algorithm/inclusive_scan.md new file mode 100644 index 00000000..63ac6244 --- /dev/null +++ b/algorithm/inclusive_scan.md @@ -0,0 +1,39 @@ +# 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(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(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(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(std::cout, " "), + std::multiplies<>{}, + 1); +``` +**[See Sample code](snippets/algorithm/inclusive_scan.cpp)**
+**[Run Code](https://rextester.com/UVR33146)** + + diff --git a/snippets/algorithm/inclusive_scan.cpp b/snippets/algorithm/inclusive_scan.cpp new file mode 100644 index 00000000..c00149e5 --- /dev/null +++ b/snippets/algorithm/inclusive_scan.cpp @@ -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 +#include +#include +#include + +int main() { + std::vector 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(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(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(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(std::cout, " "), + std::multiplies<>{}, + 1); + + std::cout << std::endl; +} + From 16ef29aec08d71eb4e815aee2e420bc6b1040d80 Mon Sep 17 00:00:00 2001 From: Claudio Montanari <10453981@polimi.it> Date: Sat, 26 Sep 2020 10:53:23 -0500 Subject: [PATCH 2/3] Revert "Add inclusive_scan description and code snippet" This reverts commit faa5f55ef2442fb66f9370f52e1b5dc6feb4b9a4. Reverted previous commit to fix author name and email from git configuration. --- algorithm/inclusive_scan.md | 39 -------------------- snippets/algorithm/inclusive_scan.cpp | 51 --------------------------- 2 files changed, 90 deletions(-) delete mode 100644 algorithm/inclusive_scan.md delete mode 100644 snippets/algorithm/inclusive_scan.cpp diff --git a/algorithm/inclusive_scan.md b/algorithm/inclusive_scan.md deleted file mode 100644 index 63ac6244..00000000 --- a/algorithm/inclusive_scan.md +++ /dev/null @@ -1,39 +0,0 @@ -# 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(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(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(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(std::cout, " "), - std::multiplies<>{}, - 1); -``` -**[See Sample code](snippets/algorithm/inclusive_scan.cpp)**
-**[Run Code](https://rextester.com/UVR33146)** - - diff --git a/snippets/algorithm/inclusive_scan.cpp b/snippets/algorithm/inclusive_scan.cpp deleted file mode 100644 index c00149e5..00000000 --- a/snippets/algorithm/inclusive_scan.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - Author : Claudio Montanari - Date : 26/06/1995 - Time : 15:30 - Description : Computes inclusive prefix binary operation on a range (sum by default). -*/ - -#include -#include -#include -#include - -int main() { - std::vector 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(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(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(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(std::cout, " "), - std::multiplies<>{}, - 1); - - std::cout << std::endl; -} - From 474a67c37be40fd89ab9537e5927f38fc261b327 Mon Sep 17 00:00:00 2001 From: Claudio Montanari <10453981@polimi.it> Date: Sat, 26 Sep 2020 11:15:28 -0500 Subject: [PATCH 3/3] Add inclusive_scan description and code snippet --- algorithm/inclusive_scan.md | 37 +++++++++++++++++++ snippets/algorithm/inclusive_scan.cpp | 51 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 algorithm/inclusive_scan.md create mode 100644 snippets/algorithm/inclusive_scan.cpp diff --git a/algorithm/inclusive_scan.md b/algorithm/inclusive_scan.md new file mode 100644 index 00000000..2bf55f95 --- /dev/null +++ b/algorithm/inclusive_scan.md @@ -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(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(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(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(std::cout, " "), + std::multiplies<>{}, + 1); +``` +**[See Sample code](../snippets/algorithm/inclusive_scan.cpp)**
+**[Run Code](https://rextester.com/UVR33146)** diff --git a/snippets/algorithm/inclusive_scan.cpp b/snippets/algorithm/inclusive_scan.cpp new file mode 100644 index 00000000..c00149e5 --- /dev/null +++ b/snippets/algorithm/inclusive_scan.cpp @@ -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 +#include +#include +#include + +int main() { + std::vector 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(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(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(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(std::cout, " "), + std::multiplies<>{}, + 1); + + std::cout << std::endl; +} +