Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
fixedtable.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
12
#include "
core/types.h
"
13
14
//------------------------------------------------------------------------------
15
namespace
Util
16
{
17
template
<
class
TYPE>
class
FixedTable
18
{
19
public
:
21
FixedTable
();
23
FixedTable
(
SizeT
w,
SizeT
h);
25
FixedTable
(
SizeT
w,
SizeT
h,
const
TYPE& val);
27
FixedTable
(
const
FixedTable<TYPE>
& rhs);
29
FixedTable
(
FixedTable<TYPE>
&& rhs)
noexcept
;
31
~FixedTable
();
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
55
private
:
57
void
Delete
();
59
void
Allocate
(
SizeT
w,
SizeT
h);
61
void
Copy
(
const
FixedTable<TYPE>
& src);
62
63
SizeT
width
;
64
SizeT
height
;
65
TYPE*
elements
;
66
};
67
68
//------------------------------------------------------------------------------
71
template
<
class
TYPE>
72
FixedTable<TYPE>::FixedTable
() :
73
width
(0),
74
height
(0),
75
elements
(0)
76
{
77
// empty
78
}
79
80
//------------------------------------------------------------------------------
83
template
<
class
TYPE>
84
void
85
FixedTable<TYPE>::Delete
()
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
//------------------------------------------------------------------------------
99
template
<
class
TYPE>
100
void
101
FixedTable<TYPE>::Allocate
(
SizeT
w,
SizeT
h)
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
//------------------------------------------------------------------------------
118
template
<
class
TYPE>
119
void
120
FixedTable<TYPE>::Copy
(
const
FixedTable<TYPE>
& rhs)
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
//------------------------------------------------------------------------------
141
template
<
class
TYPE>
142
void
143
FixedTable<TYPE>::Clear
(
const
TYPE& val)
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
//------------------------------------------------------------------------------
160
template
<
class
TYPE>
161
FixedTable<TYPE>::FixedTable
(
SizeT
w,
SizeT
h) :
162
width
(0),
163
height
(0),
164
elements
(0)
165
{
166
this->
Allocate
(w, h);
167
}
168
169
//------------------------------------------------------------------------------
172
template
<
class
TYPE>
173
FixedTable<TYPE>::FixedTable
(
SizeT
w,
SizeT
h,
const
TYPE& val) :
174
width
(0),
175
height
(0),
176
elements
(0)
177
{
178
this->
Allocate
(w, h);
179
this->
Clear
(val);
180
}
181
182
//------------------------------------------------------------------------------
185
template
<
class
TYPE>
186
FixedTable<TYPE>::FixedTable
(
const
FixedTable<TYPE>
& rhs)
187
:
width
(0)
188
,
height
(0)
189
,
elements
(0)
190
{
191
this->
Copy
(rhs);
192
}
193
194
//------------------------------------------------------------------------------
197
template
<
class
TYPE>
198
FixedTable<TYPE>::FixedTable
(
FixedTable<TYPE>
&& rhs) noexcept
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
//------------------------------------------------------------------------------
211
template
<
class
TYPE>
212
FixedTable<TYPE>::~FixedTable
()
213
{
214
this->
Delete
();
215
}
216
217
//------------------------------------------------------------------------------
220
template
<
class
TYPE>
221
void
222
FixedTable<TYPE>::operator=
(
const
FixedTable<TYPE>
& rhs)
223
{
224
this->
Delete
();
225
this->
Copy
(rhs);
226
}
227
228
//------------------------------------------------------------------------------
231
template
<
class
TYPE>
232
void
233
FixedTable<TYPE>::operator=
(
FixedTable<TYPE>
&& rhs)
noexcept
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
//------------------------------------------------------------------------------
250
template
<
class
TYPE>
251
bool
252
FixedTable<TYPE>::operator==
(
const
FixedTable<TYPE>
& rhs)
const
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
//------------------------------------------------------------------------------
282
template
<
class
TYPE>
283
bool
284
FixedTable<TYPE>::operator!=
(
const
FixedTable<TYPE>
& rhs)
const
285
{
286
return
!(*
this
== rhs);
287
}
288
289
//------------------------------------------------------------------------------
292
template
<
class
TYPE>
293
void
294
FixedTable<TYPE>::SetSize
(
SizeT
w,
SizeT
h)
295
{
296
this->
Delete
();
297
this->
Allocate
(w, h);
298
}
299
300
//------------------------------------------------------------------------------
303
template
<
class
TYPE>
304
SizeT
305
FixedTable<TYPE>::Width
()
const
306
{
307
return
this->
width
;
308
}
309
310
//------------------------------------------------------------------------------
313
template
<
class
TYPE>
314
SizeT
315
FixedTable<TYPE>::Height
()
const
316
{
317
return
this->
height
;
318
}
319
320
//------------------------------------------------------------------------------
323
template
<
class
TYPE>
324
void
325
FixedTable<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
//------------------------------------------------------------------------------
339
template
<
class
TYPE>
340
TYPE&
341
FixedTable<TYPE>::At
(
IndexT
x,
IndexT
y)
const
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
//------------------------------------------------------------------------------
Util::FixedTable::operator==
bool operator==(const FixedTable< TYPE > &rhs) const
equality operator
Definition
fixedtable.h:252
Util::FixedTable::Set
void Set(IndexT x, IndexT y, const TYPE &val)
set value at [x,y] position
Definition
fixedtable.h:325
Util::FixedTable::width
SizeT width
Definition
fixedtable.h:63
Util::FixedTable::operator!=
bool operator!=(const FixedTable< TYPE > &rhs) const
inequality operator
Definition
fixedtable.h:284
Util::FixedTable::FixedTable
FixedTable()
default constructor
Definition
fixedtable.h:72
Util::FixedTable::Copy
void Copy(const FixedTable< TYPE > &src)
copy content
Definition
fixedtable.h:120
Util::FixedTable::Height
SizeT Height() const
get height
Definition
fixedtable.h:315
Util::FixedTable::At
TYPE & At(IndexT x, IndexT y) const
access value at [x,y] position
Definition
fixedtable.h:341
Util::FixedTable::SetSize
void SetSize(SizeT w, SizeT h)
set width and height (clears existing content)
Definition
fixedtable.h:294
Util::FixedTable::Width
SizeT Width() const
get width
Definition
fixedtable.h:305
Util::FixedTable::elements
TYPE * elements
Definition
fixedtable.h:65
Util::FixedTable::Delete
void Delete()
delete content
Definition
fixedtable.h:85
Util::FixedTable::Allocate
void Allocate(SizeT w, SizeT h)
allocate for given size
Definition
fixedtable.h:101
Util::FixedTable::~FixedTable
~FixedTable()
destructor
Definition
fixedtable.h:212
Util::FixedTable::operator=
void operator=(const FixedTable< TYPE > &rhs)
assignment operator
Definition
fixedtable.h:222
Util::FixedTable::Clear
void Clear(const TYPE &val)
clear the table with value
Definition
fixedtable.h:143
Util::FixedTable::height
SizeT height
Definition
fixedtable.h:64
n_assert
#define n_assert(exp)
Definition
debug.h:50
Util
A quad tree designed to return regions of free 2D space.
Definition
String.cs:6
types.h
SizeT
int SizeT
Definition
types.h:42
IndexT
int IndexT
Definition
types.h:41
code
foundation
util
fixedtable.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.