35template <
typename Type, std::
size_t rows, std::
size_t columns>
55 constexpr Array2D(
const Type (&initList)[rows][columns])
57 for (std::size_t row = 0; row < rows; ++row)
59 for (std::size_t col = 0; col < columns; ++col)
61 m_data[row * columns + col] = initList[row][col];
73 explicit constexpr Array2D(std::array<Type, rows * columns> data)
74 : m_data(std::move(data))
84 PEAK_COMMON_NO_DISCARD
constexpr const Type*
GetData()
const
103 constexpr Type&
At(std::size_t row, std::size_t col)
105 if (row >= rows || col >= columns)
107 throw OutOfRangeException(
"The given row or column index is out of range. Please check the array dimensions.");
109 return m_data[row * columns + col];
126 PEAK_COMMON_NO_DISCARD
constexpr const Type&
At(std::size_t row, std::size_t col)
const
128 if (row >= rows || col >= columns)
130 throw OutOfRangeException(
"The given row or column index is out of range. Please check the array dimensions.");
132 return m_data[row * columns + col];
166 PEAK_COMMON_NO_DISCARD
auto begin();
173 PEAK_COMMON_NO_DISCARD
auto cbegin()
const;
182 PEAK_COMMON_NO_DISCARD
auto end();
189 PEAK_COMMON_NO_DISCARD
auto cend()
const;
194 for (std::size_t row = 0; row < rows; ++row)
196 for (std::size_t col = 0; col < columns; ++col)
198 stream << this->
At(row, col);
208 std::array<Type, rows * columns> m_data;
211template <
typename Type, std::
size_t rows, std::
size_t columns>
214 return m_data.begin();
217template <
typename T, std::
size_t rows, std::
size_t columns>
220 return m_data.cbegin();
223template <
typename Type, std::
size_t rows, std::
size_t columns>
229template <
typename T, std::
size_t rows, std::
size_t columns>
232 return m_data.cend();
A class template for a two-dimensional array.
constexpr Array2D(std::array< Type, rows *columns > data)
Constructor that initializes the array with a given 1D data array.
auto end()
Returns an iterator to the end of the array.
constexpr Array2D()
Default constructor that initializes the array with default values.
constexpr const Type & At(std::size_t row, std::size_t col) const
Accesses the element at the specified row and column (const version).
constexpr std::size_t GetNumberOfRows() const
Gets the number of rows in the array.
auto cbegin() const
Returns a const iterator to the beginning of the array.
constexpr const Type * GetData() const
Returns a pointer to the underlying data of the array.
constexpr std::size_t GetNumberOfColumns() const
Gets the number of columns in the array.
constexpr Array2D(const Type(&initList)[rows][columns])
Constructor that initializes the array with a given 2D initializer list.
constexpr Type & At(std::size_t row, std::size_t col)
Accesses the element at the specified row and column.
auto begin()
Returns an iterator to the beginning of the array.
auto cend() const
Returns a const iterator to the end of the array.
Occurs when a provided value falls outside the allowed valid range.