35 #ifndef OPM_SPARSETABLE_HEADER
36 #define OPM_SPARSETABLE_HEADER
41 #include <boost/range/iterator_range.hpp>
42 #include <opm/common/ErrorMacros.hpp>
67 template <
typename DataIter,
typename IntegerIter>
69 IntegerIter rowsize_beg, IntegerIter rowsize_end)
70 : data_(data_beg, data_end)
72 setRowStartsFromSizes(rowsize_beg, rowsize_end);
82 template <
typename DataIter,
typename IntegerIter>
83 void assign(DataIter data_beg, DataIter data_end,
84 IntegerIter rowsize_beg, IntegerIter rowsize_end)
86 data_.assign(data_beg, data_end);
87 setRowStartsFromSizes(rowsize_beg, rowsize_end);
94 template <
typename IntegerIter>
95 void allocate(IntegerIter rowsize_beg, IntegerIter rowsize_end)
97 typedef typename std::vector<T>::size_type sz_t;
99 sz_t ndata = std::accumulate(rowsize_beg, rowsize_end, sz_t(0));
101 setRowStartsFromSizes(rowsize_beg, rowsize_end);
106 template <
typename DataIter>
109 data_.insert(data_.end(), row_beg, row_end);
110 if (row_start_.empty()) {
111 row_start_.reserve(2);
112 row_start_.push_back(0);
114 row_start_.push_back(data_.size());
120 return row_start_.empty();
126 return empty() ? 0 : row_start_.size() - 1;
130 void reserve(
int exptd_nrows,
int exptd_ndata)
132 row_start_.reserve(exptd_nrows + 1);
133 data_.reserve(exptd_ndata);
139 row_start_.swap(other.row_start_);
140 data_.swap(other.data_);
153 OPM_ERROR_IF(row < 0 || row >=
size(),
"Row index " << row <<
" is out of range");
155 return row_start_[row + 1] - row_start_[row];
172 assert(row >= 0 && row <
size());
173 const T* start_ptr = data_.empty() ? 0 : &data_[0];
174 return row_type(start_ptr + row_start_[row], start_ptr + row_start_[row + 1]);
180 assert(row >= 0 && row <
size());
181 T* start_ptr = data_.empty() ? 0 : &data_[0];
182 return mutable_row_type(start_ptr + row_start_[row], start_ptr + row_start_[row + 1]);
188 return data_ == other.data_ && row_start_ == other.row_start_;
191 template<
class charT,
class traits>
192 void print(std::basic_ostream<charT, traits>& os)
const
194 os <<
"Number of rows: " <<
size() <<
'\n';
196 os <<
"Row starts = [";
197 std::copy(row_start_.begin(), row_start_.end(),
198 std::ostream_iterator<int>(os,
" "));
201 os <<
"Data values = [";
202 std::copy(data_.begin(), data_.end(),
203 std::ostream_iterator<T>(os,
" "));
211 std::vector<T> data_;
214 std::vector<int> row_start_;
216 template <
class IntegerIter>
217 void setRowStartsFromSizes(IntegerIter rowsize_beg, IntegerIter rowsize_end)
221 int num_rows = rowsize_end - rowsize_beg;
223 OPM_THROW(std::runtime_error,
"Must have at least one row. Got " << num_rows <<
" rows.");
226 if (*std::min_element(rowsize_beg, rowsize_end) < 0) {
227 OPM_THROW(std::runtime_error,
"All row sizes must be at least 0.");
230 row_start_.resize(num_rows + 1);
232 std::partial_sum(rowsize_beg, rowsize_end, row_start_.begin() + 1);
234 if (
int(data_.size()) != row_start_.back()) {
235 OPM_THROW(std::runtime_error,
"End of row start indices different from data size.");
244 #endif // OPM_SPARSETABLE_HEADER
SparseTable(DataIter data_beg, DataIter data_end, IntegerIter rowsize_beg, IntegerIter rowsize_end)
Definition: SparseTable.hpp:68
int rowSize(int row) const
Returns the size of a table row.
Definition: SparseTable.hpp:150
const T data(int i) const
Definition: SparseTable.hpp:206
bool operator==(const SparseTable &other) const
Equality.
Definition: SparseTable.hpp:186
Definition: AnisotropicEikonal.hpp:43
boost::iterator_range< const T * > row_type
Defining the row type, returned by operator[].
Definition: SparseTable.hpp:166
boost::iterator_range< T * > mutable_row_type
Definition: SparseTable.hpp:167
void appendRow(DataIter row_beg, DataIter row_end)
Appends a row to the table.
Definition: SparseTable.hpp:107
void print(std::basic_ostream< charT, traits > &os) const
Definition: SparseTable.hpp:192
void assign(DataIter data_beg, DataIter data_end, IntegerIter rowsize_beg, IntegerIter rowsize_end)
Definition: SparseTable.hpp:83
bool empty() const
True if the table contains no rows.
Definition: SparseTable.hpp:118
void swap(SparseTable< T > &other)
Swap contents for other SparseTable
Definition: SparseTable.hpp:137
int size() const
Returns the number of rows in the table.
Definition: SparseTable.hpp:124
row_type operator[](int row) const
Returns a row of the table.
Definition: SparseTable.hpp:170
SparseTable()
Default constructor. Yields an empty SparseTable.
Definition: SparseTable.hpp:58
void clear()
Makes the table empty().
Definition: SparseTable.hpp:159
int dataSize() const
Returns the number of data elements.
Definition: SparseTable.hpp:144
Definition: TofDiscGalReorder.hpp:38
void reserve(int exptd_nrows, int exptd_ndata)
Allocate storage for table of expected size.
Definition: SparseTable.hpp:130
mutable_row_type operator[](int row)
Returns a mutable row of the table.
Definition: SparseTable.hpp:178
void allocate(IntegerIter rowsize_beg, IntegerIter rowsize_end)
Definition: SparseTable.hpp:95