DynamicState::find will return std::optional<std::size_t>

This commit is contained in:
Joakim Hove 2020-09-16 11:59:03 +02:00
parent e03a97b539
commit a023d4b437
3 changed files with 25 additions and 26 deletions

View File

@ -179,28 +179,26 @@ class DynamicState {
}
}
/// Will return the index of the first occurence of @value, or
/// -1 if @value is not found.
int find(const T& value) const {
/// Will return the index of the first occurence of @value
std::optional<std::size_t> find(const T& value) const {
auto iter = std::find( m_data.begin() , m_data.end() , value);
if( iter == this->m_data.end() ) return -1;
if( iter == this->m_data.end() ) return {};
return std::distance( m_data.begin() , iter );
}
template<typename P>
int find_if(P&& pred) const {
std::optional<std::size_t> 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;
if( iter == this->m_data.end() ) return {};
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 {
/// Will return the index of the first value which is != @value
std::optional<std::size_t> find_not(const T& value) const {
auto iter = std::find_if_not( m_data.begin() , m_data.end() , [&value] (const T& elm) { return value == elm; });
if( iter == this->m_data.end() ) return -1;
if( iter == this->m_data.end() ) return {};
return std::distance( m_data.begin() , iter );
}

View File

@ -2618,8 +2618,8 @@ void Schedule::invalidNamePattern( const std::string& namePattern, std::size_t
for (const auto& well_pair : this->wells_static) {
const auto& well_name = well_pair.first;
const auto& dynamic_state = well_pair.second;
auto open_step = static_cast<std::size_t>(dynamic_state.find_not(nullptr));
if (open_step <= timeStep)
auto open_step = dynamic_state.find_not(nullptr);
if (open_step.value() <= timeStep)
names.push_back(well_name);
}
return names;

View File

@ -202,28 +202,29 @@ BOOST_AUTO_TEST_CASE( find ) {
Opm::TimeMap timeMap = make_timemap(6);
Opm::DynamicState<int> state(timeMap , 137);
BOOST_CHECK_EQUAL( state.find( 137 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 200 ) , -1 );
BOOST_CHECK_EQUAL( state.find_not(137), -1);
BOOST_CHECK_EQUAL( state.find_not(200), 0);
BOOST_CHECK_EQUAL( state.find( 137 ).value() , 0 );
BOOST_CHECK_EQUAL( state.find_not(200).value(), 0);
BOOST_CHECK( !state.find( 200 ));
BOOST_CHECK( !state.find_not(137));
state.update( 0 , 200 );
BOOST_CHECK_EQUAL( state.find( 137 ) , -1 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK( !state.find( 137 ) );
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0 );
state.update( 2 , 300 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ) , 2 );
BOOST_CHECK_EQUAL( state.find_not( 200 ) , 2 );
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ).value() , 2 );
BOOST_CHECK_EQUAL( state.find_not( 200 ).value() , 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 );
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ).value() , 2 );
BOOST_CHECK_EQUAL( state.find( 400 ).value() , 4 );
BOOST_CHECK( !state.find( 500 ));
auto pred = [] (const int& elm) { return elm == 400 ;};
BOOST_CHECK_EQUAL( state.find_if(pred), 4);
BOOST_CHECK_EQUAL( state.find_if(pred).value(), 4);
}