DynamicState::update_equal() will return index of next value
This commit is contained in:
@@ -21,10 +21,11 @@
|
||||
#ifndef DYNAMICSTATE_HPP_
|
||||
#define DYNAMICSTATE_HPP_
|
||||
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
|
||||
|
||||
@@ -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<std::size_t> 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 {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user