Skip to content

Commit 7bdf0f1

Browse files
authored
Merge pull request #1043 from atgeirr/sparsetable-initializer-list
SparseTable initializer_list constructor
2 parents 0ff52dd + a11a48e commit 7bdf0f1

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

opm/grid/utility/SparseTable.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#include <algorithm>
4646
#include <cassert>
47+
#include <initializer_list>
4748
#include <numeric>
4849
#include <ostream>
4950
#include <type_traits>
@@ -53,7 +54,7 @@ namespace Opm
5354
{
5455

5556

56-
template<class>
57+
template<class>
5758
inline constexpr bool always_false_v = false;
5859

5960
// Poison iterator is a helper class that will allow for compilation only when it is not used.
@@ -132,10 +133,10 @@ struct PoisonIterator {
132133
IntegerIter rowsize_beg, IntegerIter rowsize_end)
133134
: data_(data_beg, data_end)
134135
{
135-
setRowStartsFromSizes(rowsize_beg, rowsize_end);
136+
setRowStartsFromSizes(rowsize_beg, rowsize_end);
136137
}
137138

138-
SparseTable (Storage<T>&& data, Storage<int>&& row_starts)
139+
SparseTable(Storage<T>&& data, Storage<int>&& row_starts)
139140
: data_(std::move(data))
140141
, row_start_(std::move(row_starts))
141142
{
@@ -148,8 +149,18 @@ struct PoisonIterator {
148149
}
149150

150151

152+
/// Initializer list constructor for easy construction of small SparseTables.
153+
SparseTable(std::initializer_list<std::initializer_list<T>> initlist) requires (std::is_same_v<Storage<T>, std::vector<T>>)
154+
{
155+
row_start_.push_back(0);
156+
for (const auto& row : initlist) {
157+
data_.insert(data_.end(), row);
158+
row_start_.push_back(data_.size());
159+
}
160+
}
161+
151162
/// Sets the table to contain the given data, organized into
152-
/// rows as indicated by the given row sizes.
163+
/// rows as indicated by the given row sizes.
153164
/// \param data_beg The start of the table data.
154165
/// \param data_end One-beyond-end of the table data.
155166
/// \param rowsize_beg The start of the row length data.
@@ -158,8 +169,8 @@ struct PoisonIterator {
158169
void assign(DataIter data_beg, DataIter data_end,
159170
IntegerIter rowsize_beg, IntegerIter rowsize_end)
160171
{
161-
data_.assign(data_beg, data_end);
162-
setRowStartsFromSizes(rowsize_beg, rowsize_end);
172+
data_.assign(data_beg, data_end);
173+
setRowStartsFromSizes(rowsize_beg, rowsize_end);
163174
}
164175

165176

tests/test_sparsetable.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ BOOST_AUTO_TEST_CASE(construction_and_queries)
108108
}
109109
BOOST_CHECK(st2 == st2_allocate);
110110

111+
// Test initializer_list constructor.
112+
const SparseTable<int> st2_initlist = { {0}, {}, {1, 2}, {3, 4, 5, 6}, {7, 8, 9} };
113+
BOOST_CHECK(st2 == st2_initlist);
114+
111115
// One element too few.
112116
BOOST_CHECK_THROW(const SparseTable<int> st3(elem, elem + num_elem - 1, rowsizes, rowsizes + num_rows), std::exception);
113117

0 commit comments

Comments
 (0)