CommonLibSSE NG
Loading...
Searching...
No Matches
GArrayBase.h
Go to the documentation of this file.
1#pragma once
2
3#include "GMemory.h"
4
5namespace RE
6{
7 class GMemoryHeap;
8
9 template <class T>
11 {
12 public:
13 using ValueType = typename T::ValueType;
14 using AllocatorType = typename T::AllocatorType;
15 using SizePolicyType = typename T::SizePolicyType;
17
18 struct iterator
19 {
20 public:
22 array(nullptr),
23 curIndex(-1)
24 {}
25
26 iterator(SelfType* a_arr, SPInt a_idx = 0) :
27 array(a_arr),
28 curIndex(a_idx)
29 {}
30
31 bool operator==(const iterator& a_it) const
32 {
33 return array == a_it.array && curIndex == a_it.curIndex;
34 }
35
36 bool operator!=(const iterator& a_it) const
37 {
38 return array != a_it.array || curIndex != a_it.curIndex;
39 }
40
42 {
43 if (array) {
44 if (curIndex < static_cast<SPInt>(array->GetSize())) {
45 ++curIndex;
46 }
47 }
48 return *this;
49 }
50
51 iterator operator++(std::int32_t)
52 {
53 iterator it(*this);
54 operator++();
55 return it;
56 }
57
59 {
60 if (array) {
61 if (curIndex >= 0) {
62 --curIndex;
63 }
64 }
65 return *this;
66 }
67
68 iterator operator--(std::int32_t)
69 {
70 iterator it(*this);
71 operator--();
72 return it;
73 }
74
75 iterator operator+(std::int32_t a_delta) const
76 {
77 return iterator(array, curIndex + a_delta);
78 }
79
80 iterator operator-(std::int32_t a_delta) const
81 {
82 return iterator(array, curIndex - a_delta);
83 }
84
85 SPInt operator-(const iterator& a_right) const
86 {
87 assert(array == a_right.array);
88 return curIndex - a_right.curIndex;
89 }
90
92 {
93 assert(array);
94 return (*array)[curIndex];
95 }
96
98 {
99 assert(array);
100 return &(*array)[curIndex];
101 }
102
104 {
105 assert(array);
106 return &(*array)[curIndex];
107 }
108
109 bool IsFinished() const
110 {
111 return !array || curIndex < 0 || curIndex >= static_cast<std::int32_t>(array->GetSize());
112 }
113
114 void Remove()
115 {
116 if (!IsFinished()) {
117 array->RemoveAt(curIndex);
118 }
119 }
120
122 {
123 return curIndex;
124 }
125
126 private:
127 SelfType* array;
128 SPInt curIndex;
129 };
130
132 {
133 public:
135 array(nullptr),
136 curIndex(-1)
137 {}
138
139 const_iterator(const SelfType* a_arr, SPInt a_idx = 0) :
140 array(a_arr),
141 curIndex(a_idx)
142 {}
143
144 bool operator==(const const_iterator& a_it) const
145 {
146 return array == a_it.array && curIndex == a_it.curIndex;
147 }
148
149 bool operator!=(const const_iterator& a_it) const
150 {
151 return array != a_it.array || curIndex != a_it.curIndex;
152 }
153
155 {
156 if (array) {
157 if (curIndex < static_cast<std::int32_t>(array->GetSize())) {
158 ++curIndex;
159 }
160 }
161 return *this;
162 }
163
165 {
166 const_iterator it(*this);
167 operator++();
168 return it;
169 }
170
172 {
173 if (array) {
174 if (curIndex >= 0)
175 --curIndex;
176 }
177 return *this;
178 }
179
181 {
182 const_iterator it(*this);
183 operator--();
184 return it;
185 }
186
187 const_iterator operator+(std::int32_t a_delta) const
188 {
189 return const_iterator(array, curIndex + a_delta);
190 }
191
192 const_iterator operator-(std::int32_t a_delta) const
193 {
194 return const_iterator(array, curIndex - a_delta);
195 }
196
197 SPInt operator-(const const_iterator& a_right) const
198 {
199 assert(array == a_right.array);
200 return curIndex - a_right.curIndex;
201 }
202
203 const ValueType& operator*() const
204 {
205 assert(array);
206 return (*array)[curIndex];
207 }
208
209 const ValueType* operator->() const
210 {
211 assert(array);
212 return &(*array)[curIndex];
213 }
214
215 const ValueType* GetPtr() const
216 {
217 assert(array);
218 return &(*array)[curIndex];
219 }
220
221 bool IsFinished() const
222 {
223 return !array || curIndex < 0 || curIndex >= static_cast<std::int32_t>(array->GetSize());
224 }
225
227 {
228 return curIndex;
229 }
230
231 private:
232 const SelfType* array;
233 SPInt curIndex;
234 };
235
236 GFC_MEMORY_REDEFINE_NEW(GArrayBase, AllocatorType::StatId);
237
239 data()
240 {}
241
242 GArrayBase(std::int32_t a_size) :
243 data(a_size)
244 {}
245
246 GArrayBase(const SelfType& a_array) :
247 data(a_array.data)
248 {}
249
251 data(a_heap)
252 {}
253
254 GArrayBase(GMemoryHeap* a_heap, std::int32_t a_size) :
255 data(a_heap, a_size)
256 {}
257
258 GArrayBase(const ValueType& a_defaultVal) :
259 data(a_defaultVal)
260 {}
261
262 GArrayBase(const ValueType& a_defaultVal, std::int32_t a_size) :
263 data(a_defaultVal, a_size)
264 {}
265
267 {
268 return data.policy;
269 }
270
271 void SetSizePolicy(const SizePolicyType& a_policy)
272 {
273 data.policy = a_policy;
274 }
275
276 bool NeverShrinking() const
277 {
278 return data.policy.NeverShrinking();
279 }
280
282 {
283 return data.size;
284 }
285
287 {
288 return data.GetCapacity();
289 }
290
292 {
293 return data.GetCapacity() * sizeof(ValueType);
294 }
295
297 {
298 data.ClearAndRelease();
299 }
300
301 void Clear()
302 {
303 data.Resize(0);
304 }
305
306 void Resize(UPInt a_newSize)
307 {
308 data.Resize(a_newSize);
309 }
310
311 void Reserve(UPInt a_newCapacity)
312 {
313 if (a_newCapacity > data.GetCapacity()) {
314 data.Reserve(a_newCapacity);
315 }
316 }
317
319 {
320 assert(a_index < data.size);
321 return data.data[a_index];
322 }
323
324 const ValueType& At(UPInt a_index) const
325 {
326 assert(a_index < data.size);
327 return data.data[a_index];
328 }
329
330 ValueType ValueAt(UPInt a_index) const
331 {
332 assert(a_index < data.size);
333 return data.data[a_index];
334 }
335
337 {
338 assert(a_index < data.size);
339 return data.data[a_index];
340 }
341
342 const ValueType& operator[](UPInt a_index) const
343 {
344 assert(a_index < data.size);
345 return data.data[a_index];
346 }
347
348 void PushBack(const ValueType& a_val)
349 {
350 data.PushBack(a_val);
351 }
352
353 template <class S>
354 void PushBackAlt(const S& a_val)
355 {
356 data.PushBackAlt(a_val);
357 }
358
359 void PopBack()
360 {
361 assert(data.size > 0);
362 data.Resize(data.size - 1);
363 }
364
366 {
367 return At(0);
368 }
369
370 const ValueType& Front() const
371 {
372 return At(0);
373 }
374
376 {
377 return At(data.size - 1);
378 }
379
380 const ValueType& Back() const
381 {
382 return At(data.size - 1);
383 }
384
385 const SelfType& operator=(const SelfType& a_array)
386 {
387 Resize(a_array.GetSize());
388 for (UPInt i = 0; i < data.size; i++) {
389 *(data.data + i) = a_array[i];
390 }
391 return *this;
392 }
393
394 void RemoveMultipleAt(UPInt a_index, UPInt a_num)
395 {
396 assert(a_index + a_num <= data.size);
397
398 if (data.size == a_num) {
399 Clear();
400 } else {
401 AllocatorType::DestructArray(data.data + a_index, a_num);
402 AllocatorType::CopyArrayForward(
403 data.data + a_index,
404 data.data + a_index + a_num,
405 data.size + a_num - a_index);
406 data.size -= a_num;
407 }
408 }
409
410 void RemoveAt(UPInt a_index)
411 {
412 assert(a_index < data.size);
413
414 if (data.size == 1) {
415 Clear();
416 } else {
417 AllocatorType::Destruct(data.data + a_index);
418 AllocatorType::CopyArrayForward(
419 data.data + a_index,
420 data.data + a_index + 1,
421 data.size - 1 - a_index);
422 --data.size;
423 }
424 }
425
426 void InsertAt(UPInt a_index, const ValueType& a_val = ValueType())
427 {
428 assert(a_index <= data.size);
429
430 data.Resize(data.size + 1);
431 if (a_index < data.size - 1) {
432 AllocatorType::CopyArrayBackward(
433 data.data + a_index + 1,
434 data.data + a_index,
435 data.size - 1 - a_index);
436 }
437 AllocatorType::Construct(data.data + a_index, a_val);
438 }
439
440 void InsertMultipleAt(UPInt a_index, UPInt a_num, const ValueType& a_val = ValueType())
441 {
442 assert(a_index <= data.size);
443
444 data.Resize(data.size + a_num);
445 if (a_index < data.size - a_num) {
446 AllocatorType::CopyArrayBackward(
447 data.data + a_index + a_num,
448 data.data + a_index,
449 data.size - a_num - a_index);
450 }
451
452 for (UPInt i = 0; i < a_num; ++i) {
453 AllocatorType::Construct(data.data + a_index + i, a_val);
454 }
455 }
456
457 void Append(const SelfType& a_other)
458 {
459 Append(a_other.data.data, a_other.GetSize());
460 }
461
462 void Append(const ValueType a_other[], UPInt a_count)
463 {
464 data.Append(a_other, a_count);
465 }
466
468 {
469 return iterator(this);
470 }
471
473 {
474 return iterator(this, static_cast<SPInt>(GetSize()));
475 }
476
478 {
479 return iterator(this, static_cast<SPInt>(GetSize() - 1));
480 }
481
483 {
484 return const_iterator(this);
485 }
486
488 {
489 return const_iterator(this, static_cast<SPInt>(GetSize()));
490 }
491
493 {
494 return const_iterator(this, static_cast<SPInt>(GetSize() - 1));
495 }
496
497 // members
498 T data; // 00
499 };
500}
Definition GArrayBase.h:11
void Clear()
Definition GArrayBase.h:301
iterator begin()
Definition GArrayBase.h:467
const ValueType & Front() const
Definition GArrayBase.h:370
const_iterator end() const
Definition GArrayBase.h:487
GArrayBase(GMemoryHeap *a_heap)
Definition GArrayBase.h:250
T data
Definition GArrayBase.h:498
void RemoveAt(UPInt a_index)
Definition GArrayBase.h:410
GArrayBase(GMemoryHeap *a_heap, std::int32_t a_size)
Definition GArrayBase.h:254
GFC_MEMORY_REDEFINE_NEW(GArrayBase, AllocatorType::StatId)
void PushBackAlt(const S &a_val)
Definition GArrayBase.h:354
void InsertAt(UPInt a_index, const ValueType &a_val=ValueType())
Definition GArrayBase.h:426
GArrayBase(std::int32_t a_size)
Definition GArrayBase.h:242
void ClearAndRelease()
Definition GArrayBase.h:296
void Resize(UPInt a_newSize)
Definition GArrayBase.h:306
void InsertMultipleAt(UPInt a_index, UPInt a_num, const ValueType &a_val=ValueType())
Definition GArrayBase.h:440
iterator Last()
Definition GArrayBase.h:477
const SelfType & operator=(const SelfType &a_array)
Definition GArrayBase.h:385
bool NeverShrinking() const
Definition GArrayBase.h:276
const_iterator Last() const
Definition GArrayBase.h:492
UPInt GetSize() const
Definition GArrayBase.h:281
const_iterator begin() const
Definition GArrayBase.h:482
typename T::AllocatorType AllocatorType
Definition GArrayBase.h:14
void RemoveMultipleAt(UPInt a_index, UPInt a_num)
Definition GArrayBase.h:394
const ValueType & Back() const
Definition GArrayBase.h:380
GArrayBase(const ValueType &a_defaultVal, std::int32_t a_size)
Definition GArrayBase.h:262
GArrayBase()
Definition GArrayBase.h:238
GArrayBase(const ValueType &a_defaultVal)
Definition GArrayBase.h:258
typename T::SizePolicyType SizePolicyType
Definition GArrayBase.h:15
ValueType & operator[](UPInt a_index)
Definition GArrayBase.h:336
void PushBack(const ValueType &a_val)
Definition GArrayBase.h:348
void SetSizePolicy(const SizePolicyType &a_policy)
Definition GArrayBase.h:271
void Reserve(UPInt a_newCapacity)
Definition GArrayBase.h:311
UPInt GetCapacity() const
Definition GArrayBase.h:286
SizePolicyType * GetSizePolicy() const
Definition GArrayBase.h:266
typename T::ValueType ValueType
Definition GArrayBase.h:13
const ValueType & operator[](UPInt a_index) const
Definition GArrayBase.h:342
ValueType ValueAt(UPInt a_index) const
Definition GArrayBase.h:330
const ValueType & At(UPInt a_index) const
Definition GArrayBase.h:324
ValueType & Back()
Definition GArrayBase.h:375
void Append(const SelfType &a_other)
Definition GArrayBase.h:457
UPInt GetNumBytes() const
Definition GArrayBase.h:291
iterator end()
Definition GArrayBase.h:472
void Append(const ValueType a_other[], UPInt a_count)
Definition GArrayBase.h:462
ValueType & At(UPInt a_index)
Definition GArrayBase.h:318
void PopBack()
Definition GArrayBase.h:359
ValueType & Front()
Definition GArrayBase.h:365
GArrayBase(const SelfType &a_array)
Definition GArrayBase.h:246
Definition GMemoryHeap.h:16
Definition AbsorbEffect.h:6
std::size_t UPInt
Definition SFTypes.h:5
std::ptrdiff_t SPInt
Definition SFTypes.h:8
Definition GArrayBase.h:132
bool IsFinished() const
Definition GArrayBase.h:221
const_iterator()
Definition GArrayBase.h:134
bool operator!=(const const_iterator &a_it) const
Definition GArrayBase.h:149
const_iterator operator--(std::int32_t)
Definition GArrayBase.h:180
const_iterator & operator++()
Definition GArrayBase.h:154
const ValueType & operator*() const
Definition GArrayBase.h:203
SPInt operator-(const const_iterator &a_right) const
Definition GArrayBase.h:197
const_iterator operator++(std::int32_t)
Definition GArrayBase.h:164
const_iterator(const SelfType *a_arr, SPInt a_idx=0)
Definition GArrayBase.h:139
const_iterator & operator--()
Definition GArrayBase.h:171
const ValueType * operator->() const
Definition GArrayBase.h:209
const_iterator operator+(std::int32_t a_delta) const
Definition GArrayBase.h:187
const_iterator operator-(std::int32_t a_delta) const
Definition GArrayBase.h:192
SPInt GetIndex() const
Definition GArrayBase.h:226
const ValueType * GetPtr() const
Definition GArrayBase.h:215
bool operator==(const const_iterator &a_it) const
Definition GArrayBase.h:144
Definition GArrayBase.h:19
iterator & operator--()
Definition GArrayBase.h:58
ValueType & operator*() const
Definition GArrayBase.h:91
bool IsFinished() const
Definition GArrayBase.h:109
void Remove()
Definition GArrayBase.h:114
ValueType * GetPtr() const
Definition GArrayBase.h:103
bool operator!=(const iterator &a_it) const
Definition GArrayBase.h:36
bool operator==(const iterator &a_it) const
Definition GArrayBase.h:31
iterator(SelfType *a_arr, SPInt a_idx=0)
Definition GArrayBase.h:26
ValueType * operator->() const
Definition GArrayBase.h:97
iterator operator++(std::int32_t)
Definition GArrayBase.h:51
SPInt GetIndex() const
Definition GArrayBase.h:121
iterator & operator++()
Definition GArrayBase.h:41
SPInt operator-(const iterator &a_right) const
Definition GArrayBase.h:85
iterator operator--(std::int32_t)
Definition GArrayBase.h:68
iterator()
Definition GArrayBase.h:21
iterator operator+(std::int32_t a_delta) const
Definition GArrayBase.h:75
iterator operator-(std::int32_t a_delta) const
Definition GArrayBase.h:80