From e03a97b539e44e040b7d4889d48a5c0d3303c559 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 16 Sep 2020 09:55:09 +0200 Subject: [PATCH] DynamicState::update_equal() will return index of next value --- .../EclipseState/Schedule/DynamicState.hpp | 20 ++++++++++--------- tests/parser/DynamicStateTests.cpp | 20 ++++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp b/opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp index 2f341e745..f0d84eab4 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,25 +156,26 @@ 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 {}; } } diff --git a/tests/parser/DynamicStateTests.cpp b/tests/parser/DynamicStateTests.cpp index 21de38b08..3b3ed3f03 100644 --- a/tests/parser/DynamicStateTests.cpp +++ b/tests/parser/DynamicStateTests.cpp @@ -265,15 +265,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); + } }