-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ngen.Array.hpp
473 lines (394 loc) · 13.8 KB
/
Ngen.Array.hpp
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* _______ ________
\ \ / _____/ ____ ___
/ | \/ \ ____/ __ \ / \
/ | \ \_\ \ ___/| | \
\____|__ /\______ /\___ >___| /
\/ \/ \/ \/
The MIT License (MIT)
COPYRIGHT (C) 2016 RAZORWARE, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef __NGEN_ARRAY_HPP
#define __NGEN_ARRAY_HPP
#include "Ngen.Delegate.hpp"
namespace Ngen {
/** @brief A contiguous data structure for holding items contiguously in memory. */
template<typename T> class Array { // : public virtual IContainer<uword, T> {
public:
/** @brief A type-definition of the type without template decorations. */
typedef Array<T> TSelf;
/** @brief Constructor. Default. */
Array() : mData(null), mLength(0), mCapacity(0), mIsReadonly(false) {}
/** @brief Constructor. Initialize.
* @param capacity The number of structures to pre-allocate for the array.
*/
Array(uword capacity) : mData(Memory::New<T>(capacity)), mLength(0), mCapacity(capacity), mIsReadonly(false) {}
/** @brief Constructor. (T*, uword, bool)
* @param copy The pointer where the data structures will be referenced to create the array.
* @param length The number of structures being copied.
* @param readOnly Determines if the array will copy the data structures or only reference them.
*/
Array(std::initializer_list<T> copy, bool readOnly = true) : mData(null), mLength(0), mCapacity(0), mIsReadonly(readOnly) {
Set((T*)copy.begin(), copy.size(), readOnly);
}
/** @brief Constructor. (T*, uword, bool)
* @param copy The pointer where the data structures will be referenced to create the array.
* @param length The number of structures being copied.
* @param readOnly Determines if the array will copy the data structures or only reference them.
*/
Array(T* copy, uword length, bool readOnly = true) : mData(null), mLength(0), mCapacity(0), mIsReadonly(readOnly) {
Set(copy, length, readOnly);
}
/** @brief Constructor. Copy. (const TSelf&)
* @param copy The array to copy elements from.
*/
Array(const TSelf& copy) : mData(null), mLength(0), mCapacity(0), mIsReadonly(false) {
Set(copy.mData, copy.mLength, copy.mCapacity, copy.mIsReadonly);
}
/** @brief Constructor. Copy. (const TSelf&, bool)
* @param copy The array to copy elements from.
* @param readOnly Determines if the array will copy the data structures or only reference them.
*/
Array(const TSelf& copy, bool readOnly) : mData(null), mLength(0), mCapacity(0), mIsReadonly(readOnly) {
Set(copy.mData, copy.mLength, copy.mCapacity, readOnly);
}
/** @brief Constructor. Move. (TSelf&&) */
Array(TSelf&& move) : mData(move.mData), mLength(move.mLength), mCapacity(move.mCapacity), mIsReadonly(move.mIsReadonly) {
if(!move.mIsReadonly) {
move.mData = null;
move.mLength = 0;
move.mCapacity = 0;
}
}
/** @brief De-constructor. */
~Array() {
pClear(true);
}
/** @brief operator[](uword).*/
const T& operator[](uword index) const {
if(IsNullOrEmpty()) {
THROW(InvalidOperationException("Cannot index an array that is null or empty!"));
} else if(index >= mLength) {
THROW(OutOfRangeException("The parameter 'index' must be less than the length of the array!"));
}
return *(mData + index);
}
/** @brief operator[](uword).*/
T& operator[](uword index) {
if(IsNullOrEmpty()) {
THROW(InvalidOperationException("Cannot index an array that is null or empty!"));
} else if(index >= mLength) {
THROW(OutOfRangeException("The parameter 'index' must be less than the length of the array!"));
}
return *(mData + index);
}
/** @brief operator+(const TSelf&)*/
TSelf operator+(const TSelf& rhs) const {
TSelf result = TSelf(*this, false);
result.AddRange(rhs);
return TSelf((TSelf&&)result);
}
/** @brief operator+=(const TSelf&)*/
TSelf& operator+=(const TSelf& rhs) const {
this->AddRange(rhs);
return *this;
}
/** @brief operator+(const T&)*/
TSelf operator+(const T& rhs) const {
TSelf result = TSelf(*this, false);
result.Add(rhs);
return TSelf((TSelf&&)result);
}
/** @brief operator+=(const T&)*/
TSelf& operator+=(const T& rhs) const {
this->Add(rhs);
return *this;
}
/** @brief Re-initializes the array using the given parameters.
* @param data A pointer to the data structures being referenced.
* @param length The number of data structures to copy.
* @param readOnly Determines if the data structures will be copied or only referenced.
*/
void Set(T* data, uword length, bool readOnly = false) {
this->Set(data, length, 0, readOnly);
}
/** @brief Re-initializes the array using the given parameters.
* @param data A pointer to the data structures being referenced.
* @param length The number of data structures to copy.
* @param capacity The total number of structures to reserve space for. Must be greater-than length or zero.
* @param readOnly Determines if the data structures will be copied or only referenced.
*/
void Set(T* data, uword length, uword capacity, bool readOnly = false) {
if(data == null) {
THROW(InvalidParameterException("The parameter 'data' cannot be null!"));
} else if(length == 0) {
THROW(InvalidParameterException("The parameter 'length' cannot be a zero!"));
} else if(capacity != 0 && length > capacity) {
THROW(InvalidParameterException("The parameter 'length' must be less-than or equal-to the parameter 'capacity'!"));
}
pClear(true);
mIsReadonly = readOnly;
mLength = length;
mCapacity = capacity == 0 ? length : capacity;
if(mIsReadonly) {
mData = data;
} else {
pReallocate(mCapacity, true);
T* begin = mData;
do {
*begin = *data++;
} while(End() != begin++);
}
}
/** @brief Clears all resources used by the array. */
void Clear() {
this->pClear(false);
}
/** @brief Gets the number of items in the array. */
uword Length() const {
return mLength;
}
/** @brief Gets the pre-allocated capacity of the array. */
uword Capacity() const {
return mCapacity;
}
/** @param Gets a value that determines if the array contains null data. */
bool IsNullOrEmpty() const {
return isnull(mData) || mLength == 0;
}
/** @brief Determines if the array is empty.
* @param length [inref] A place to store the length of the array.
*/
bool IsEmpty(uword& length) const {
length = mLength;
return IsNullOrEmpty();
}
/** @brief Get an iterator beginning at the given index. */
T* Begin(uword at = 0) const {
if(at >= mLength) {
THROW(InvalidParameterException("The parameter 'at' must be less-than the length of the array!"));
}
return mData + at;
}
/** @brief Gets an iterator pointing at the last item in the array. */
T* End() const {
return mData + mLength-1;
}
/** @brief Adds item to the end of the array. */
void Add(const T& item) {
pReallocate(mLength + 1);
memory::Place(mData + mLength, item);
mLength++;
}
/** @brief Adds an array of items to the end of the array. */
void AddRange(const TSelf& rhs) {
if(rhs.IsNullOrEmpty()) {
return;
}
pReallocate(mLength + rhs.mLength);
T* begin = Begin(mLength);
T* rhsp = rhs.mData;
mLength += rhs.mLength;
do {
*begin = *rhsp++;
} while(End() != begin++);
}
/** @brief Removes the first occurrence of an item from the array.
* @param item The data structure to use as a matching predicate for the item being removed.
*/
void Remove(const T& item) {
if(IsNullOrEmpty()) {
THROW(InvalidOperationException("Unable to remove items from a null or empty array!"));
}
T* begin = Begin();
do {
if(*begin == item) {
break;
}
} while(End() != begin++);
while(begin != End()) {
*begin = *(begin+1);
++begin;
}
mLength--;
}
/** @brief Removes the first occurrence of an item from the array.
* @param item The data structure to use as a matching predicate for the item being removed.
*/
void RemoveAt(uword index) {
if(IsNullOrEmpty()) {
THROW(InvalidOperationException("Unable to remove items from a null or empty array!"));
} // index > length !!
T* begin = Begin(index);
while(End() != begin) {
*begin = *(begin+1);
++begin;
}
mLength--;
}
/** @brief Counts the number of items in the array matching the given item.
* @param item The data structure to use as a matching predicate for the item being counted.
*/
uword Count(const T& item) const {
uword result = 0;
T* begin = mData;
do {
if(*begin == item) {
++result;
}
} while(End() != begin++);
return result;
}
bool Contains(const T& item) {
T* begin = mData;
do {
if(*begin == item) {
return true;
}
} while(End() != begin++);
return false;
}
/** @brief Iterates over each item and allocates them into a new collection based on a given predicate.
* @param predicate A function or lambda used to filter out unwanted items.
* @return An array of items that was collected after being filter using the predicate.
*/
TSelf Where(typename StaticDelegate<bool, T&>::TFunction predicate) {
if(this->IsNullOrEmpty()) {
return Array<T&>();
}
TSelf result = TSelf(1);
for(uword i = 0; i < mLength; ++i) {
if(predicate(Begin(i))) {
result.Add(*Begin(i));
}
}
return TSelf((TSelf&&)result);
}
/** @brief Iterates over each item and allocates selected data into a new collection based on a different data type.
* @param selector A function or lambda used to gather the required data.
* @return An array of items that was collected after being transformed by using the selector.
*/
template<typename TSelect> Array<TSelect> Select(typename StaticDelegate<TSelect, T&>::TFunction selector) {
if(this->IsNullOrEmpty()) {
return Array<TSelect>();
}
TSelf result = TSelf(1);
for(uword i = 0; i < mLength; ++i) {
result.Add(selector(*(mData+i)));
}
return Array<TSelect>((Array<TSelect>&&)result);
}
/** @brief Gets the last item in the array, or will throw an exception if the array does contain multiple items.
*/
T& Last() {
pThrowIfNullOrEmpty();
if(mLength == 1) {
THROW(InvalidOperationException("The array only contains a single item."));
}
return *(End());
}
/** @brief Gets the first item in the array.
*/
T& First() {
pThrowIfNullOrEmpty();
return *(Begin());
}
/** @brief Gets the only item in the array, or will throw an exception if the array contains multiple items.
*/
T& Single() {
pThrowIfNullOrEmpty();
if(mLength != 1) {
THROW(InvalidOperationException("The array contains multiple entries."));
}
return *(mData);
}
template<typename TCast> Array<TCast> AsType() const {
Array<TCast> result = Array<TCast>();
for(uword i = 0; i < this->mLength; ++i) {
result.Add((TCast)*(this->Begin(i)));
}
return Array<TCast>((Array<TCast>&&)result);
}
template<typename TCast> Array<TCast> AsType(typename StaticDelegate<TCast, T&>::TFunction caster) const {
Array<TCast> result = Array<TCast>();
for(uword i = 0; i < this->mLength; ++i) {
result.Add(caster(*this->Begin(i)));
}
return Array<TCast>((Array<TCast>&&)result);
}
Array<T*> ToPointerArray() const {
auto result = Array<T*>(mLength);
for(uword i = 0; i < this->mLength; ++i) {
result.Add(this->Begin(i));
}
return Array<T*>((Array<T*>&&)result);
}
protected:
/** @brief Clears the items from the array, with an option to ignore read-only errors.
* @param ignoreReadOnlyError Determines if the read-only flag should be checked before clearing the items.
*/
void pClear(bool ignoreReadOnlyError) {
if(!ignoreReadOnlyError) {
pThrowIfReadonly();
}
if(!mIsReadonly) {
if(!isnull(mData) && mLength != 0) {
memory::Delete<T>(mData);
mLength = 0;
mCapacity = 0;
mData = null;
}
}
}
/** @brief Throws an InvalidOperationException if the array is read-only. */
void pThrowIfReadonly() const {
if(mIsReadonly) {
throw InvalidOperationException("Cannot manipulate a read-only array!");
}
}
/** @brief Throws an InvalidOperationException if the array is read-only. */
void pThrowIfNullOrEmpty() const {
if(mLength == 0 || isnull(mData)) {
throw InvalidOperationException("The array is null or empty.");
}
}
/** @brief Reallocates the array to a new capacity. */
void pReallocate(uword newCapacity, bool ignoreReadonly = true) {
if(!ignoreReadonly) {
pThrowIfReadonly();
}
if(!mIsReadonly) {
if(newCapacity == mCapacity) {
return;
} else if(newCapacity > mCapacity) {
mCapacity = newCapacity;
T* begin = mData;
mData = Memory::New<T>(mCapacity);
memory::Copy(begin, mData, mLength);
if(!isnull(begin) && mLength > 0) {
memory::Delete(begin);
}
}
}
}
T* mData;
uword mLength;
uword mCapacity;
bool mIsReadonly;
};
}
#endif // __NGEN_ARRAY_HPP