diff --git a/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp b/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp index 2f341e745..f30dcc6ca 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp @@ -21,10 +21,11 @@ #ifndef DYNAMICSTATE_HPP_ #define DYNAMICSTATE_HPP_ -#include -#include #include +#include +#include #include +#include #include @@ -155,50 +156,49 @@ class DynamicState { Schedule object, if e.g. a well is initially closed in the interval [T1,T2] and then opened at time T1 < Tx < T2 then the open should be applied for all times in the range [Tx,T2]. + + The return value is the index of the first element different from value, + or an empty optional if there is no such element. */ - void update_equal(size_t index, const T& value) { + std::optional update_equal(size_t index, const T& value) { if (this->m_data.size() <= index) throw std::out_of_range("Invalid index for update_equal()"); const T prev_value = this->m_data[index]; - if (prev_value == value) - return; - + //if (prev_value == value) + // return {}; while (true) { if (this->m_data[index] != prev_value) - break; + return index; this->m_data[index] = value; index++; if (index == this->m_data.size()) - break; - + return {}; } } - /// 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 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 - int find_if(P&& pred) const { + std::optional 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; + 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 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 ); } diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp index ae0ab02d4..e0881ba72 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Schedule.cpp @@ -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(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; diff --git a/tests/parser/DynamicStateTests.cpp b/tests/parser/DynamicStateTests.cpp index 21de38b08..27c6be507 100644 --- a/tests/parser/DynamicStateTests.cpp +++ b/tests/parser/DynamicStateTests.cpp @@ -202,28 +202,29 @@ BOOST_AUTO_TEST_CASE( find ) { Opm::TimeMap timeMap = make_timemap(6); Opm::DynamicState 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); } @@ -265,15 +266,21 @@ BOOST_AUTO_TEST_CASE( update_equal ) { BOOST_CHECK_EQUAL(state[4], 50); BOOST_CHECK_EQUAL(state[5], 100); - state.update_equal(4,50); - BOOST_CHECK_EQUAL(state[4], 50); - BOOST_CHECK_EQUAL(state[5], 100); + { + auto next_index = state.update_equal(4,50); + BOOST_CHECK_EQUAL(state[4], 50); + BOOST_CHECK_EQUAL(state[5], 100); + BOOST_CHECK_EQUAL(next_index.value() , 5); + } - state.update_equal(9,200); - BOOST_CHECK_EQUAL(state[8] , 100); - BOOST_CHECK_EQUAL(state[9] , 200); - BOOST_CHECK_EQUAL(state[10], 200); + { + auto next_index = state.update_equal(9,200); + BOOST_CHECK_EQUAL(state[8] , 100); + BOOST_CHECK_EQUAL(state[9] , 200); + BOOST_CHECK_EQUAL(state[10], 200); + BOOST_CHECK(!next_index); + } }