-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.hpp
More file actions
489 lines (413 loc) · 11.7 KB
/
Array.hpp
File metadata and controls
489 lines (413 loc) · 11.7 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/********************************************************************************
* Purpose: *
* Simple Dynamic array data structure. *
* Author: *
* Anilcan Gulkaya 2023 anilcangulkaya7@gmail.com github @benanil *
********************************************************************************/
#pragma once
#include "Memory.hpp"
#include "Algorithms.hpp"
AX_NAMESPACE
template<typename ValueT,
typename AllocatorT = MallocAllocator<ValueT>>
struct Array
{
using Iterator = ValueT*;
using ConstIterator = const ValueT*;
static const int InitialSize = AllocatorT::InitialSize;
// all variables are public but:
// don't change this variables if you don't know what are you doing
ValueT* arr = nullptr;
int m_count = 0;
int m_capacity = 0;
AllocatorT allocator{};
Array() : arr(nullptr), m_count(0), m_capacity(0), allocator()
{ }
~Array()
{
Clear();
}
explicit Array(const AllocatorT& _allocator) : arr(nullptr), m_count(0), m_capacity(0), allocator(_allocator)
{ }
explicit Array(Array const& other) : Array(other.Data(), other.Size()) {}
explicit Array(int _capacity) : m_capacity(_capacity)
{
if (_capacity == 0) return;
arr = allocator.AllocateUninitialized(m_capacity);
}
Array(int _capacity, int _count)
: m_capacity(_capacity), m_count(_count)
{
arr = allocator.Allocate(m_capacity);
}
Array(int _count, const ValueT& val)
: m_capacity(_count + (_count / 2)), m_count(_count)
{
arr = allocator.AllocateUninitialized(m_capacity);
FillN(arr, val, m_count);
}
Array(Iterator _begin, Iterator _end)
{
m_count = PointerDistance(_begin, _end);
m_capacity = m_count + (m_count / 2);
arr = allocator.AllocateUninitialized(m_capacity);
MoveArray(arr, _begin, m_count);
}
Array(ConstIterator _begin, ConstIterator _end)
{
m_count = PointerDistance(_begin, _end);
m_capacity = m_count + (m_count / 2);
arr = allocator.AllocateUninitialized(m_capacity);
Copy(arr, _begin, m_count);
}
Array(ConstIterator _begin, int _size)
: m_count(_size), m_capacity(_size + (_size / 2))
{
arr = allocator.AllocateUninitialized(m_capacity);
Copy(arr, _begin, _size);
}
Array& operator = (Array const& other)
{
if (&other != this)
{
GrowIfNecessary(other.Size());
Copy(arr, other.arr, other.Size());
m_count = other.Size();
}
return *this;
}
Array& operator = (Array&& other)
{
if (&other != this)
{
if (arr != nullptr)
allocator.Deallocate(arr, m_capacity);
arr = other.arr;
allocator = other.allocator;
m_count = other.Size();
m_capacity = other.m_capacity;
other.m_capacity = 0;
other.m_count = 0;
other.arr = nullptr;
}
return *this;
}
bool operator == (const Array& other) const
{
if (&other == this) return true;
if (other.Size() != Size()) return false;
for (int i = 0; i < other.Size(); ++i)
{
if (arr[i] != other.arr[i])
return false;
}
return true;
}
bool operator != (const Array& other) const
{
return !(other == *this);
}
ValueT& operator[](int index)
{
ASSERTR(index >= 0 && index < m_count, return arr[0]);
return arr[index];
}
ValueT& GetUnchecked(int index)
{
return arr[index];
}
const ValueT& GetUnchecked(int index) const
{
return arr[index];
}
const ValueT& operator[](int index) const
{
ASSERTR(index >= 0 && index < m_count, return arr[0]);
return arr[index];
}
Array operator + (const ValueT& other) const { Array _new = *this; _new.Add(other); return _new; }
Array& operator += (const ValueT& type) { Add(type); return *this; }
Array& operator += (const Array& other) { Add(other.cbegin(), other.Size()); return *this; }
// you should know what are you doing if you want to use this function
// takes the ownership of the data, which means you need to make deallocation
ValueT* TakeOwnership()
{
m_capacity = m_count = 0;
ValueT *res = arr;
arr = nullptr;
return res;
}
void Add(const ValueT& value)
{
GrowIfNecessary(m_count + 1);
arr[m_count++] = value;
}
void Add(ConstIterator _begin, int _size)
{
ASSERTR(m_count < INT32_MAX - _size, return); // array max size reached
GrowIfNecessary(m_count + _size);
for (int i = 0; i < _size; i++)
{
arr[m_count++] = *_begin++;
}
}
void AddUninitialized(int _size)
{
ASSERTR(m_count < INT32_MAX - _size, return); // array max size reached
GrowIfNecessary(m_count + _size);
m_count += _size;
}
void Add(ConstIterator _begin, ConstIterator _end)
{
Add(_begin, PointerDistance(_begin, _end));
}
void Insert(int index, const ValueT& value)
{
OpenSpace(index, 1);
arr[index] = value;
}
void Insert(ConstIterator _it, const ValueT* _begin, const ValueT* _end)
{
Insert(PointerDistance(begin(), _it), _begin, _end);
}
void Insert(int index, const ValueT* _begin, const ValueT* _end)
{
uint size = PointerDistance(_begin, _end);
OpenSpace(index, size);
Copy(arr, _begin, size);
}
void InsertUnordered(int index, const ValueT& value)
{
GrowIfNecessary(m_count + 1);
arr[m_count++] = (ValueT&&)arr[index];
arr[index] = value;
}
typedef bool(*RemoveFunc)(const ValueT& val);
// returns number of elements removed
int RemoveAll(RemoveFunc match)
{
int freeIndex = 0; // the first free slot in items array
// Find the first item which needs to be removed.
while (freeIndex < m_count && match(arr[freeIndex]) == false) freeIndex++;
if (freeIndex >= m_count) return 0;
int current = freeIndex + 1;
while (current < m_count)
{
// Find the first item which needs to be kept.
while (current < m_count && match(arr[current]) == true) current++;
if (current < m_count)
{
// copy item to the free slot.
arr[freeIndex++] = Forward<ValueT>(arr[current++]);
}
}
int numCleared = m_count - freeIndex;
ClearN(arr + freeIndex, numCleared);
m_count = freeIndex;
return numCleared; // removed item count
}
template<typename... Args>
ValueT& EmplaceAt(int index, Args&&... args)
{
ASSERTR(m_count != INT32_MAX, return arr[index])
OpenSpace(index, 1);
arr[index].~ValueT();
ValueT val(Forward<Args>(args)...);
arr[index] = (ValueT&&)val;
return arr[index];
}
template<typename... Args>
ValueT& EmplaceBack(Args&&... args)
{
ASSERTR(m_count != INT32_MAX, return arr[m_count-1])
GrowIfNecessary(m_count + 1);
ValueT val(Forward<Args>(args)...);
arr[m_count] = (ValueT&&)val;
return arr[m_count++];
}
void Reset()
{
m_count = 0;
}
void Clear()
{
if (arr != nullptr)
{
allocator.Deallocate(arr, m_capacity);
m_count = 0;
m_capacity = 0;
arr = nullptr;
}
}
void RemoveAt(Iterator _it, int _count = 1)
{
RemoveSpace(PointerDistance(begin(), _it), _count);
}
void RemoveAt(int _index, int _count = 1)
{
RemoveSpace(_index, _count);
}
void RemoveBack() { RemoveSpace(m_count - 1, 1); }
void Remove(const ValueT& val)
{
int index = IndexOf(arr, val, m_count);
if (index != -1)
{
RemoveSpace(index, 1);
}
}
// faster than remove
void RemoveUnordered(int index)
{
arr[index].~ValueT();
Swap(arr[index], arr[--m_count]);
}
void Reserve(int _size)
{
if (_size > m_capacity)
{
GrowIfNecessary(_size);
}
}
void Resize(int _size)
{
if (_size > m_capacity)
{
if (arr) // array can be nullptr (first initialization)
arr = allocator.Reallocate(arr, m_capacity, _size);
else
arr = allocator.Allocate(_size);
m_capacity = _size;
}
m_count = _size;
}
// you can use this function after removing elements in the array, this will reduct the amount of memory used
void ShrinkToFit()
{
int newCapacity = CalculateArrayGrowth(m_count);
if (m_capacity > newCapacity)
{
arr = allocator.Reallocate(arr, m_capacity, newCapacity);
}
}
const ValueT& Back() const { return arr[m_count - 1]; }
ValueT& Back() { return arr[m_count - 1]; }
void PopBack() { arr[--m_count].~ValueT(); }
void PushBack(const ValueT& val) { Add(val); }
const ValueT& Front() const { return arr[0]; }
ValueT& Front() { return arr[0]; }
void PopFront() { RemoveAt(0); }
int Size() const { return m_count; }
int Capacity() const { return m_capacity; }
ValueT* Data() { return arr; }
const ValueT* Data() const { return arr; }
bool Empty() const { return m_count == 0; }
ConstIterator cbegin() const { return arr; }
ConstIterator cend() const { return arr + m_count; }
ConstIterator end() const { return arr + m_count; }
ConstIterator begin() const { return arr; }
Iterator begin() { return arr; }
Iterator end() { return arr + m_count; }
#ifdef ASTL_STL_COMPATIBLE
using iterator = ValueT*;
using const_iterator = const ValueT*;
void erase(Iterator _it, int _count = 1) { Remove(_it, _count); }
void insert(int index, const ValueT& value) { Insert(index, value); }
void push_back(const ValueT& value) { Add(value); }
template<typename... Args>
ValueT& emplace_back(Args&&... args)
{
return EmplaceBack(Forward<Args>(args)...);
}
int size() const { return m_count; }
int capacity() const { return m_capacity; }
ValueT* data() { return arr; }
const ValueT* data() const { return arr; }
bool empty() const { return size == 0; }
const ValueT& back() const { return arr[m_count - 1]; }
ValueT& back() { return arr[m_count - 1]; }
ValueT pop_back() { return arr[--m_count]; }
void resize(int _size) { Resize(_size); }
void shrink_to_fit() { ShrinkToFit(); }
#endif
void GrowIfNecessary(int _size)
{
if (AX_UNLIKELY(_size > m_capacity))
{
int oldCapacity = m_capacity;
m_capacity = _size <= InitialSize ? InitialSize : CalculateArrayGrowth(_size);
if (arr) // array can be nullptr (first initialization)
arr = allocator.Reallocate(arr, oldCapacity, m_capacity);
else
arr = allocator.Allocate(m_capacity);
}
}
void OpenSpace(int _index, int _count)
{
long i = m_count + _count;
int j = m_count;
ASSERTR(i <= INT32_MAX, i = INT32_MAX);
GrowIfNecessary((int)i);
while (j >= _index)
{
arr[--i] = (ValueT&&) arr[--j];
}
m_count += _count;
}
void RemoveSpace(int _index, int _count)
{
ASSERTR((_index + _count) <= m_count, return);
int newSize = MAX(m_count - _count, 0);
int i = _index;
int j = _index + _count;
// *******i****j*** < opens space with incrementing both of the pointers (two pointer algorithm)
while (j < m_count)
{
arr[i++] = (ValueT&&) arr[j++];
}
if_constexpr (!AllocatorT::IsPod)
{
for (int i = newSize; i < m_count; ++i)
{
arr[i].~ValueT();
}
}
m_count = newSize;
}
};
template<typename T, int size>
using StackArray = Array<T, StackAllocator<T, size>>;
template<typename T>
using ObjArray = Array<T, Allocator<T>>;
inline void SBFree(void* a) { if (a) FreeAligned(((int*)a)-2); }
inline int SBCount(void* a) { if (!a) return 0; return *((int*)a - 1); }
template<typename T>
inline void SBPush(T*& b, const T& v)
{
T* a = b;
constexpr int twoIntSize = sizeof(int) + sizeof(int);
if (a == nullptr)
{
a = (T*)AllocAligned(sizeof(T) * 16 + twoIntSize, alignof(T));
((int*)a)[0] = 16;
((int*)a)[1] = 0;
a = (T*)((int*)a+2);
}
int* arr = ((int*)a);
int size = arr[-1], capacity = arr[-2];
if (size + 1 >= capacity)
{
int* old = arr-2;
int newCapacity = CalculateArrayGrowth(capacity);
a = (T*)AllocAligned(newCapacity * sizeof(T) + twoIntSize, alignof(T));
MemCpy<alignof(T)>(a, old, capacity * sizeof(T) + twoIntSize);
FreeAligned(old);
a = (T*)((int*)a+2);
arr = ((int*)a);
arr[-2] = newCapacity;
}
SmallMemCpy(a + size, &v, sizeof(T));
((int*)a)[-1] = size + 1;
b = a;
}
AX_END_NAMESPACE