Skip to content

Commit a11a48e

Browse files
author
Atgeirr Flø Rasmussen
committed
Add SparseTable constructor for initializer list, and a test.
1 parent d443a7a commit a11a48e

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

opm/grid/utility/SparseTable.hpp

Lines changed: 11 additions & 0 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>
@@ -148,6 +149,16 @@ 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
152163
/// rows as indicated by the given row sizes.
153164
/// \param data_beg The start of the table data.

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)