From e030a590a9ac5931f962c33e511f933e2a0751e6 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Fri, 20 Feb 2026 13:27:47 +0100 Subject: [PATCH] replace std::find with std::ranges::find where appropriate the c++-20 constrained algorithm is safer and code is more readable --- opm/simulators/flow/KeywordValidation.hpp | 2 +- opm/simulators/flow/OutputExtractor.hpp | 2 +- opm/simulators/flow/SimulatorSerializer.cpp | 5 ++--- opm/simulators/flow/rescoup/ReservoirCouplingMaster.cpp | 2 +- opm/simulators/linalg/gpubridge/CprCreation.cpp | 2 +- opm/simulators/wells/WellInterfaceGeneric.cpp | 2 +- opm/simulators/wells/WellState.hpp | 3 ++- tests/test_rstconv.cpp | 9 +++------ 8 files changed, 12 insertions(+), 15 deletions(-) diff --git a/opm/simulators/flow/KeywordValidation.hpp b/opm/simulators/flow/KeywordValidation.hpp index 130ff0ef51..196bad627d 100644 --- a/opm/simulators/flow/KeywordValidation.hpp +++ b/opm/simulators/flow/KeywordValidation.hpp @@ -161,7 +161,7 @@ struct SupportedKeywords { bool operator()(const T& value) const { - return std::find(m_allowed_values.begin(), m_allowed_values.end(), value) != m_allowed_values.end(); + return std::ranges::find(m_allowed_values, value) != m_allowed_values.end(); } private: diff --git a/opm/simulators/flow/OutputExtractor.hpp b/opm/simulators/flow/OutputExtractor.hpp index 10b7c36584..34821a4f66 100644 --- a/opm/simulators/flow/OutputExtractor.hpp +++ b/opm/simulators/flow/OutputExtractor.hpp @@ -319,7 +319,7 @@ struct BlockExtractor }, handler); const auto found_handler = - std::find(gen_handlers.begin(), gen_handlers.end(), kw_name); + std::ranges::find(gen_handlers, kw_name); if (found_handler != gen_handlers.end()) { phase = std::distance(gen_handlers.begin(), found_handler) % numPhases; } diff --git a/opm/simulators/flow/SimulatorSerializer.cpp b/opm/simulators/flow/SimulatorSerializer.cpp index 6e6f5bce1f..35130d2892 100644 --- a/opm/simulators/flow/SimulatorSerializer.cpp +++ b/opm/simulators/flow/SimulatorSerializer.cpp @@ -226,9 +226,8 @@ void SimulatorSerializer::checkSerializedCmdLine(const std::string& current, if (!difference.empty()) { std::vector only_stored, only_curr; for (std::size_t i = 0; i < difference.size(); ) { - auto stored_it = std::find(stored_strings.begin(), - stored_strings.end(), difference[i]); - auto pos = difference[i].find_first_of('='); + const auto stored_it = std::ranges::find(stored_strings, difference[i]); + const auto pos = difference[i].find_first_of('='); if (i < difference.size() - 1 && difference[i].compare(0, pos, difference[i+1], 0, pos) == 0) { if (stored_it == stored_strings.end()) { diff --git a/opm/simulators/flow/rescoup/ReservoirCouplingMaster.cpp b/opm/simulators/flow/rescoup/ReservoirCouplingMaster.cpp index b7ba3213ec..a49e6edbfa 100644 --- a/opm/simulators/flow/rescoup/ReservoirCouplingMaster.cpp +++ b/opm/simulators/flow/rescoup/ReservoirCouplingMaster.cpp @@ -135,7 +135,7 @@ int ReservoirCouplingMaster:: getSlaveIdx(const std::string &slave_name) const { - auto it = std::find(this->slave_names_.begin(), this->slave_names_.end(), slave_name); + const auto it = std::ranges::find(this->slave_names_, slave_name); if (it != this->slave_names_.end()) { return std::distance(this->slave_names_.begin(), it); } diff --git a/opm/simulators/linalg/gpubridge/CprCreation.cpp b/opm/simulators/linalg/gpubridge/CprCreation.cpp index 291fdacdca..59432577c8 100644 --- a/opm/simulators/linalg/gpubridge/CprCreation.cpp +++ b/opm/simulators/linalg/gpubridge/CprCreation.cpp @@ -80,7 +80,7 @@ create_preconditioner_amg(BlockedMatrix *mat_) for (int row = 0; row < cprNb; ++row) { int start = mat->rowPointers[row]; int end = mat->rowPointers[row + 1]; - auto candidate = std::find(mat->colIndices + start, mat->colIndices + end, row); + const auto candidate = std::find(mat->colIndices + start, mat->colIndices + end, row); assert(candidate != mat->colIndices + end); diagIndices[0][row] = candidate - mat->colIndices; } diff --git a/opm/simulators/wells/WellInterfaceGeneric.cpp b/opm/simulators/wells/WellInterfaceGeneric.cpp index 8e678f9e73..560aa9f6b4 100644 --- a/opm/simulators/wells/WellInterfaceGeneric.cpp +++ b/opm/simulators/wells/WellInterfaceGeneric.cpp @@ -693,7 +693,7 @@ template void WellInterfaceGeneric::addPerforations(const std::vector& perfs) { for (const auto& perf : perfs) { - auto it = std::find(well_cells_.begin(), well_cells_.end(), perf.cell); + const auto it = std::ranges::find(well_cells_, perf.cell); if (it != this->well_cells_.end()) { // If perforation to cell already exists, just add contribution. const auto ind = std::distance(this->well_cells_.begin(), it); diff --git a/opm/simulators/wells/WellState.hpp b/opm/simulators/wells/WellState.hpp index 3b309ec08a..d389baba3e 100644 --- a/opm/simulators/wells/WellState.hpp +++ b/opm/simulators/wells/WellState.hpp @@ -334,7 +334,8 @@ public: } bool is_permanently_inactive_well(const std::string& wname) const { - return std::find(this->permanently_inactive_well_names_.begin(), this->permanently_inactive_well_names_.end(), wname) != this->permanently_inactive_well_names_.end(); + return std::ranges::find(this->permanently_inactive_well_names_, wname) != + this->permanently_inactive_well_names_.end(); } private: diff --git a/tests/test_rstconv.cpp b/tests/test_rstconv.cpp index f21e3af80a..6df39fa61a 100644 --- a/tests/test_rstconv.cpp +++ b/tests/test_rstconv.cpp @@ -126,7 +126,7 @@ BOOST_AUTO_TEST_CASE(RstConvTest) std::vector& v = max[c]; while (v.size() < sample.N) { int m = dist(mersenne_engine); - if (std::find(v.begin(), v.end(), m) == v.end()) { + if (std::ranges::find(v, m) == v.end()) { v.push_back(m); } } @@ -145,9 +145,7 @@ BOOST_AUTO_TEST_CASE(RstConvTest) for (int i = 0; i < 10; ++i) { for (int c = 0; c < 6; ++c) { if (sample.phase[c] != -1) { - bool inMax = std::find(max[c].begin(), - max[c].end(), - cellMapping[i]) != max[c].end(); + const bool inMax = std::ranges::find(max[c], cellMapping[i]) != max[c].end(); residual[i][sample.phase[c]] = inMax ? 1.0 : 1.0 / (i+2); } } @@ -169,8 +167,7 @@ BOOST_AUTO_TEST_CASE(RstConvTest) for (int i = 0; i < cc.size() * 10; ++i) { for (int c = 0; c < 6; ++c) { if (sample.phase[c] != -1) { - bool inMax = std::find(max[c].begin(), - max[c].end(), i) != max[c].end(); + const bool inMax = std::ranges::find(max[c], i) != max[c].end(); BOOST_CHECK_EQUAL(cnv.getData()[c][i], inMax ? 2 : 0); } }