Add DynamicState::find_if()
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
|
||||
|
||||
@@ -152,6 +153,14 @@ class DynamicState {
|
||||
return std::distance( m_data.begin() , iter );
|
||||
}
|
||||
|
||||
template<typename P>
|
||||
int find_if(P&& pred) const {
|
||||
auto iter = std::find_if(m_data.begin(), m_data.end(), std::forward<P>(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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user