From 8e5cbe91a91835e96b200fc02005ef2e5b28329c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 3 Jul 2026 13:15:15 +0100 Subject: [PATCH 1/5] Use std::ranges::find instead of std::find --- include/bout/hypre_interface.hxx | 4 ++-- include/bout/multiostream.hxx | 4 ++-- include/bout/petsc_interface.hxx | 2 +- include/bout/solver.hxx | 2 +- src/sys/options.cxx | 3 +-- tests/unit/solver/test_solverfactory.cxx | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/bout/hypre_interface.hxx b/include/bout/hypre_interface.hxx index 189e91d159..527a5e2ee4 100644 --- a/include/bout/hypre_interface.hxx +++ b/include/bout/hypre_interface.hxx @@ -458,9 +458,9 @@ public: Element& operator+=(BoutReal value_) { ASSERT3(finite(value_)); - auto column_position = std::find(cbegin(positions), cend(positions), column); + auto column_position = std::ranges::find(positions, column); if (column_position != cend(positions)) { - const auto i = std::distance(cbegin(positions), column_position); + const auto i = std::distance(begin(positions), column_position); value += weights[i] * value_; } addValues(value_); diff --git a/include/bout/multiostream.hxx b/include/bout/multiostream.hxx index ca3cc2d0c7..9caceb382d 100644 --- a/include/bout/multiostream.hxx +++ b/include/bout/multiostream.hxx @@ -17,7 +17,7 @@ private: public: void add(std::basic_ostream& str) { - auto pos = std::find(streams_.begin(), streams_.end(), &str); + auto pos = std::ranges::find(streams_, &str); // Already been added if (pos != streams_.end()) { @@ -28,7 +28,7 @@ public: } void remove(std::basic_ostream& str) { - auto pos = std::find(streams_.begin(), streams_.end(), &str); + auto pos = std::ranges::find(streams_, &str); if (pos != streams_.end()) { streams_.erase(pos); diff --git a/include/bout/petsc_interface.hxx b/include/bout/petsc_interface.hxx index 3ef3a2a49a..92a5519fa1 100644 --- a/include/bout/petsc_interface.hxx +++ b/include/bout/petsc_interface.hxx @@ -406,7 +406,7 @@ public: Element& operator+=(BoutReal val) { ASSERT3(std::isfinite(val)); - auto columnPosition = std::find(positions.begin(), positions.end(), petscCol); + auto columnPosition = std::ranges::find(positions, petscCol); if (columnPosition != positions.end()) { const int index = std::distance(positions.begin(), columnPosition); value += weights[index] * val; diff --git a/include/bout/solver.hxx b/include/bout/solver.hxx index 09ede6a32b..18e1a6de59 100644 --- a/include/bout/solver.hxx +++ b/include/bout/solver.hxx @@ -514,7 +514,7 @@ protected: /// Does \p vars contain a field with \p name? template bool contains(const std::vector>& vars, const std::string& name) { - const auto in_vars = std::find(begin(vars), end(vars), name); + const auto in_vars = std::ranges::find(vars, name); return in_vars != end(vars); } diff --git a/src/sys/options.cxx b/src/sys/options.cxx index 85cfa7a49a..fa41bbf6a3 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -877,8 +877,7 @@ Options Options::getUnused(const std::vector& exclude_sources) cons return false; } const auto source = option.attributes.at("source").as(); - return std::find(exclude_sources.begin(), exclude_sources.end(), source) - != exclude_sources.end(); + return std::ranges::find(exclude_sources, source) != std::ranges::end(exclude_sources); }; const auto conditionally_used = [](const Options& option) -> bool { diff --git a/tests/unit/solver/test_solverfactory.cxx b/tests/unit/solver/test_solverfactory.cxx index 577c2986fe..2168aebc4c 100644 --- a/tests/unit/solver/test_solverfactory.cxx +++ b/tests/unit/solver/test_solverfactory.cxx @@ -17,7 +17,7 @@ TEST(SolverFactoryTest, GetDefaultSolverType) { TEST(SolverFactoryTest, RegisterSolver) { auto available = SolverFactory::getInstance().listAvailable(); - auto found_fake = std::find(begin(available), end(available), "fake_solver"); + auto found_fake = std::ranges::find(available, "fake_solver"); EXPECT_NE(found_fake, end(available)); } From 2bbe21ce8d5c8cd3303b908eb0ac4b1a8f30c438 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 2 Jul 2026 12:21:38 +0100 Subject: [PATCH 2/5] Fix ranges::find with projection Extract `VarStr::name` to compare to `name` --- include/bout/solver.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/solver.hxx b/include/bout/solver.hxx index 18e1a6de59..97e07ee5ff 100644 --- a/include/bout/solver.hxx +++ b/include/bout/solver.hxx @@ -514,7 +514,7 @@ protected: /// Does \p vars contain a field with \p name? template bool contains(const std::vector>& vars, const std::string& name) { - const auto in_vars = std::ranges::find(vars, name); + const auto in_vars = std::ranges::find(vars, name, &VarStr::name); return in_vars != end(vars); } From 8eeb8c6031fdccb393cbc0ebd12c79496b25eac6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 2 Jul 2026 12:33:39 +0100 Subject: [PATCH 3/5] Use std::ranges::end for consistency (and slightly safer) --- include/bout/solver.hxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/bout/solver.hxx b/include/bout/solver.hxx index 97e07ee5ff..b081b8bfd0 100644 --- a/include/bout/solver.hxx +++ b/include/bout/solver.hxx @@ -514,8 +514,7 @@ protected: /// Does \p vars contain a field with \p name? template bool contains(const std::vector>& vars, const std::string& name) { - const auto in_vars = std::ranges::find(vars, name, &VarStr::name); - return in_vars != end(vars); + return std::ranges::find(vars, name, &VarStr::name) != std::ranges::end(vars); } /// Vectors of variables to evolve From bc4ed7fbf3f5ef7c13d557524753fa81337c46ca Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 3 Jul 2026 13:47:07 +0100 Subject: [PATCH 4/5] Use std::ranges library for std::fill and std::transform --- src/sys/options.cxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/sys/options.cxx b/src/sys/options.cxx index fa41bbf6a3..d659426d0a 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -760,13 +760,13 @@ struct ConvertContainer> { Container operator()(int value) { Container result(similar_to); - std::fill(std::begin(result), std::end(result), static_cast(value)); + std::ranges::fill(result, static_cast(value)); return result; } Container operator()(BoutReal value) { Container result(similar_to); - std::fill(std::begin(result), std::end(result), static_cast(value)); + std::ranges::fill(result, static_cast(value)); return result; } @@ -779,8 +779,8 @@ struct ConvertContainer> { Container result(similar_to); result.reshape(value.shape()); // Resize to shape of input - std::transform(std::begin(value), std::end(value), std::begin(result), - [](const OtherScalar& x) { return static_cast(x); }); + std::ranges::transform(value, std::begin(result), + [](const auto& x) { return static_cast(x); }); return result; } @@ -877,7 +877,8 @@ Options Options::getUnused(const std::vector& exclude_sources) cons return false; } const auto source = option.attributes.at("source").as(); - return std::ranges::find(exclude_sources, source) != std::ranges::end(exclude_sources); + return std::ranges::find(exclude_sources, source) + != std::ranges::end(exclude_sources); }; const auto conditionally_used = [](const Options& option) -> bool { From 2c090f48b8620f226665e84fdc0fd710b394cd09 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 8 Jul 2026 10:46:51 +0100 Subject: [PATCH 5/5] Use `std::ranges::sort` and `std::ranges::unique` to avoid the verbose iterator pairs `begin(), end()` in `makeUnique()`. --- src/field/fieldgroup.cxx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/field/fieldgroup.cxx b/src/field/fieldgroup.cxx index f85cbb6843..f9f166fa32 100644 --- a/src/field/fieldgroup.cxx +++ b/src/field/fieldgroup.cxx @@ -1,5 +1,5 @@ - #include +#include FieldGroup operator+(const FieldGroup& lhs, const FieldGroup& rhs) { return FieldGroup(lhs) += rhs; @@ -7,16 +7,16 @@ FieldGroup operator+(const FieldGroup& lhs, const FieldGroup& rhs) { void FieldGroup::makeUnique() { // Need to sort vector before making unique - std::sort(fvec.begin(), fvec.end()); + std::ranges::sort(fvec); // Remove duplicate entries (doesn't resize vector though) - auto last = std::unique(fvec.begin(), fvec.end()); + auto fvec_dupes = std::ranges::unique(fvec); // Resizes vector to remove memory no longer required - fvec.erase(last, fvec.end()); + fvec.erase(fvec_dupes.begin(), fvec_dupes.end()); // Now do the same for the vector of Field3Ds - std::sort(f3vec.begin(), f3vec.end()); - auto last_f3 = std::unique(f3vec.begin(), f3vec.end()); - f3vec.erase(last_f3, f3vec.end()); + std::ranges::sort(f3vec); + auto f3vec_dupes = std::ranges::unique(f3vec); + f3vec.erase(f3vec_dupes.begin(), f3vec_dupes.end()); }