Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
trivialarray.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
12
#include "
core/types.h
"
13
#include "
util/array.h
"
14
15
//------------------------------------------------------------------------------
16
namespace
Util
17
{
18
template
<
class
TYPE>
class
TrivialArray
:
public
Array
<TYPE>
19
{
20
public
:
22
typedef
TYPE*
Iterator
;
23
25
TrivialArray
();
27
TrivialArray
(
SizeT
initialCapacity,
SizeT
initialGrow);
29
TrivialArray
(
SizeT
initialSize,
SizeT
initialGrow,
const
TYPE& initialValue);
31
TrivialArray
(
const
TrivialArray<TYPE>
& rhs);
33
TrivialArray
(
const
Array<TYPE>
& rhs);
35
TrivialArray
(std::initializer_list<TYPE> list);
37
~TrivialArray
();
38
40
void
operator=
(
const
TrivialArray<TYPE>
& rhs);
42
void
operator=
(
const
Array<TYPE>
& rhs);
43
45
void
EraseIndex
(
IndexT
index);
47
void
EraseIndexSwap
(
IndexT
index);
48
50
void
Clear
();
51
52
private
:
54
void
Destroy
(TYPE* elm);
56
void
Copy
(
const
TrivialArray<TYPE>
& src);
58
void
Copy
(
const
Array<TYPE>
& src);
60
void
GrowTo
(
SizeT
newCapacity);
62
void
Move
(
IndexT
fromIndex,
IndexT
toIndex);
63
};
64
65
//------------------------------------------------------------------------------
68
template
<
class
TYPE>
69
TrivialArray<TYPE>::TrivialArray
()
70
{
71
static_assert
(std::is_trivial<TYPE>::value,
"Non trivial type used, try Util::Array instead"
);
72
}
73
74
//------------------------------------------------------------------------------
77
template
<
class
TYPE>
78
TrivialArray<TYPE>::TrivialArray
(
SizeT
_capacity,
SizeT
_grow) :
79
Array
<TYPE>(_capacity, _grow)
80
81
{
82
static_assert
(std::is_trivial<TYPE>::value,
"Non trivial type used, try Util::Array instead"
);
83
}
84
85
//------------------------------------------------------------------------------
88
template
<
class
TYPE>
89
TrivialArray<TYPE>::TrivialArray
(
SizeT
initialSize,
SizeT
_grow,
const
TYPE& initialValue) :
90
Array
<TYPE>(initialSize, _grow, initialValue)
91
{
92
static_assert
(std::is_trivial<TYPE>::value,
"Non trivial type used, try Util::Array instead"
);
93
}
94
95
//------------------------------------------------------------------------------
98
template
<
class
TYPE>
99
TrivialArray<TYPE>::TrivialArray
(std::initializer_list<TYPE> list) :
100
Array
<TYPE>(list)
101
{
102
static_assert
(std::is_trivial<TYPE>::value,
"Non trivial type used, try Util::Array instead"
);
103
104
}
105
106
//------------------------------------------------------------------------------
109
template
<
class
TYPE>
110
TrivialArray<TYPE>::TrivialArray
(
const
TrivialArray<TYPE>
& rhs)
111
{
112
this->
elements
= 0;
113
this->
Copy
(rhs);
114
}
115
116
//------------------------------------------------------------------------------
119
template
<
class
TYPE>
120
TrivialArray<TYPE>::TrivialArray
(
const
Array<TYPE>
& rhs)
121
{
122
this->
elements
= 0;
123
this->
Copy
(rhs);
124
}
125
126
//------------------------------------------------------------------------------
129
template
<
class
TYPE>
void
130
TrivialArray<TYPE>::Copy
(
const
TrivialArray<TYPE>
& src)
131
{
132
#if NEBULA_BOUNDSCHECKS
133
n_assert
(0 == this->
elements
);
134
#endif
135
136
this->
grow
= src.
grow
;
137
this->
capacity
= src.
capacity
;
138
this->
size
= src.
size
;
139
if
(this->
capacity
> 0)
140
{
141
this->
elements
=
new
TYPE[this->
capacity
];
142
Memory::Copy
(src.
elements
, this->elements, this->size *
sizeof
(TYPE));
143
}
144
}
145
146
//------------------------------------------------------------------------------
149
template
<
class
TYPE>
void
150
TrivialArray<TYPE>::Copy
(
const
Array<TYPE>
& src)
151
{
152
#if NEBULA_BOUNDSCHECKS
153
n_assert
(0 == this->
elements
);
154
#endif
155
this->
grow
= src.
grow
;
156
this->
capacity
= src.
capacity
;
157
this->
size
= src.
size
;
158
if
(this->
capacity
> 0)
159
{
160
this->
elements
=
new
TYPE[this->
capacity
];
161
Memory::Copy
(src.
elements
, this->elements, this->size *
sizeof
(TYPE));
162
}
163
}
164
165
//------------------------------------------------------------------------------
168
template
<
class
TYPE>
void
169
TrivialArray<TYPE>::Destroy
(TYPE* elm)
170
{
171
// empty
172
}
173
174
//------------------------------------------------------------------------------
177
template
<
class
TYPE>
178
TrivialArray<TYPE>::~TrivialArray
()
179
{
180
this->
Delete
();
181
}
182
183
//------------------------------------------------------------------------------
186
template
<
class
TYPE>
void
187
TrivialArray<TYPE>::operator=
(
const
TrivialArray<TYPE>
& rhs)
188
{
189
if
(
this
!= &rhs)
190
{
191
if
((this->
capacity
> 0) && (rhs.
size
<= this->capacity))
192
{
193
// source array fits into our capacity, copy in place
194
n_assert
(0 != this->
elements
);
195
Memory::Copy
(rhs.
elements
, this->elements, rhs.
size
*
sizeof
(TYPE));
196
197
this->
grow
= rhs.
grow
;
198
this->
size
= rhs.
size
;
199
}
200
else
201
{
202
// source array doesn't fit into our capacity, need to reallocate
203
this->
Delete
();
204
this->
Copy
(rhs);
205
}
206
}
207
}
208
209
//------------------------------------------------------------------------------
212
template
<
class
TYPE>
void
213
TrivialArray<TYPE>::operator=
(
const
Array<TYPE>
& rhs)
214
{
215
if
(
this
!= &rhs)
216
{
217
if
((this->
capacity
> 0) && (rhs.
size
<= this->capacity))
218
{
219
// source array fits into our capacity, copy in place
220
n_assert
(0 != this->
elements
);
221
Memory::Copy
(rhs.
elements
, this->elements, rhs.
size
*
sizeof
(TYPE));
222
223
this->
grow
= rhs.
grow
;
224
this->
size
= rhs.
size
;
225
}
226
else
227
{
228
// source array doesn't fit into our capacity, need to reallocate
229
this->
Delete
();
230
this->
Copy
(rhs);
231
}
232
}
233
}
234
//------------------------------------------------------------------------------
237
template
<
class
TYPE>
void
238
TrivialArray<TYPE>::GrowTo
(
SizeT
newCapacity)
239
{
240
TYPE* newArray =
new
TYPE[newCapacity];
241
if
(this->
elements
)
242
{
243
Memory::Copy
(this->
elements
, newArray, this->
size
*
sizeof
(TYPE));
244
// discard old array
245
delete
[] this->
elements
;
246
}
247
this->
elements
= newArray;
248
this->
capacity
= newCapacity;
249
}
250
251
//------------------------------------------------------------------------------
256
template
<
class
TYPE>
void
257
TrivialArray<TYPE>::Move
(
IndexT
fromIndex,
IndexT
toIndex)
258
{
259
#if NEBULA_BOUNDSCHECKS
260
n_assert
(this->
elements
);
261
n_assert
(fromIndex < this->
size
);
262
#endif
263
264
// nothing to move?
265
if
(fromIndex == toIndex)
266
{
267
return
;
268
}
269
270
// compute number of elements to move
271
SizeT
num = this->size - fromIndex;
272
273
// check if array needs to grow
274
SizeT
neededSize = toIndex + num;
275
while
(neededSize > this->
capacity
)
276
{
277
this->
Grow
();
278
}
279
280
//TODO: check if memory move is faster than this
281
if
(fromIndex > toIndex)
282
{
283
// this is a backward move
284
IndexT
i;
285
for
(i = 0; i < num; i++)
286
{
287
this->
elements
[toIndex + i] = this->
elements
[fromIndex + i];
288
}
289
}
290
else
291
{
292
// this is a forward move
293
int
i;
// NOTE: this must remain signed for the following loop to work!!!
294
for
(i = num - 1; i >= 0; --i)
295
{
296
this->
elements
[toIndex + i] = this->
elements
[fromIndex + i];
297
}
298
}
299
300
// adjust array size
301
this->size = toIndex + num;
302
}
303
304
//------------------------------------------------------------------------------
307
template
<
class
TYPE>
void
308
TrivialArray<TYPE>::EraseIndex
(
IndexT
index)
309
{
310
#if NEBULA_BOUNDSCHECKS
311
n_assert
(this->
elements
&& (index < this->
size
));
312
#endif
313
if
(index == (this->size - 1))
314
{
315
// special case: last element
316
this->size--;
317
}
318
else
319
{
320
this->
Move
(index + 1, index);
321
}
322
}
323
324
//------------------------------------------------------------------------------
328
template
<
class
TYPE>
void
329
TrivialArray<TYPE>::EraseIndexSwap
(
IndexT
index)
330
{
331
#if NEBULA_BOUNDSCHECKS
332
n_assert
(this->
elements
&& (index < this->
size
));
333
#endif
334
335
// swap with last element, and destroy last element
336
IndexT
lastElementIndex = this->size - 1;
337
if
(index < lastElementIndex)
338
{
339
this->
elements
[index] = this->
elements
[lastElementIndex];
340
}
341
this->size--;
342
}
343
344
//------------------------------------------------------------------------------
349
template
<
class
TYPE>
void
350
TrivialArray<TYPE>::Clear
()
351
{
352
this->
size
= 0;
353
}
354
355
}
// namespace Util
356
//------------------------------------------------------------------------------
array.h
Util::Array::capacity
SizeT capacity
Definition
array.h:264
Util::Array::elements
TYPE * elements
Definition
array.h:266
Util::Array::grow
SizeT grow
Definition
array.h:263
Util::Array::Delete
void Delete()
delete content
Definition
array.h:440
Util::Array::Grow
void Grow()
grow array with grow value
Definition
array.h:600
Util::Array::size
size_t size() const
Definition
array.h:1770
Util::Array::Array
Array()
constructor with default parameters
Definition
array.h:275
Util::TrivialArray::Iterator
TYPE * Iterator
define iterator
Definition
trivialarray.h:22
Util::TrivialArray::Copy
void Copy(const TrivialArray< TYPE > &src)
copy content
Definition
trivialarray.h:130
Util::TrivialArray::~TrivialArray
~TrivialArray()
destructor
Definition
trivialarray.h:178
Util::TrivialArray::Move
void Move(IndexT fromIndex, IndexT toIndex)
move elements, grows array if needed
Definition
trivialarray.h:257
Util::TrivialArray::EraseIndexSwap
void EraseIndexSwap(IndexT index)
erase element at index, fill gap by swapping in last element, destroys sorting!
Definition
trivialarray.h:329
Util::TrivialArray::Clear
void Clear()
clear array (calls destructors)
Definition
trivialarray.h:350
Util::TrivialArray::EraseIndex
void EraseIndex(IndexT index)
erase element at index, keep sorting intact
Definition
trivialarray.h:308
Util::TrivialArray::TrivialArray
TrivialArray()
constructor with default parameters
Definition
trivialarray.h:69
Util::TrivialArray::GrowTo
void GrowTo(SizeT newCapacity)
grow array to target size
Definition
trivialarray.h:238
Util::TrivialArray::operator=
void operator=(const TrivialArray< TYPE > &rhs)
assignment operator
Definition
trivialarray.h:187
Util::TrivialArray::Destroy
void Destroy(TYPE *elm)
does nothing
Definition
trivialarray.h:169
n_assert
#define n_assert(exp)
Definition
debug.h:50
Memory::Copy
void Copy(const void *from, void *to, size_t numBytes)
Copy a chunk of memory (note the argument order is different from memcpy()!
Definition
osxmemory.cc:213
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
trivialarray.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.