MxEngine
Array2D.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/STL/MxVector.h"
32 #include "Utilities/Array/ArrayView.h"
33 
34 namespace MxEngine
35 {
40  template<typename T, template<typename> typename Base = MxVector>
41  class Array2D
42  {
43  public:
44  using BaseStorage = Base<T>;
45 
46  private:
50  BaseStorage _vec;
54  size_t _height;
55  public:
56 
57  Array2D();
58  Array2D(const Array2D&) = default;
59  Array2D(Array2D&&) = default;
60  Array2D& operator=(const Array2D&) = default;
61  Array2D& operator=(Array2D&&) = default;
62  ~Array2D() = default;
63  [[nodiscard]] size_t size() const;
64  size_t width() const;
65  size_t height() const;
66  T* data();
67  const T* data() const;
68  ArrayView<T> operator[](size_t idx);
69  ArrayView<const T> operator[](size_t idx) const;
70 
71  void resize(size_t width, size_t height, T value = T());
72  void rearrange(size_t width, size_t height);
73  void clear();
74 
75  auto begin() { return this->_vec.begin(); }
76  auto end() { return this->_vec.end(); }
77  auto begin() const { return this->_vec.begin(); }
78  auto end() const { return this->_vec.end(); }
79  };
80 
84  template<typename T, template<typename> typename Base>
86  : _height(0)
87  {
88  }
89 
93  template<typename T, template<typename> typename Base>
94  inline size_t Array2D<T, Base>::size() const
95  {
96  return this->_vec.size();
97  }
98 
102  template<typename T, template<typename> typename Base>
103  inline size_t Array2D<T, Base>::height() const
104  {
105  return this->_height;
106  }
107 
111  template<typename T, template<typename> typename Base>
112  inline size_t Array2D<T, Base>::width() const
113  {
114  return this->_height == 0 ? 0 : this->_vec.size() / this->_height;
115  }
116 
120  template<typename T, template<typename> typename Base>
122  {
123  return this->_vec.data();
124  }
125 
129  template<typename T, template<typename> typename Base>
130  inline const T* Array2D<T, Base>::data() const
131  {
132  return this->_vec.data();
133  }
134 
140  template<typename T, template<typename> typename Base>
142  {
143  MX_ASSERT(idx * this->_height < this->_vec.size());
144  return ArrayView<T>(_vec.data() + idx * this->_height, this->_height);
145  }
146 
152  template<typename T, template<typename> typename Base>
154  {
155  MX_ASSERT(idx * this->_height < this->_vec.size());
156  return ArrayView<const T>(_vec.data() + idx * this->_height, this->_height);
157  }
158 
165  template<typename T, template<typename> typename Base>
166  inline void Array2D<T, Base>::resize(size_t width, size_t height, T value)
167  {
168  this->_vec.resize(width * height, value);
169  this->_height = height;
170  }
171 
178  template<typename T, template<typename> typename Base>
179  inline void Array2D<T, Base>::rearrange(size_t width, size_t height)
180  {
181  MX_ASSERT(width * height == this->_vec.size());
182  this->_height = height;
183  }
184 
188  template<typename T, template<typename> typename Base>
190  {
191  this->_vec.clear();
192  this->_height = 0;
193  }
194 }
Definition: Array2D.h:41
void clear()
Definition: Array2D.h:189
size_t width() const
Definition: Array2D.h:112
Array2D()
Definition: Array2D.h:85
void resize(size_t width, size_t height, T value=T())
Definition: Array2D.h:166
void rearrange(size_t width, size_t height)
Definition: Array2D.h:179
size_t size() const
Definition: Array2D.h:94
size_t height() const
Definition: Array2D.h:103
T * data()
Definition: Array2D.h:121
Definition: ArrayView.h:44
Definition: Application.cpp:49
ArrayView< T > operator[](size_t idx)
Definition: Array2D.h:141