Nebula
Loading...
Searching...
No Matches
fixedtable.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/types.h"
13
14//------------------------------------------------------------------------------
15namespace Util
16{
17template<class TYPE> class FixedTable
18{
19public:
25 FixedTable(SizeT w, SizeT h, const TYPE& val);
33 void operator=(const FixedTable<TYPE>& rhs);
35 void operator=(FixedTable<TYPE>&& rhs) noexcept;
37 bool operator==(const FixedTable<TYPE>& rhs) const;
39 bool operator!=(const FixedTable<TYPE>& rhs) const;
40
42 void SetSize(SizeT w, SizeT h);
44 SizeT Width() const;
46 SizeT Height() const;
48 void Clear(const TYPE& val);
49
51 void Set(IndexT x, IndexT y, const TYPE& val);
53 TYPE& At(IndexT x, IndexT y) const;
54
55private:
57 void Delete();
59 void Allocate(SizeT w, SizeT h);
61 void Copy(const FixedTable<TYPE>& src);
62
65 TYPE* elements;
66};
67
68//------------------------------------------------------------------------------
71template<class TYPE>
73 width(0),
74 height(0),
75 elements(0)
76{
77 // empty
78}
79
80//------------------------------------------------------------------------------
83template<class TYPE>
84void
86{
87 if (this->elements)
88 {
89 delete[] this->elements;
90 this->elements = 0;
91 }
92 this->width = 0;
93 this->height = 0;
94}
95
96//------------------------------------------------------------------------------
99template<class TYPE>
100void
102{
103 #if NEBULA_BOUNDSCHECKS
104 n_assert(0 == this->elements);
105 #endif
106 if ((w > 0) && (h > 0))
107 {
108 this->elements = new TYPE[w * h];
109 }
110 this->width = w;
111 this->height = h;
112}
113
114//------------------------------------------------------------------------------
118template<class TYPE>
119void
121{
122 if (this != &rhs)
123 {
124 this->Allocate(rhs.width, rhs.height);
125 IndexT y;
126 for (y = 0; y < this->height; y++)
127 {
128 IndexT x;
129 for (x = 0; x < this->width; x++)
130 {
131 int flatIndex = y * this->width + x;
132 this->elements[flatIndex] = rhs.elements[flatIndex];
133 }
134 }
135 }
136}
137
138//------------------------------------------------------------------------------
141template<class TYPE>
142void
144{
145 IndexT y;
146 for (y = 0; y < this->height; y++)
147 {
148 IndexT x;
149 for (x = 0; x < this->width; x++)
150 {
151 int flatIndex = y * this->width + x;
152 this->elements[flatIndex] = val;
153 }
154 }
155}
156
157//------------------------------------------------------------------------------
160template<class TYPE>
162 width(0),
163 height(0),
164 elements(0)
165{
166 this->Allocate(w, h);
167}
168
169//------------------------------------------------------------------------------
172template<class TYPE>
174 width(0),
175 height(0),
176 elements(0)
177{
178 this->Allocate(w, h);
179 this->Clear(val);
180}
181
182//------------------------------------------------------------------------------
185template<class TYPE>
187 : width(0)
188 , height(0)
189 , elements(0)
190{
191 this->Copy(rhs);
192}
193
194//------------------------------------------------------------------------------
197template<class TYPE>
199 : width(rhs.width)
200 , height(rhs.height)
201 , elements(rhs.height)
202{
203 rhs.elements = nullptr;
204 rhs.width = 0;
205 rhs.height = 0;
206}
207
208//------------------------------------------------------------------------------
211template<class TYPE>
213{
214 this->Delete();
215}
216
217//------------------------------------------------------------------------------
220template<class TYPE>
221void
223{
224 this->Delete();
225 this->Copy(rhs);
226}
227
228//------------------------------------------------------------------------------
231template<class TYPE>
232void
234{
235 if (*this != rhs)
236 {
237 this->Delete();
238 this->elements = rhs.elements;
239 this->height = rhs.height;
240 this->width = rhs.width;
241 rhs.elements = nullptr;
242 rhs.width = 0;
243 rhs.height = 0;
244 }
245}
246
247//------------------------------------------------------------------------------
250template<class TYPE>
251bool
253{
254 if ((this->width == rhs.width) && (this->height == rhs.height))
255 {
256 // check elements
257 IndexT y;
258 for (y = 0; y < this->height; y++)
259 {
260 IndexT x;
261 for (x = 0; x < this->width; x++)
262 {
263 int flatIndex = y * this->width + x;
264 if (this->elements[flatIndex] != rhs.elements[flatIndex])
265 {
266 return false;
267 }
268 }
269 }
270 return true;
271 }
272 else
273 {
274 // different size
275 return false;
276 }
277}
278
279//------------------------------------------------------------------------------
282template<class TYPE>
283bool
285{
286 return !(*this == rhs);
287}
288
289//------------------------------------------------------------------------------
292template<class TYPE>
293void
295{
296 this->Delete();
297 this->Allocate(w, h);
298}
299
300//------------------------------------------------------------------------------
303template<class TYPE>
304SizeT
306{
307 return this->width;
308}
309
310//------------------------------------------------------------------------------
313template<class TYPE>
314SizeT
316{
317 return this->height;
318}
319
320//------------------------------------------------------------------------------
323template<class TYPE>
324void
325FixedTable<TYPE>::Set(IndexT x, IndexT y, const TYPE& val)
326{
327 #if NEBULA_BOUNDSCHECKS
328 n_assert((x >= 0) && (x < this->width));
329 n_assert(y < this->height);
330 n_assert(0 != this->elements);
331 #endif
332 int flatIndex = y * this->width + x;
333 this->elements[flatIndex] = val;
334}
335
336//------------------------------------------------------------------------------
339template<class TYPE>
340TYPE&
342{
343 #if NEBULA_BOUNDSCHECKS
344 n_assert((x >= 0) && (x < this->width));
345 n_assert(y < this->height);
346 n_assert(0 != this->elements);
347 #endif
348 int flatIndex = y * this->width + x;
349 return this->elements[flatIndex];
350}
351
352} // namespace Util
353//------------------------------------------------------------------------------
A fixed-size 2-dimensional array.
Definition fixedtable.h:18
bool operator==(const FixedTable< TYPE > &rhs) const
equality operator
Definition fixedtable.h:252
FixedTable(SizeT w, SizeT h)
constructor with size
Definition fixedtable.h:161
void Set(IndexT x, IndexT y, const TYPE &val)
set value at [x,y] position
Definition fixedtable.h:325
SizeT width
Definition fixedtable.h:63
bool operator!=(const FixedTable< TYPE > &rhs) const
inequality operator
Definition fixedtable.h:284
FixedTable(SizeT w, SizeT h, const TYPE &val)
constructor with size and initialized contents
Definition fixedtable.h:173
FixedTable()
default constructor
Definition fixedtable.h:72
FixedTable(const FixedTable< TYPE > &rhs)
copy constructor
Definition fixedtable.h:186
void Copy(const FixedTable< TYPE > &src)
copy content
Definition fixedtable.h:120
SizeT Height() const
get height
Definition fixedtable.h:315
TYPE & At(IndexT x, IndexT y) const
access value at [x,y] position
Definition fixedtable.h:341
void SetSize(SizeT w, SizeT h)
set width and height (clears existing content)
Definition fixedtable.h:294
SizeT Width() const
get width
Definition fixedtable.h:305
TYPE * elements
Definition fixedtable.h:65
void Delete()
delete content
Definition fixedtable.h:85
FixedTable(FixedTable< TYPE > &&rhs) noexcept
move constructor
Definition fixedtable.h:198
void operator=(FixedTable< TYPE > &&rhs) noexcept
move assignment operator
Definition fixedtable.h:233
void Allocate(SizeT w, SizeT h)
allocate for given size
Definition fixedtable.h:101
~FixedTable()
destructor
Definition fixedtable.h:212
void operator=(const FixedTable< TYPE > &rhs)
assignment operator
Definition fixedtable.h:222
void Clear(const TYPE &val)
clear the table with value
Definition fixedtable.h:143
SizeT height
Definition fixedtable.h:64
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48