15template <
typename ElementData>
22 Array2D() : _width(0), _height(0), _pData(nullptr) {}
31 size_t width()
const {
return _width; }
36 size_t height()
const {
return _height; }
48 const ElementData*
data()
const {
return _pData.get(); }
68 if (n >= _height * _width || !_pData)
69 throw std::out_of_range(
"invalid subscript");
70 ElementData*
data = _pData.get();
93 const ElementData&
at(uint32_t row, uint32_t col)
const
95 if (row >= _height || col >= _width || !_pData)
96 throw std::out_of_range(
"invalid subscript");
97 ElementData*
data = _pData.get();
98 return data[row * _width + col];
109 ElementData&
at(uint32_t row, uint32_t col)
111 return const_cast<ElementData&
>(
121 copy.
resize(_width, _height);
122 memcpy(copy.
data(),
data(), _height * _width *
sizeof(ElementData));
141 _pData.reset(
new ElementData[_width * _height], [](ElementData* p) {
delete[] p; });
157 std::shared_ptr<ElementData> _pData;
Represents a 2D container of data.
size_t height() const
Returns the height of the Array2D object.
ElementData & operator[](std::size_t n)
Returns a reference to the constant element with the specified index in the Array2D object using the ...
const ElementData & operator[](std::size_t n) const
Returns a reference to the constant element with the specified index in the Array2D object using the ...
ElementData * data()
Returns the pointer to an element in the Array2D object. The returned pointer will be invalidated aft...
const ElementData & at(uint32_t row, uint32_t col) const
Returns a reference to the constant element at the specified row and column in the Array2D object.
Array2D< ElementData > clone() const
Creates a deep copy of the Array2D object.
void resize(size_t width, size_t height)
Changes the size of the Array2D object. It destroys the existing data and reallocates memory accordin...
const ElementData * data() const
Returns the pointer to an element in the Array2D object. The returned pointer will be invalidated aft...
ElementData & at(uint32_t row, uint32_t col)
Returns a reference to the element at the specified row and column in the Array2D object.
size_t width() const
Returns the width of the Array2D object.
bool isEmpty() const
Returns true if the Array2D object has no elements.
void release()
Deallocates the data in the Array2D object.
~Array2D()
Describes a destructor.