13template <
typename T>
class [[nodiscard]]
Matrix {
22 Matrix(
size_t rows,
size_t cols,
const T& initial_value = T())
23 : data_(rows * cols, initial_value), rows_(rows), cols_(cols) {}
26 void resize(
size_t rows,
size_t cols) {
27 data_.resize(rows * cols);
32 void resize(
size_t rows,
size_t cols,
const T& initial_value) {
33 data_.assign(rows * cols, initial_value);
38 void assign(
size_t rows,
size_t cols,
const T& initial_value) {
39 data_.assign(rows * cols, initial_value);
46 return data_.data() + row_index * cols_;
49 [[nodiscard]]
const T*
operator[](
size_t row_index)
const {
50 return data_.data() + row_index * cols_;
55 return data_[row_index * cols_ + column_index];
58 [[nodiscard]]
const T&
operator()(
size_t row_index,
size_t column_index)
const {
59 return data_[row_index * cols_ + column_index];
62 [[nodiscard]]
size_t rows()
const {
65 [[nodiscard]]
size_t cols()
const {
70 [[nodiscard]]
size_t size()
const {
78 [[nodiscard]]
const T*
data()
const {
83 std::vector<T>&
raw() {
86 [[nodiscard]]
const std::vector<T>&
raw()
const {
90 [[nodiscard]]
bool empty()
const {
Matrix(size_t rows, size_t cols, const T &initial_value=T())
T * data()
Returns the underlying flat data pointer.
void resize(size_t rows, size_t cols, const T &initial_value)
std::vector< T > & raw()
Returns raw vector (useful for MPI calls that need contiguous block)
const std::vector< T > & raw() const
const T * operator[](size_t row_index) const
void assign(size_t rows, size_t cols, const T &initial_value)
void resize(size_t rows, size_t cols)
Resizes the matrix, invalidating previous content structure if dimensions change.
T * operator[](size_t row_index)
Access row row_index. Allows matrix[row_index][column_index] syntax to remain valid.
const T & operator()(size_t row_index, size_t column_index) const
size_t size() const
Returns the number of rows (to mimic outer std::vector::size())
T & operator()(size_t row_index, size_t column_index)
2D indexing operator