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; }
47 const ElementData*
data()
const {
return _pData.get(); }
65 if (n >= _height * _width || !_pData)
66 throw std::out_of_range(
"invalid subscript");
67 ElementData*
data = _pData.get();
88 const ElementData&
at(uint32_t row, uint32_t col)
const
90 if (row >= _height || col >= _width || !_pData)
91 throw std::out_of_range(
"invalid subscript");
92 ElementData*
data = _pData.get();
93 return data[row * _width + col];
103 ElementData&
at(uint32_t row, uint32_t col)
105 return const_cast<ElementData&
>(
115 copy.
resize(_width, _height);
116 memcpy(copy.
data(),
data(), _height * _width *
sizeof(ElementData));
135 _pData.reset(
new ElementData[_width * _height], [](ElementData* p) {
delete[] p; });
151 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 an element reference to the specified index in the Array2D object using the operator [].
const ElementData & operator[](std::size_t n) const
Returns a const element reference to the specified index in the Array2D object using the operator [].
ElementData * data()
Returns the pointer to element in the Array2D object. The returned pointer will be invalidated after ...
const ElementData & at(uint32_t row, uint32_t col) const
Returns a const element reference to the specified row and column index 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 element in the Array2D object. The returned pointer will be invalidated after ...
ElementData & at(uint32_t row, uint32_t col)
Returns an element reference to the specified row and column index 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.