SimpleTable improvements:

- Added constructor without DeckItem
 - Added addRow( ) method
 - Added random access get( ) methd.
This commit is contained in:
Joakim Hove
2015-12-31 09:36:53 +01:00
parent 874a5320e7
commit a2b64a83eb
7 changed files with 153 additions and 1 deletions

View File

@@ -36,6 +36,24 @@ namespace Opm {
}
SimpleTable::SimpleTable(std::shared_ptr<TableSchema> schema )
: m_schema( schema )
{
addColumns();
}
void SimpleTable::addRow( const std::vector<double>& row) {
if (row.size() == numColumns()) {
for (size_t colIndex = 0; colIndex < numColumns(); colIndex++) {
auto& col = getColumn( colIndex );
col.addValue( row[colIndex] );
}
} else
throw std::invalid_argument("Size mismatch");
}
void SimpleTable::addColumns() {
for (size_t colIdx = 0; colIdx < m_schema->size(); ++colIdx) {
const auto& schemaColumn = m_schema->getColumn( colIdx );
@@ -45,6 +63,19 @@ namespace Opm {
}
double SimpleTable::get(const std::string& column , size_t row) const {
const auto& col = getColumn( column );
return col[row];
}
double SimpleTable::get(size_t column , size_t row) const {
const auto& col = getColumn( column );
return col[row];
}
// create table from single record
void SimpleTable::init(Opm::DeckItemConstPtr deckItem)
{
@@ -79,6 +110,7 @@ size_t SimpleTable::numRows() const {
}
const TableColumn& SimpleTable::getColumn( const std::string& name) const {
return std::forward<const TableColumn &>(m_columns.get( name ));
}

View File

@@ -38,16 +38,20 @@ namespace Opm {
SimpleTable(const SimpleTable&) = default;
SimpleTable();
SimpleTable(std::shared_ptr<TableSchema> schema , Opm::DeckItemConstPtr deckItem);
explicit SimpleTable(std::shared_ptr<TableSchema> schema);
void addColumns();
void init(Opm::DeckItemConstPtr deckItem);
size_t numColumns() const;
size_t numRows() const;
void addRow( const std::vector<double>& row);
const TableColumn& getColumn(const std::string &name) const;
const TableColumn& getColumn(size_t colIdx) const;
TableColumn& getColumn(const std::string &name);
TableColumn& getColumn(size_t colIdx);
double get(const std::string& column , size_t row) const;
double get(size_t column , size_t row) const;
/*!
* \brief Evaluate a column of the table at a given position.
*

View File

@@ -217,6 +217,14 @@ namespace Opm {
}
}
std::vector<double>::const_iterator TableColumn::begin() const {
return m_values.begin();
}
std::vector<double>::const_iterator TableColumn::end() const {
return m_values.end();
}
bool TableColumn::hasDefault( ) const {
if (m_defaultCount > 0)
@@ -303,6 +311,10 @@ namespace Opm {
}
std::vector<double> TableColumn::vectorCopy() const {
return std::vector<double>( begin() , end());
}
}

View File

@@ -58,6 +58,9 @@ namespace Opm {
void assertUnitRange() const;
TableColumn& operator= (const TableColumn& other);
std::vector<double> vectorCopy() const;
std::vector<double>::const_iterator begin() const;
std::vector<double>::const_iterator end() const;
private:
void assertUpdate(size_t index, double value) const;
void assertPrevious(size_t index , double value) const;
@@ -69,6 +72,8 @@ namespace Opm {
std::vector<bool> m_default;
size_t m_defaultCount;
};
}

View File

@@ -1,4 +1,7 @@
foreach(tapp TableManagerTests TabdimsTests TableContainerTests
foreach(tapp TableManagerTests
TabdimsTests
TableContainerTests
SimpleTableTests
ColumnSchemaTests
TableSchemaTests
TableColumnTests)

View File

@@ -0,0 +1,88 @@
/*
Copyright (C) 2015 by Statoil ASA
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE SimpleTableTests
#include <opm/common/utility/platform_dependent/disable_warnings.h>
#include <boost/test/unit_test.hpp>
#include <opm/common/utility/platform_dependent/reenable_warnings.h>
#include <opm/parser/eclipse/EclipseState/Tables/TableIndex.hpp>
#include <opm/parser/eclipse/EclipseState/Tables/TableColumn.hpp>
#include <opm/parser/eclipse/EclipseState/Tables/ColumnSchema.hpp>
#include <opm/parser/eclipse/EclipseState/Tables/SimpleTable.hpp>
using namespace Opm;
BOOST_AUTO_TEST_CASE( CreateTest ) {
std::shared_ptr<TableSchema> schema = std::make_shared<TableSchema>( );
{
ColumnSchema col1("Name1" , Table::INCREASING , Table::DEFAULT_NONE);
ColumnSchema col2("Name2" , Table::INCREASING , Table::DEFAULT_NONE);
schema->addColumn( col1 );
schema->addColumn( col2 );
}
SimpleTable table(schema);
BOOST_CHECK_THROW( table.addRow( {1,2,3} ), std::invalid_argument);
table.addRow( {1,2} );
table.addRow( {3,4} );
{
const auto& col1 = table.getColumn( 0 );
const auto& col2 = table.getColumn( 1 );
BOOST_CHECK_EQUAL( col1[0] , 1 );
BOOST_CHECK_EQUAL( col2[0] , 2 );
BOOST_CHECK_EQUAL( col1[1] , 3 );
BOOST_CHECK_EQUAL( col2[1] , 4 );
}
BOOST_CHECK_THROW( table.get("NameX" , 0) , std::invalid_argument);
BOOST_CHECK_THROW( table.get(3 , 0) , std::invalid_argument);
BOOST_CHECK_THROW( table.get("Name1" , 3) , std::invalid_argument);
BOOST_CHECK_THROW( table.get(0 , 3) , std::invalid_argument);
BOOST_CHECK_EQUAL( table.get("Name1" , 0) , 1 );
BOOST_CHECK_EQUAL( table.get("Name1" , 1) , 3 );
BOOST_CHECK_EQUAL( table.get(0 , 0) , 1 );
BOOST_CHECK_EQUAL( table.get(0 , 1) , 3 );
BOOST_CHECK_EQUAL( table.get("Name2" , 0) , 2 );
BOOST_CHECK_EQUAL( table.get("Name2" , 1) , 4 );
BOOST_CHECK_EQUAL( table.get(1 , 0) , 2 );
BOOST_CHECK_EQUAL( table.get(1 , 1) , 4 );
{
const auto& col = table.getColumn("Name1");
auto exportCol = col.vectorCopy();
BOOST_CHECK_EQUAL( col.size() , exportCol.size());
for (size_t i = 0; i < col.size(); i++)
BOOST_CHECK_EQUAL( col[i] , exportCol[i]);
}
}

View File

@@ -47,6 +47,14 @@ BOOST_AUTO_TEST_CASE( CreateTest ) {
BOOST_CHECK_EQUAL( column[2] , 2 );
BOOST_CHECK_THROW( column[3] , std::invalid_argument );
{
std::vector<double> cp(column.size());
std::copy( column.begin() , column.end() , cp.begin());
for (size_t i = 0; i < column.size(); i++)
BOOST_CHECK_EQUAL( column[i] , cp[i] );
}
}