forked from wopss/RED4ext.SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynArray.hpp
More file actions
382 lines (321 loc) · 7.97 KB
/
Copy pathDynArray.hpp
File metadata and controls
382 lines (321 loc) · 7.97 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
#pragma once
#include <algorithm>
#include <cstdint>
#include <functional>
#include <type_traits>
#include <RED4ext/Common.hpp>
#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/Memory/Allocators.hpp>
#include <RED4ext/Relocation.hpp>
#include <RED4ext/Utils.hpp>
namespace RED4ext
{
namespace Memory
{
struct IAllocator;
}
template<typename T>
struct DynArray
{
DynArray(Memory::IAllocator* aAllocator = nullptr)
: entries(aAllocator ? *reinterpret_cast<T**>(aAllocator) : nullptr)
, size(0)
, capacity(0)
{
}
DynArray(std::initializer_list<T> aItems, Memory::IAllocator* aAllocator = nullptr)
: DynArray(aAllocator)
{
Reserve(aItems.size());
for (const auto& item : aItems)
{
EmplaceBack(item);
}
}
DynArray(const DynArray& aOther)
: DynArray(aOther.GetAllocator())
{
Reserve(aOther.size);
CopyFrom(aOther);
}
DynArray(DynArray&& aOther) noexcept
{
MoveFrom(std::move(aOther));
}
~DynArray()
{
if (capacity)
{
Clear();
auto allocator = *reinterpret_cast<T**>(GetAllocator());
reinterpret_cast<Memory::IAllocator*>(&allocator)->Free(entries);
entries = allocator;
capacity = 0;
}
}
DynArray& operator=(const DynArray& aOther)
{
if (this != std::addressof(aOther))
{
Clear();
Reserve(aOther.size);
CopyFrom(aOther);
}
return *this;
}
DynArray& operator=(DynArray&& aOther)
{
if (this != std::addressof(aOther))
{
Clear();
MoveFrom(std::move(aOther));
}
return *this;
}
const T& operator[](uint32_t aIndex) const
{
return entries[aIndex];
}
T& operator[](uint32_t aIndex)
{
return entries[aIndex];
}
bool Contains(const T& aItem) const
{
for (uint32_t i = 0; i != size; ++i)
{
if (aItem == entries[i])
{
return true;
}
}
return false;
}
void PushBack(const T& aItem)
{
EmplaceBack(std::forward<const T&>(aItem));
}
void PushBack(T&& aItem)
{
EmplaceBack(std::forward<T&&>(aItem));
}
template<class... TArgs>
void EmplaceBack(TArgs&&... aArgs)
{
Emplace(end(), std::forward<TArgs>(aArgs)...);
}
template<class... TArgs>
void Emplace(T* aPosition, TArgs&&... aArgs)
{
uint32_t posIdx = capacity ? static_cast<uint32_t>(aPosition - begin()) : 0;
uint32_t newSize = size + 1;
if (newSize > capacity)
{
Reserve(newSize);
}
// If not at the end
if (posIdx != size)
{
uint32_t entriesCount = size - posIdx;
MoveEntries(&entries[posIdx], &entries[posIdx + 1], entriesCount);
}
new (&entries[posIdx]) T(std::forward<TArgs>(aArgs)...);
size = newSize;
}
bool Remove(const T& aItem)
{
for (uint32_t i = 0; i != size; ++i)
{
if (aItem == entries[i])
{
return RemoveAt(i);
}
}
return false;
}
bool RemoveAt(uint32_t aIndex)
{
if (aIndex >= size)
return false;
entries[aIndex].~T();
if ((aIndex + 1) != size) // If not at the end
{
uint32_t entriesCount = size - (aIndex + 1);
MoveEntries(&entries[aIndex + 1], &entries[aIndex], entriesCount);
}
--size;
return true;
}
void Clear()
{
for (uint32_t i = 0; i != size; ++i)
{
entries[i].~T();
}
size = 0;
}
void Reserve(uint32_t aCount)
{
if (capacity >= aCount)
return;
auto newCapacity = CalculateGrowth(aCount);
SetCapacity(newCapacity);
}
void ShrinkToSize()
{
if (capacity > size)
SetCapacity(size);
}
Memory::IAllocator* GetAllocator() const
{
if (capacity == 0)
{
// Case 1: Allocator is stored instead of entries pointer
// It's only 8 bytes for VFT so it fits in a pointer
return reinterpret_cast<Memory::IAllocator*>(const_cast<T**>(&entries));
}
else
{
// Case 2: Allocator is stored at the end of entries buffer (aligned)
auto allocatorPtr = AlignUp(reinterpret_cast<size_t>(&entries[capacity]), sizeof(void*));
return reinterpret_cast<Memory::IAllocator*>(allocatorPtr);
}
}
#pragma region Iterator
T* Begin()
{
return entries;
}
const T* Begin() const
{
return entries;
}
T* begin()
{
return Begin();
}
const T* begin() const
{
return Begin();
}
T* End()
{
return entries + size;
}
const T* End() const
{
return entries + size;
}
T* end()
{
return End();
}
const T* end() const
{
return End();
}
#pragma endregion
T& Front()
{
return *entries;
}
const T& Front() const
{
return *entries;
}
T& Back()
{
return *(entries + size - 1);
}
const T& Back() const
{
return *(entries + size - 1);
}
[[nodiscard]] T* Data() noexcept
{
return entries;
}
[[nodiscard]] const T* Data() const noexcept
{
return entries;
}
[[nodiscard]] uint32_t Capacity() const noexcept
{
return capacity;
}
[[nodiscard]] uint32_t Size() const noexcept
{
return size;
}
[[nodiscard]] bool IsEmpty() const noexcept
{
return size == 0;
}
T* entries; // 00
uint32_t capacity; // 08
uint32_t size; // 0C
private:
void MoveEntries(T* aSrc, T* aDst, uint32_t aCount)
{
if (aCount == 0 || aSrc == aDst)
return;
if constexpr (std::is_trivially_copyable_v<T>)
{
std::memmove(aDst, aSrc, aCount * sizeof(T));
}
else if (aSrc < aDst)
{
for (; aCount != 0; --aCount)
{
new (&aDst[aCount - 1]) T(std::move(aSrc[aCount - 1]));
aSrc[aCount - 1].~T();
}
}
else
{
for (uint32_t i = 0; i != aCount; ++i)
{
new (&aDst[i]) T(std::move(aSrc[i]));
aSrc[i].~T();
}
}
}
uint32_t CalculateGrowth(uint32_t aNewSize)
{
uint32_t geometric = capacity + (capacity / 2);
return (std::max)(aNewSize, geometric);
}
void CopyFrom(const DynArray& aOther)
{
for (uint32_t i = 0; i != aOther.size; ++i)
{
PushBack(aOther[i]);
}
}
void SetCapacity(uint32_t aNewCapacity)
{
if (aNewCapacity < size)
return;
constexpr uint32_t alignment = alignof(T);
using func_t = void (*)(DynArray* aThis, uint32_t aCapacity, uint32_t aElementSize, uint32_t aAlignment,
void (*aMoveFunc)(T* aDstBuffer, T* aSrcBuffer, int32_t aSrcSize, DynArray* aSrcArray));
static UniversalRelocFunc<func_t> func(Detail::AddressHashes::DynArray_Realloc);
func(this, aNewCapacity, sizeof(T), alignment >= 8 ? alignment : 8, nullptr);
}
void MoveFrom(DynArray&& aOther)
{
entries = aOther.entries;
capacity = aOther.capacity;
size = aOther.size;
if (aOther.capacity)
{
aOther.entries = *reinterpret_cast<T**>(aOther.GetAllocator());
aOther.capacity = 0;
aOther.size = 0;
}
}
};
RED4EXT_ASSERT_SIZE(DynArray<void*>, 0x10);
RED4EXT_ASSERT_OFFSET(DynArray<void*>, capacity, 0x8);
RED4EXT_ASSERT_OFFSET(DynArray<void*>, size, 0xC);
} // namespace RED4ext