IDS peak ICV v1.0.0
Loading...
Searching...
No Matches
peak_icv_array_2d.hpp
Go to the documentation of this file.
1
11#pragma once
12
15#include <array>
16
17namespace peak
18{
19namespace icv
20{
21
35template <typename Type, std::size_t rows, std::size_t columns>
36class Array2D : public detail::IPrintable<Array2D<Type, rows, columns>>
37{
38public:
44 constexpr Array2D()
45 : m_data{}
46 {}
47
55 constexpr Array2D(const Type (&initList)[rows][columns]) // NOLINT
56 {
57 for (std::size_t row = 0; row < rows; ++row)
58 {
59 for (std::size_t col = 0; col < columns; ++col)
60 {
61 m_data[row * columns + col] = initList[row][col];
62 }
63 }
64 }
65
73 explicit constexpr Array2D(std::array<Type, rows * columns> data)
74 : m_data(std::move(data))
75 {}
76
84 PEAK_ICV_NO_DISCARD constexpr const Type* GetData() const
85 {
86 return m_data.data();
87 }
88
103 constexpr Type& At(std::size_t row, std::size_t col)
104 {
105 if (row >= rows || col >= columns)
106 {
107 throw OutOfRangeException("The given row or column index is out of range. Please check the array dimensions.");
108 }
109 return m_data[row * columns + col];
110 }
111
126 PEAK_ICV_NO_DISCARD constexpr const Type& At(std::size_t row, std::size_t col) const
127 {
128 if (row >= rows || col >= columns)
129 {
130 throw OutOfRangeException("The given row or column index is out of range. Please check the array dimensions.");
131 }
132 return m_data[row * columns + col];
133 }
134
142 PEAK_ICV_NO_DISCARD constexpr std::size_t GetNumberOfRows() const
143 {
144 return rows;
145 }
146
154 PEAK_ICV_NO_DISCARD constexpr std::size_t GetNumberOfColumns() const
155 {
156 return columns;
157 }
158
166 PEAK_ICV_NO_DISCARD auto begin();
167
173 PEAK_ICV_NO_DISCARD auto cbegin() const;
174
182 PEAK_ICV_NO_DISCARD auto end();
183
189 PEAK_ICV_NO_DISCARD auto cend() const;
190
191protected:
192 void Print(detail::CommaSeperatedStream& stream) const override
193 {
194 for (std::size_t row = 0; row < rows; ++row)
195 {
196 for (std::size_t col = 0; col < columns; ++col)
197 {
198 stream << this->At(row, col);
199 }
200 if (row < rows - 1)
201 {
202 stream << std::endl;
203 }
204 }
205 }
206
207private:
208 std::array<Type, rows * columns> m_data;
209};
210
211template <typename Type, std::size_t rows, std::size_t columns>
213{
214 return m_data.begin();
215}
216
217template <typename T, std::size_t rows, std::size_t columns>
219{
220 return m_data.cbegin();
221}
222
223template <typename Type, std::size_t rows, std::size_t columns>
225{
226 return m_data.end();
227}
228
229template <typename T, std::size_t rows, std::size_t columns>
231{
232 return m_data.cend();
233}
234
235} /* namespace icv */
236} /* namespace peak */
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.
Thrown when a value is outside the allowed range.