23namespace geos::operation::grid {
32 Matrix(
size_t rows,
size_t cols, std::shared_ptr<T[]> data)
38 Matrix(
size_t rows,
size_t cols)
42 if (m_rows > 0 && m_cols > 0) {
44 m_data = std::shared_ptr<T[]>(
new T[m_rows * m_cols]());
48 Matrix(
size_t rows,
size_t cols, T value)
52 if (m_rows > 0 && m_cols > 0) {
54 m_data = std::shared_ptr<T[]>(
new T[m_rows * m_cols]);
57 std::fill(m_data.get(), m_data.get() + m_rows * m_cols, value);
60 explicit Matrix(
const std::vector<std::vector<T>>& data)
61 : m_rows{ data.size() }
62 , m_cols{ data[0].size() }
64 m_data = std::shared_ptr<T[]>(
new T[m_rows * m_cols]());
66 auto lastpos = m_data.get();
67 for (
auto& row : data) {
68 lastpos = std::copy(row.begin(), row.end(), lastpos);
72 Matrix(Matrix<T>&& m) noexcept
73 : m_rows{ m.getNumRows() }
74 , m_cols{ m.getNumCols() }
76 m_data = std::move(m.m_data);
79 T& operator()(
size_t row,
size_t col)
82 return m_data.get()[row * m_cols + col];
85 const T& operator()(
size_t row,
size_t col)
const
88 return m_data.get()[row * m_cols + col];
91 bool operator!=(
const Matrix<T>& other)
const {
92 return !(*
this == other);
95 bool operator==(
const Matrix<T>& other)
const
97 if (m_rows != other.m_rows) {
100 if (m_cols != other.m_cols) {
104 return 0 == memcmp(m_data.get(), other.m_data.get(), m_rows * m_cols *
sizeof(T));
107 void increment(
size_t row,
size_t col,
const T& val)
110 m_data.get()[row * m_cols + col] += val;
113 size_t getNumRows()
const {
return m_rows; }
114 size_t getNumCols()
const {
return m_cols; }
118 return &(m_data[row * m_cols]);
126 const T* data()
const
133 return m_data.release();
136 const T* begin()
const {
140 const T* end()
const {
141 return m_data.get() + m_rows * m_cols;
144#ifdef MATRIX_CHECK_BOUNDS
145 void check(
size_t row,
size_t col)
const
147 if (row + 1 > m_rows) {
148 throw std::out_of_range(
"Row " + std::to_string(row) +
" is out of range.");
150 if (col + 1 > m_cols) {
151 throw std::out_of_range(
"Col " + std::to_string(col) +
" is out of range.");
155 void check(
size_t,
size_t)
const
161 std::shared_ptr<T[]> m_data;
169operator<<(std::ostream& os,
const Matrix<T>& m)
171 for (
size_t i = 0; i < m.getNumRows(); i++) {
172 for (
size_t j = 0; j < m.getNumCols(); j++) {
174 os << std::right << std::fixed << std::setw(10) << std::setprecision(6) << m(i, j) <<
" ";