Skip to content

Commit

Permalink
use std::any_of instead of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
akva2 committed Sep 19, 2024
1 parent 7e32aec commit e9b36bd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 63 deletions.
29 changes: 12 additions & 17 deletions opm/input/eclipse/EclipseState/EndpointScaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <opm/common/utility/String.hpp>

#include <algorithm>
#include <initializer_list>
#include <stdexcept>
#include <string>
Expand All @@ -34,15 +35,12 @@ namespace {
const char* suffix,
const std::initializer_list<std::string>& base)
{
for (const auto& kw : base) {
for (const auto* p : {"", "I"}) {
if (deck.hasKeyword(p + kw + suffix)) {
return true;
}
}
}

return false;
return std::any_of(base.begin(), base.end(),
[&deck, suffix](const auto& kw)
{
return deck.hasKeyword(kw + suffix) ||
deck.hasKeyword("I" + kw + suffix);
});
}

bool hasScaling(const Opm::Deck& deck,
Expand All @@ -51,14 +49,11 @@ namespace {
const auto direction = std::initializer_list<const char*> {
"", "X-", "X", "Y-", "Y", "Z-", "Z"
};

for (const auto* suffix : direction) {
if (hasScaling(deck, suffix, base)) {
return true;
}
}

return false;
return std::any_of(direction.begin(), direction.end(),
[&deck, &base](const auto& suffix)
{
return hasScaling(deck, suffix, base);
});
}

bool hasHorzScaling(const Opm::Deck& deck)
Expand Down
13 changes: 7 additions & 6 deletions opm/input/eclipse/EclipseState/SummaryConfig/SummaryConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,12 +1995,13 @@ const SummaryConfigNode& SummaryConfig::operator[](std::size_t index) const {
}


bool SummaryConfig::match(const std::string& keywordPattern) const {
for (const auto& keyword : this->short_keywords) {
if (shmatch(keywordPattern, keyword))
return true;
}
return false;
bool SummaryConfig::match(const std::string& keywordPattern) const
{
return std::any_of(this->short_keywords.begin(), this->short_keywords.end(),
[&keywordPattern](const auto& keyword)
{
return shmatch(keywordPattern, keyword);
});
}

SummaryConfig::keyword_list
Expand Down
17 changes: 9 additions & 8 deletions opm/input/eclipse/Schedule/Action/Actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>

#include <opm/input/eclipse/Schedule/Action/Actions.hpp>
#include <opm/input/eclipse/Schedule/Action/ActionX.hpp>

#include <algorithm>

namespace Opm {
namespace Action {

Expand Down Expand Up @@ -101,12 +101,13 @@ int Actions::max_input_lines() const {
}


bool Actions::ready(const State& state, std::time_t sim_time) const {
for (const auto& action : this->actions) {
if (action.ready(state, sim_time))
return true;
}
return false;
bool Actions::ready(const State& state, std::time_t sim_time) const
{
return std::any_of(this->actions.begin(), this->actions.end(),
[&state, sim_time](const auto& action)
{
return action.ready(state, sim_time);
});
}

std::vector<const PyAction *> Actions::pending_python(const State& state) const {
Expand Down
17 changes: 11 additions & 6 deletions opm/input/eclipse/Schedule/MSW/Compsegs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cmath>

#include <fmt/format.h>
#include <opm/input/eclipse/Schedule/MSW/Compsegs.hpp>

#include <opm/input/eclipse/Parser/ParserKeywords/C.hpp>
#include <opm/input/eclipse/Parser/ParseContext.hpp>
Expand All @@ -35,7 +33,10 @@
#include <opm/input/eclipse/Schedule/Well/Connection.hpp>
#include <opm/input/eclipse/Schedule/ScheduleGrid.hpp>

#include "Compsegs.hpp"
#include <algorithm>
#include <cmath>

#include <fmt/format.h>

namespace Opm {
namespace Compsegs {
Expand Down Expand Up @@ -336,8 +337,12 @@ namespace {
}
}

for (const auto& connection : new_connection_set) {
if (!connection.attachedToSegment())
if (std::any_of(new_connection_set.begin(), new_connection_set.end(),
[](const auto& connection)
{
return !connection.attachedToSegment();
}))
{
throw std::runtime_error("Not all the connections are attached with a segment. "
"The information from COMPSEGS is not complete");
}
Expand Down
20 changes: 10 additions & 10 deletions opm/input/eclipse/Schedule/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
along with OPM.
*/

#include <string>

#include <opm/input/eclipse/Deck/Deck.hpp>
#include <opm/input/eclipse/Parser/ParserKeywords/S.hpp>
#include <opm/input/eclipse/Schedule/Source.hpp>

#include <algorithm>
#include <string>

namespace Opm {
namespace {

Expand Down Expand Up @@ -137,14 +138,13 @@ std::vector<Source::SourceCell>::const_iterator Source::end() const {
return this->m_cells.end();
}

bool Source::hasSource(const std::array<int, 3>& input) const {
for (const auto& source : m_cells) {
if (source.ijk == input)
{
return true;
}
}
return false;
bool Source::hasSource(const std::array<int, 3>& input) const
{
return std::any_of(m_cells.begin(), m_cells.end(),
[&input](const auto& source)
{
return source.ijk == input;
});
}

double Source::rate(const std::pair<std::array<int, 3>, SourceComponent>& input) const {
Expand Down
14 changes: 8 additions & 6 deletions opm/input/eclipse/Schedule/Well/Well.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "../MSW/Compsegs.hpp"
#include "../eval_uda.hpp"

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
Expand Down Expand Up @@ -1257,12 +1258,13 @@ std::map<int, std::vector<Connection>> Well::getCompletions() const {
return completions;
}

bool Well::hasCompletion(int completion) const {
for (const auto& conn : *this->connections) {
if (conn.complnum() == completion)
return true;
}
return false;
bool Well::hasCompletion(int completion) const
{
return std::any_of(this->connections->begin(), this->connections->end(),
[completion](const auto& conn)
{
return conn.complnum() == completion;
});
}


Expand Down
18 changes: 8 additions & 10 deletions test_util/compareECL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

#include <opm/common/ErrorMacros.hpp>

#include <algorithm>
#include <filesystem>
#include <getopt.h>
#include <iostream>
#include <sstream>
#include <string>
#include <getopt.h>
#include <fstream>

static void printHelp() {
std::cout << "\ncompareECL compares ECLIPSE files (restart (.RST), unified restart (.UNRST), initial (.INIT), summary (.SMRY), unified summary (.UNSMRY) or .RFT) and gridsizes (from .EGRID or .GRID file) from two simulations.\n"
Expand Down Expand Up @@ -70,14 +71,11 @@ static bool has_result_files(const std::string& rootName)
{
std::vector<std::string> extList = { "EGRID", "INIT", "UNRST", "SMSPEC", "RFT" };

for (const auto& ext : extList) {
std::ifstream is(rootName + '.' + ext);
if (is) {
return true;
}
}

return false;
return std::any_of(extList.begin(), extList.end(),
[&rootName](const auto& ext)
{
return std::filesystem::exists(rootName + '.' + ext);
});
}

//------------------------------------------------//
Expand Down

0 comments on commit e9b36bd

Please sign in to comment.