Added find() method to DynamicState

This commit is contained in:
Joakim Hove
2016-03-22 16:30:07 +01:00
parent 1491898706
commit b91c6fdbf3
2 changed files with 42 additions and 0 deletions

View File

@@ -143,6 +143,23 @@ namespace Opm {
}
/// Will return the index of the first occurence of @value, or
/// -1 if @value is not found.
ssize_t find(const T& value) {
auto iter = std::find( m_data.begin() , m_data.end() , value);
if (iter != m_data.end()) {
// Found normally
return std::distance( m_data.begin() , iter );
} else {
if ((m_data.size() == 0) && (value == m_currentValue))
// Not found explicitly - but the value corresponds to the initial 'current value'
return 0;
// Not found
return -1;
}
}
private:

View File

@@ -248,3 +248,28 @@ BOOST_AUTO_TEST_CASE( UpdateEmptyInitial ) {
state.updateInitial( 99 );
BOOST_CHECK_EQUAL( state[5] , 99 );
}
BOOST_AUTO_TEST_CASE( find ) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(boost::posix_time::ptime(startDate)));
Opm::DynamicState<int> state(timeMap , 137);
for (size_t i = 0; i < 5; i++)
timeMap->addTStep( boost::posix_time::hours( (i+1) * 24 ));
BOOST_CHECK_EQUAL( state.find( 137 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 200 ) , -1 );
state.update( 0 , 200 );
BOOST_CHECK_EQUAL( state.find( 137 ) , -1 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
state.update( 2 , 300 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ) , 2 );
state.update( 4 , 400 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ) , 2 );
BOOST_CHECK_EQUAL( state.find( 400 ) , 4 );
BOOST_CHECK_EQUAL( state.find( 500 ) , -1 );
}