From 5487694459120480bf95758942d187fb0e7abe34 Mon Sep 17 00:00:00 2001 From: Bernhard Manfred Gruber Date: Wed, 16 Mar 2022 21:13:18 +0100 Subject: [PATCH] use shuffle and reduce --- code/smartPointers/CMakeLists.txt | 3 +++ code/smartPointers/smartPointers.cpp | 2 +- code/smartPointers/solution/smartPointers.sol.cpp | 2 +- code/stl/CMakeLists.txt | 3 +++ code/stl/Makefile | 6 +++--- code/stl/randomize.cpp | 5 ++--- code/stl/solution/randomize.sol.cpp | 9 ++++----- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/code/smartPointers/CMakeLists.txt b/code/smartPointers/CMakeLists.txt index 604df2ed..ff97cfc7 100644 --- a/code/smartPointers/CMakeLists.txt +++ b/code/smartPointers/CMakeLists.txt @@ -3,6 +3,9 @@ cmake_minimum_required( VERSION 3.1 ) project( smartPointers LANGUAGES CXX ) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + # Set up the compilation environment. include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) include( "${CMAKE_CURRENT_SOURCE_DIR}/../SolutionTarget.cmake" ) diff --git a/code/smartPointers/smartPointers.cpp b/code/smartPointers/smartPointers.cpp index fb51bc33..439ef02e 100644 --- a/code/smartPointers/smartPointers.cpp +++ b/code/smartPointers/smartPointers.cpp @@ -34,7 +34,7 @@ double sumEntries(const double* data, std::size_t size) { if (size > 200) throw std::invalid_argument("I only want to sum 200 numbers or less."); - return std::accumulate(data, data + size, 0); + return std::reduce(data, data + size, 0); } // Often, data are owned by one entity, and only used by others. Fix the leak. diff --git a/code/smartPointers/solution/smartPointers.sol.cpp b/code/smartPointers/solution/smartPointers.sol.cpp index 1d14fb6f..912af3c4 100644 --- a/code/smartPointers/solution/smartPointers.sol.cpp +++ b/code/smartPointers/solution/smartPointers.sol.cpp @@ -35,7 +35,7 @@ double sumEntries(const double* data, std::size_t size) { if (size > 200) throw std::invalid_argument("I only want to sum 200 numbers or less."); - return std::accumulate(data, data + size, 0.); + return std::reduce(data, data + size, 0.); } // Often, data are owned by one entity, and only used by others. Fix the leak. diff --git a/code/stl/CMakeLists.txt b/code/stl/CMakeLists.txt index 5ac94b8b..09702ef3 100644 --- a/code/stl/CMakeLists.txt +++ b/code/stl/CMakeLists.txt @@ -3,6 +3,9 @@ cmake_minimum_required( VERSION 3.1 ) project( stl LANGUAGES CXX ) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + # Set up the compilation environment. include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" ) include( "${CMAKE_CURRENT_SOURCE_DIR}/../SolutionTarget.cmake" ) diff --git a/code/stl/Makefile b/code/stl/Makefile index f1897eb4..28431845 100644 --- a/code/stl/Makefile +++ b/code/stl/Makefile @@ -5,10 +5,10 @@ clean: rm -f *o randomize *~ randomize.sol randomize.nostl randomize : randomize.cpp - ${CXX} -g -O0 -Wall -Wextra -L. -o $@ $< + ${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $< randomize.nostl : randomize.nostl.cpp - ${CXX} -g -O0 -Wall -Wextra -L. -o $@ $< + ${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $< randomize.sol : solution/randomize.sol.cpp - ${CXX} -g -O0 -Wall -Wextra -I. -L. -o $@ $< + ${CXX} -std=c++17 -g -O0 -Wall -Wextra -I. -L. -o $@ $< diff --git a/code/stl/randomize.cpp b/code/stl/randomize.cpp index e4799095..59e5678a 100644 --- a/code/stl/randomize.cpp +++ b/code/stl/randomize.cpp @@ -1,12 +1,11 @@ #include -#include #include #include #include +#include #include "Complex.hpp" using namespace std; -using namespace __gnu_cxx; template struct Generator { @@ -20,7 +19,7 @@ void compute(int len, T initial, T step) { // fill and randomize v generate(, , Generator...); - random_shuffle(...); + shuffle(..., std::default_random_engine{}); // compute differences adjacent_difference(...); diff --git a/code/stl/solution/randomize.sol.cpp b/code/stl/solution/randomize.sol.cpp index d0aa97b4..9ad71015 100644 --- a/code/stl/solution/randomize.sol.cpp +++ b/code/stl/solution/randomize.sol.cpp @@ -1,12 +1,11 @@ #include -#include #include #include #include +#include #include "Complex.hpp" using namespace std; -using namespace __gnu_cxx; template struct Generator { @@ -31,14 +30,14 @@ void compute(int len, T initial, T step) { // fill and randomize v generate(v.begin(), v.end(), Generator(initial, step)); - random_shuffle(v.begin(), v.end()); + shuffle(v.begin(), v.end(), std::default_random_engine{}); // compute differences adjacent_difference(v.begin(), v.end(), diffs.begin()); // compute standard deviation of it - T sum = accumulate(diffs.begin()+1, diffs.end(), T()); - T sumsq = accumulate(diffs.begin()+1, diffs.end(), T(), sumsquare()); + T sum = reduce(diffs.begin()+1, diffs.end(), T()); + T sumsq = reduce(diffs.begin()+1, diffs.end(), T(), sumsquare()); T mean = sum/len; T variance = sumsq/len - mean*mean;