From 9e40f3be4fed7008e769da9abd8cece14586304b Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Tue, 2 Apr 2019 07:23:28 +0200 Subject: [PATCH] Add DynamicState::find_if() --- .../eclipse/EclipseState/Schedule/DynamicState.hpp | 9 +++++++++ tests/parser/DynamicStateTests.cpp | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp b/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp index e712227e8..10c8bc2cf 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -152,6 +153,14 @@ class DynamicState { return std::distance( m_data.begin() , iter ); } + template + int find_if(P&& pred) const { + auto iter = std::find_if(m_data.begin(), m_data.end(), std::forward

(pred)); + if( iter == this->m_data.end() ) return -1; + + return std::distance( m_data.begin() , iter ); + } + /// Will return the index of the first value which is != @value, or -1 /// if all values are == @value int find_not(const T& value) const { diff --git a/tests/parser/DynamicStateTests.cpp b/tests/parser/DynamicStateTests.cpp index 3dfdef37d..1c850c6e2 100644 --- a/tests/parser/DynamicStateTests.cpp +++ b/tests/parser/DynamicStateTests.cpp @@ -248,6 +248,10 @@ BOOST_AUTO_TEST_CASE( find ) { BOOST_CHECK_EQUAL( state.find( 300 ) , 2 ); BOOST_CHECK_EQUAL( state.find( 400 ) , 4 ); BOOST_CHECK_EQUAL( state.find( 500 ) , -1 ); + + + auto pred = [] (const int& elm) { return elm == 400 ;}; + BOOST_CHECK_EQUAL( state.find_if(pred), 4); }