1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
// This code is in the public domain -- Ignacio Casta�o <castano@gmail.com>
#pragma once
#ifndef NV_CORE_ARRAY_INL
#define NV_CORE_ARRAY_INL
#include "Array.h"
#include "Stream.h"
#include "Utils.h" // swap
#include <string.h> // memmove
#include <new> // for placement new
namespace nv
{
template <typename T>
NV_FORCEINLINE T & Array<T>::append()
{
uint old_size = m_size;
uint new_size = m_size + 1;
setArraySize(new_size);
construct_range(m_buffer, new_size, old_size);
return m_buffer[old_size]; // Return reference to last element.
}
// Push an element at the end of the vector.
template <typename T>
NV_FORCEINLINE void Array<T>::push_back( const T & val )
{
#if 1
nvDebugCheck(&val < m_buffer || &val >= m_buffer+m_size);
uint old_size = m_size;
uint new_size = m_size + 1;
setArraySize(new_size);
construct_range(m_buffer, new_size, old_size, val);
#else
uint new_size = m_size + 1;
if (new_size > m_capacity)
{
// @@ Is there any way to avoid this copy?
// @@ Can we create a copy without side effects? Ie. without calls to constructor/destructor. Use alloca + memcpy?
// @@ Assert instead of copy?
const T copy(val); // create a copy in case value is inside of this array.
setArraySize(new_size);
new (m_buffer+new_size-1) T(copy);
}
else
{
m_size = new_size;
new(m_buffer+new_size-1) T(val);
}
#endif // 0/1
}
template <typename T>
NV_FORCEINLINE void Array<T>::pushBack( const T & val )
{
push_back(val);
}
template <typename T>
NV_FORCEINLINE Array<T> & Array<T>::append( const T & val )
{
push_back(val);
return *this;
}
// Qt like push operator.
template <typename T>
NV_FORCEINLINE Array<T> & Array<T>::operator<< ( T & t )
{
push_back(t);
return *this;
}
// Pop the element at the end of the vector.
template <typename T>
NV_FORCEINLINE void Array<T>::pop_back()
{
nvDebugCheck( m_size > 0 );
resize( m_size - 1 );
}
template <typename T>
NV_FORCEINLINE void Array<T>::popBack(uint count)
{
nvDebugCheck(m_size >= count);
resize(m_size - count);
}
template <typename T>
NV_FORCEINLINE void Array<T>::popFront(uint count)
{
nvDebugCheck(m_size >= count);
//resize(m_size - count);
if (m_size == count) {
clear();
}
else {
destroy_range(m_buffer, 0, count);
memmove(m_buffer, m_buffer + count, sizeof(T) * (m_size - count));
m_size -= count;
}
}
// Get back element.
template <typename T>
NV_FORCEINLINE const T & Array<T>::back() const
{
nvDebugCheck( m_size > 0 );
return m_buffer[m_size-1];
}
// Get back element.
template <typename T>
NV_FORCEINLINE T & Array<T>::back()
{
nvDebugCheck( m_size > 0 );
return m_buffer[m_size-1];
}
// Get front element.
template <typename T>
NV_FORCEINLINE const T & Array<T>::front() const
{
nvDebugCheck( m_size > 0 );
return m_buffer[0];
}
// Get front element.
template <typename T>
NV_FORCEINLINE T & Array<T>::front()
{
nvDebugCheck( m_size > 0 );
return m_buffer[0];
}
// Check if the given element is contained in the array.
template <typename T>
NV_FORCEINLINE bool Array<T>::contains(const T & e) const
{
return find(e, NULL);
}
// Return true if element found.
template <typename T>
NV_FORCEINLINE bool Array<T>::find(const T & element, uint * indexPtr) const
{
return find(element, 0, m_size, indexPtr);
}
// Return true if element found within the given range.
template <typename T>
NV_FORCEINLINE bool Array<T>::find(const T & element, uint begin, uint end, uint * indexPtr) const
{
return ::nv::find(element, m_buffer, begin, end, indexPtr);
}
// Remove the element at the given index. This is an expensive operation!
template <typename T>
void Array<T>::removeAt(uint index)
{
nvDebugCheck(index >= 0 && index < m_size);
if (m_size == 1) {
clear();
}
else {
m_buffer[index].~T();
memmove(m_buffer+index, m_buffer+index+1, sizeof(T) * (m_size - 1 - index));
m_size--;
}
}
// Remove the first instance of the given element.
template <typename T>
bool Array<T>::remove(const T & element)
{
uint index;
if (find(element, &index)) {
removeAt(index);
return true;
}
return false;
}
// Insert the given element at the given index shifting all the elements up.
template <typename T>
void Array<T>::insertAt(uint index, const T & val/*=T()*/)
{
nvDebugCheck( index >= 0 && index <= m_size );
setArraySize(m_size + 1);
if (index < m_size - 1) {
memmove(m_buffer+index+1, m_buffer+index, sizeof(T) * (m_size - 1 - index));
}
// Copy-construct into the newly opened slot.
new(m_buffer+index) T(val);
}
// Append the given data to our vector.
template <typename T>
NV_FORCEINLINE void Array<T>::append(const Array<T> & other)
{
append(other.m_buffer, other.m_size);
}
// Append the given data to our vector.
template <typename T>
void Array<T>::append(const T other[], uint count)
{
if (count > 0) {
const uint old_size = m_size;
setArraySize(m_size + count);
for (uint i = 0; i < count; i++ ) {
new(m_buffer + old_size + i) T(other[i]);
}
}
}
// Remove the given element by replacing it with the last one.
template <typename T>
void Array<T>::replaceWithLast(uint index)
{
nvDebugCheck( index < m_size );
nv::swap(m_buffer[index], back()); // @@ Is this OK when index == size-1?
(m_buffer+m_size-1)->~T();
m_size--;
}
// Resize the vector preserving existing elements.
template <typename T>
void Array<T>::resize(uint new_size)
{
uint old_size = m_size;
// Destruct old elements (if we're shrinking).
destroy_range(m_buffer, new_size, old_size);
setArraySize(new_size);
// Call default constructors
construct_range(m_buffer, new_size, old_size);
}
// Resize the vector preserving existing elements and initializing the
// new ones with the given value.
template <typename T>
void Array<T>::resize(uint new_size, const T & elem)
{
nvDebugCheck(&elem < m_buffer || &elem > m_buffer+m_size);
uint old_size = m_size;
// Destruct old elements (if we're shrinking).
destroy_range(m_buffer, new_size, old_size);
setArraySize(new_size);
// Call copy constructors
construct_range(m_buffer, new_size, old_size, elem);
}
// Fill array with the given value.
template <typename T>
void Array<T>::fill(const T & elem)
{
fill(m_buffer, m_size, elem);
}
// Clear the buffer.
template <typename T>
NV_FORCEINLINE void Array<T>::clear()
{
nvDebugCheck(isValidPtr(m_buffer));
// Destruct old elements
destroy_range(m_buffer, 0, m_size);
m_size = 0;
}
// Shrink the allocated vector.
template <typename T>
NV_FORCEINLINE void Array<T>::shrink()
{
if (m_size < m_capacity) {
setArrayCapacity(m_size);
}
}
// Preallocate space.
template <typename T>
NV_FORCEINLINE void Array<T>::reserve(uint desired_size)
{
if (desired_size > m_capacity) {
setArrayCapacity(desired_size);
}
}
// Copy elements to this array. Resizes it if needed.
template <typename T>
NV_FORCEINLINE void Array<T>::copy(const T * data, uint count)
{
#if 1 // More simple, but maybe not be as efficient?
destroy_range(m_buffer, 0, m_size);
setArraySize(count);
construct_range(m_buffer, count, 0, data);
#else
const uint old_size = m_size;
destroy_range(m_buffer, count, old_size);
setArraySize(count);
copy_range(m_buffer, data, old_size);
construct_range(m_buffer, count, old_size, data);
#endif
}
// Assignment operator.
template <typename T>
NV_FORCEINLINE Array<T> & Array<T>::operator=( const Array<T> & a )
{
copy(a.m_buffer, a.m_size);
return *this;
}
// Release ownership of allocated memory and returns pointer to it.
template <typename T>
T * Array<T>::release() {
T * tmp = m_buffer;
m_buffer = NULL;
m_capacity = 0;
m_size = 0;
return tmp;
}
// Change array size.
template <typename T>
inline void Array<T>::setArraySize(uint new_size) {
m_size = new_size;
if (new_size > m_capacity) {
uint new_buffer_size;
if (m_capacity == 0) {
// first allocation is exact
new_buffer_size = new_size;
}
else {
// following allocations grow array by 25%
new_buffer_size = new_size + (new_size >> 2);
}
setArrayCapacity( new_buffer_size );
}
}
// Change array capacity.
template <typename T>
inline void Array<T>::setArrayCapacity(uint new_capacity) {
nvDebugCheck(new_capacity >= m_size);
if (new_capacity == 0) {
// free the buffer.
if (m_buffer != NULL) {
free<T>(m_buffer);
m_buffer = NULL;
}
}
else {
// realloc the buffer
m_buffer = realloc<T>(m_buffer, new_capacity);
}
m_capacity = new_capacity;
}
// Array serialization.
template <typename Typ>
inline Stream & operator<< ( Stream & s, Array<Typ> & p )
{
if (s.isLoading()) {
uint size;
s << size;
p.resize( size );
}
else {
s << p.m_size;
}
for (uint i = 0; i < p.m_size; i++) {
s << p.m_buffer[i];
}
return s;
}
// Swap the members of the two given vectors.
template <typename Typ>
inline void swap(Array<Typ> & a, Array<Typ> & b)
{
nv::swap(a.m_buffer, b.m_buffer);
nv::swap(a.m_capacity, b.m_capacity);
nv::swap(a.m_size, b.m_size);
}
} // nv namespace
// IC: These functions are for compatibility with the Foreach macro in The Witness.
template <typename T> inline int item_count(const nv::Array<T> & array) { return array.count(); }
template <typename T> inline const T & item_at(const nv::Array<T> & array, int i) { return array.at(i); }
template <typename T> inline T & item_at(nv::Array<T> & array, int i) { return array.at(i); }
template <typename T> inline int item_advance(const nv::Array<T> & array, int i) { return ++i; }
template <typename T> inline int item_remove(nv::Array<T> & array, int i) { array.replaceWithLast(i); return i - 1; }
template <typename T> inline int item_count(const nv::Array<T> * array) { return array->count(); }
template <typename T> inline const T & item_at(const nv::Array<T> * array, int i) { return array->at(i); }
template <typename T> inline T & item_at(nv::Array<T> * array, int i) { return array->at(i); }
template <typename T> inline int item_advance(const nv::Array<T> * array, int i) { return ++i; }
template <typename T> inline int item_remove(nv::Array<T> * array, int i) { array->replaceWithLast(i); return i - 1; }
#endif // NV_CORE_ARRAY_INL
|