Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions models/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ add_cml_tests(
cml_message/test/cml_message_test.cc
double_to_words/test/convert_double_to_words_test.cc
table_interp_cpp/test/table_independent_variable_test.cc
table_interp_cpp/test/table_move_data_test.cc
)
37 changes: 37 additions & 0 deletions models/utilities/table_interp_cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,40 @@ This model is independently documented in the docs directory.
## Verification

This model contains independent verification test cases in the verif directory.

## Loading vectors without copying

`TableIndependentVariable::load_data` and `GenericMultiInputTable::load_data`
accept rvalue `DoubleVec` (`std::vector<double>`) arguments. Use `std::move` when
transferring a large data vector that the caller no longer needs:

```cpp
#include <utility>

double input = 0.5;
double output = 0.0;
TableIndependentVariable axis(input);
DoubleVec calibration{0.0, 1.0, 2.0};
if (!axis.load_data(std::move(calibration)) || !axis.initialize()) {
return;
}

GenericMultiInputTable table(output);
table.add_independent(axis);
DoubleVec samples{10.0, 20.0, 30.0};
if (!table.load_data(std::move(samples), SizeVec{1, 3}) || !table.initialize()) {
return;
}
table.update(); // output is 15.0
```

The same move overload is inherited by the single-input table classes. Existing
lvalue and pointer overloads continue to copy. Validation is shared between the
copy and move paths: independent data must be nonempty and monotonic, and
dependent data must match its dimensions and output count. Rejected vector
arguments are not moved from. After a successful move, the caller's vector is
valid but its contents are unspecified; reassign it before using its data again.

These methods keep the existing reload rules. Independent-variable data must
be cleared before reloading; a dependent table can replace previously loaded
data and retains its existing warning.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class GenericMultiInputTable
const SizeVec &dim_list);
bool load_data( const DoubleVec & data_in,
const SizeVec &dim_list);
// Move validated vector storage into the table; rejected inputs are unchanged.
bool load_data( DoubleVec && data_in,
const SizeVec &dim_list);

void add_dependent( double & new_dep_var);
void append_dependent_data( double & new_dep_var,
Expand Down Expand Up @@ -124,6 +127,7 @@ class GenericMultiInputTable
bool load_data_internal_check( const SizeVec &dim_list );
bool copy_data(const double * data_in);
bool copy_data(const DoubleVec & data_in);
bool check_vector_data(const DoubleVec & data_in);
size_t configure_internal_data_structure();
void configure_support_arrays();
virtual void generate_base_values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public:
virtual bool load_data( const double* const data_in,
size_t size_in);
virtual bool load_data( const DoubleVec & data_in);
// Move validated vector storage into the table; rejected inputs are unchanged.
virtual bool load_data( DoubleVec && data_in);

bool initialize();

Expand Down Expand Up @@ -225,6 +227,7 @@ private:
void tag_as_off_table_back();
void tag_as_off_table_front();
void generate_fraction();
bool check_data( const DoubleVec & data_in);
bool check_monotonicity( const std::vector<double> & data_in);
};

Expand Down
36 changes: 32 additions & 4 deletions models/utilities/table_interp_cpp/src/generic_multi_input_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LIBRARY DEPENDENCIES:

#include <algorithm>
#include <cstddef>
#include <utility>

#include "../include/generic_multi_input_table.hh"
#include "../include/table_independent_variable.hh"
Expand Down Expand Up @@ -103,10 +104,23 @@ GenericMultiInputTable::load_data(
return copy_data(data_in);
}

/****************************************************************************/
bool
GenericMultiInputTable::load_data(
DoubleVec && data_in,
const SizeVec &dim_list)
{
if (!load_data_internal_check(dim_list) || !check_vector_data(data_in)) {
return false;
}
data = std::move(data_in);
data_loaded = true;
return true;
}

/*****************************************************************************
load_data_internal_check
Purpose:(Perform internal checks on data, common to both methods of loading
data)
Purpose:(Perform internal checks common to all data-loading methods.)
*****************************************************************************/
bool
GenericMultiInputTable::load_data_internal_check(
Expand Down Expand Up @@ -679,6 +693,22 @@ GenericMultiInputTable::copy_data(
bool
GenericMultiInputTable::copy_data(
const DoubleVec & data_in)
{
if (!check_vector_data(data_in)) {
return false;
}
data = data_in;
data_loaded = true;
return true;
}

/*****************************************************************************
check_vector_data
Purpose:(Check vector dimensions before copying or moving the data.)
*****************************************************************************/
bool
GenericMultiInputTable::check_vector_data(
const DoubleVec & data_in)
{
// Configure internal data structure, abort on error
const size_t total_data_elements = configure_internal_data_structure();
Expand All @@ -703,8 +733,6 @@ GenericMultiInputTable::copy_data(
num_data_elements_per_increment_of_index.clear();
return false;
}
data = data_in;
data_loaded = true;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ LIBRARY DEPENDENCIES:
#include <cmath>
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
#include "cml/models/utilities/cml_message/include/cml_message.hh"
#include "cml/models/utilities/math_utils/include/math_utils.hh"
Expand Down Expand Up @@ -104,6 +105,34 @@ TableIndependentVariable::load_data(
bool
TableIndependentVariable::load_data(
const DoubleVec & data_in)
{
if (!check_data(data_in)) {
return false;
}
data = data_in;
data_loaded = true;
return true;
}
/****************************************************************************/
bool
TableIndependentVariable::load_data(
DoubleVec && data_in)
{
if (!check_data(data_in)) {
return false;
}
data = std::move(data_in);
data_loaded = true;
return true;
}

/*****************************************************************************
check_data
Purpose:(Validate input before copying or moving its storage.)
*****************************************************************************/
bool
TableIndependentVariable::check_data(
const DoubleVec & data_in)
{
if (data_loaded) {
CMLMessage::error(
Expand Down Expand Up @@ -136,11 +165,6 @@ TableIndependentVariable::load_data(
"There is nothing wrong, just nothing to look up; output value "
"is constant.\n");
}
// copy the data.
data = data_in;

data_loaded = true;

return true;
}

Expand Down
Loading