-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArray.h
359 lines (291 loc) · 9.24 KB
/
Array.h
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
#pragma once
#include "inc.h"
#include "MemoryOps.h"
#include "ContainerAllocationPolicies.h"
struct FMemory
{
static void* (*Realloc)(void* Original, SIZE_T Count, uint32_t Alignment /* = DEFAULT_ALIGNMENT */);
};
template <typename T = __int64>
static T* AllocUnreal(size_t Size)
{
return (T*)FMemory::Realloc(0, Size, 0);
}
template<typename InElementType> //, typename InAllocatorType>
class TArray
{
// protected:
public:
friend class FString;
using ElementAllocatorType = InElementType*;
using SizeType = int32;
ElementAllocatorType Data = nullptr; // AllocatorInstance;
SizeType ArrayNum;
SizeType ArrayMax;
public:
inline InElementType& At(int i, size_t Size = sizeof(InElementType)) const { return *(InElementType*)(__int64(Data) + (static_cast<long long>(Size) * i)); }
inline InElementType& at(int i, size_t Size = sizeof(InElementType)) const { return *(InElementType*)(__int64(Data) + (static_cast<long long>(Size) * i)); }
inline InElementType* AtPtr(int i, size_t Size = sizeof(InElementType)) const { return (InElementType*)(__int64(Data) + (static_cast<long long>(Size) * i)); }
bool IsValidIndex(int i) { return i > 0 && i < ArrayNum; }
ElementAllocatorType& GetData() const { return Data; }
ElementAllocatorType& GetData() { return Data; }
void Reserve(int Number, size_t Size = sizeof(InElementType))
{
// LOG_INFO(LogDev, "ArrayNum {}", ArrayNum);
// Data = (InElementType*)FMemory::Realloc(Data, (ArrayMax = ArrayNum + Number) * Size, 0);
Data = /* (ArrayMax - ArrayNum) >= ArrayNum ? Data : */ (InElementType*)FMemory::Realloc(Data, (ArrayMax = Number + ArrayNum) * Size, 0);
}
int CalculateSlackReserve(SizeType NumElements, SIZE_T NumBytesPerElement) const
{
return DefaultCalculateSlackReserve(NumElements, NumBytesPerElement, false);
}
void ResizeArray(SizeType NewNum, SIZE_T NumBytesPerElement)
{
const SizeType CurrentMax = ArrayMax;
SizeType v3 = NewNum;
if (NewNum)
{
/* SizeType v6 = (unsigned __int64)FMemory::QuantizeSize(4 * NewNum, 0) >> 2;
// if (v3 > (int)v6)
// LODWORD(v6) = 0x7FFFFFFF;
v3 = v6; */
}
if (v3 != CurrentMax && (Data || v3))
Data = (InElementType*)FMemory::Realloc(Data, NumBytesPerElement * v3, 0);
ArrayNum = v3; // ?
ArrayMax = v3;
}
void RefitArray(SIZE_T NumBytesPerElement = sizeof(InElementType))
{
auto newNum = ArrayNum;
// newNum = FMemory::QuantizeSize(NumBytesPerElement * newNum, 0) >> 4
ArrayMax = newNum;
if (Data || ArrayNum)
{
Data = false ? (InElementType*)VirtualAlloc(0, newNum * NumBytesPerElement, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE) :
(InElementType*)FMemory::Realloc(Data, newNum * NumBytesPerElement, 0);
}
}
int AddUninitialized2(SIZE_T NumBytesPerElement = sizeof(InElementType))
{
const int OldArrayNum = ArrayNum;
ArrayNum = OldArrayNum + 1;
if (OldArrayNum + 1 > ArrayMax)
{
RefitArray(NumBytesPerElement);
// ResizeArray(ArrayNum, NumBytesPerElement);
}
return OldArrayNum;
}
void CopyFromArray(TArray<InElementType>& OtherArray, SIZE_T NumBytesPerElement = sizeof(InElementType))
{
if (!OtherArray.ArrayNum && !ArrayMax) // so if the new array has nothing, and we currently have nothing allocated, then we can just return
{
ArrayMax = 0;
return;
}
ResizeArray(OtherArray.ArrayNum, NumBytesPerElement);
memcpy(this->Data, OtherArray.Data, NumBytesPerElement * OtherArray.ArrayNum);
}
/*
FORCENOINLINE void ResizeForCopy(SizeType NewMax, SizeType PrevMax, int ElementSize = sizeof(InElementType))
{
if (NewMax)
{
NewMax = CalculateSlackReserve(NewMax, ElementSize);
}
if (NewMax != PrevMax)
{
int ReserveCount = NewMax - PrevMax; // IDK TODO Milxnor
Reserve(ReserveCount, ElementSize);
// AllocatorInstance.ResizeAllocation(0, NewMax, ElementSize);
}
ArrayMax = NewMax;
}
template <typename OtherElementType, typename OtherSizeType>
void CopyToEmpty(const OtherElementType* OtherData, OtherSizeType OtherNum, SizeType PrevMax, SizeType ExtraSlack, int ElementSize = sizeof(InElementType))
{
SizeType NewNum = (SizeType)OtherNum;
// checkf((OtherSizeType)NewNum == OtherNum, TEXT("Invalid number of elements to add to this array type: %llu"), (unsigned long long)NewNum);
// checkSlow(ExtraSlack >= 0);
ArrayNum = NewNum;
if (OtherNum || ExtraSlack || PrevMax)
{
ResizeForCopy(NewNum + ExtraSlack, PrevMax);
ConstructItems<InElementType>(GetData(), OtherData, OtherNum);
}
else
{
ArrayMax = 0; // AllocatorInstance.GetInitialCapacity();
}
}
FORCEINLINE TArray(const TArray& Other)
{
CopyToEmpty(Other.Data, Other.Num(), 0, 0);
}
*/
TArray() : Data(nullptr), ArrayNum(0), ArrayMax(0) {}
inline int Num() const { return ArrayNum; }
inline int size() const { return ArrayNum; }
/* FORCENOINLINE void ResizeTo(int32 NewMax)
{
if (NewMax)
{
NewMax = AllocatorInstance.CalculateSlackReserve(NewMax, sizeof(ElementType));
}
if (NewMax != ArrayMax)
{
ArrayMax = NewMax;
AllocatorInstance.ResizeAllocation(ArrayNum, ArrayMax, sizeof(ElementType));
}
}
void Empty(int32 Slack = 0)
{
// DestructItems(GetData(), ArrayNum);
// checkSlow(Slack >= 0);
ArrayNum = 0;
if (ArrayMax != Slack)
{
ResizeTo(Slack);
}
}
void Reset(int32 NewSize = 0)
{
// If we have space to hold the excepted size, then don't reallocate
if (NewSize <= ArrayMax)
{
// DestructItems(GetData(), ArrayNum);
ArrayNum = 0;
}
else
{
Empty(NewSize);
}
} */
void RemoveAtImpl(int32 Index, int32 Count, bool bAllowShrinking)
{
if (Count)
{
// CheckInvariants();
// checkSlow((Count >= 0) & (Index >= 0) & (Index + Count <= ArrayNum));
// DestructItems(GetData() + Index, Count); // TODO milxnor
// Skip memmove in the common case that there is nothing to move.
int32 NumToMove = ArrayNum - Index - Count;
if (NumToMove)
{
/* FMemory::Memmove
(
(uint8*)AllocatorInstance.GetAllocation() + (Index) * sizeof(ElementType),
(uint8*)AllocatorInstance.GetAllocation() + (Index + Count) * sizeof(ElementType),
NumToMove * sizeof(ElementType)
); */
// memmove(Data + (Index) * sizeof(InElementType), Data + (Index + Count) * sizeof(InElementType), NumToMove * sizeof(InElementType)); // i think this wrong
}
ArrayNum -= Count;
if (bAllowShrinking)
{
// ResizeShrink(); // TODO milxnor
}
}
}
FORCEINLINE SizeType CalculateSlackGrow(SizeType NumElements, SizeType NumAllocatedElements, SIZE_T NumBytesPerElement) const
{
return ArrayMax - NumElements;
}
template <typename CountType>
FORCEINLINE void RemoveAt(int32 Index, CountType Count, bool bAllowShrinking = true)
{
// static_assert(!TAreTypesEqual<CountType, bool>::Value, "TArray::RemoveAt: unexpected bool passed as the Count argument");
RemoveAtImpl(Index, Count, bAllowShrinking);
}
void ResizeGrow(int32 OldNum, size_t Size = sizeof(InElementType))
{
// LOG_INFO(LogMemory, "FMemory::Realloc: {}", __int64(FMemory::Realloc));
ArrayMax = ArrayNum; // CalculateSlackGrow(/* ArrayNum */ OldNum, ArrayMax, Size);
// AllocatorInstance.ResizeAllocation(OldNum, ArrayMax, sizeof(ElementType));
// LOG_INFO(LogMemory, "ArrayMax: {} Size: {}", ArrayMax, Size);
Data = (InElementType*)FMemory::Realloc(Data, ArrayNum * Size, 0);
}
FORCEINLINE int32 AddUninitialized(int32 Count = 1, size_t Size = sizeof(InElementType))
{
// CheckInvariants();
if (Count < 0)
{
return 0;
}
const int32 OldNum = ArrayNum;
if ((ArrayNum += Count) > ArrayMax)
{
ResizeGrow(OldNum, Size);
}
return OldNum;
}
FORCEINLINE int32 Emplace(const InElementType& New, size_t Size = sizeof(InElementType))
{
const int32 Index = AddUninitialized(1, Size); // resizes array
memcpy_s((InElementType*)(__int64(Data) + (Index * Size)), Size, (void*)&New, Size);
// new(GetData() + Index) ElementType(Forward<ArgsType>(Args)...);
return Index;
}
/* int Add(const InElementType& New, int Size = sizeof(InElementType))
{
return Emplace(New, Size);
} */
int AddPtr(InElementType* New, size_t Size = sizeof(InElementType))
{
// LOG_INFO(LogDev, "ArrayMax: {}", ArrayMax);
if ((ArrayNum + 1) > ArrayMax)
{
Reserve(1, Size);
}
if (Data)
{
memcpy_s((InElementType*)(__int64(Data) + (ArrayNum * Size)), Size, (void*)New, Size);
++ArrayNum;
return ArrayNum; // - 1;
}
return -1;
}
int Add(const InElementType& New, size_t Size = sizeof(InElementType))
{
// LOG_INFO(LogDev, "ArrayMax: {}", ArrayMax);
if ((ArrayNum + 1) > ArrayMax)
{
Reserve(1, Size);
}
if (Data)
{
memcpy_s((InElementType*)(__int64(Data) + (ArrayNum * Size)), Size, (void*)&New, Size);
++ArrayNum;
return ArrayNum; // - 1;
}
return -1;
}
void Free()
{
if (Data && ArrayNum > 0 && sizeof(InElementType) > 0)
{
// VirtualFree(Data, _msize(Data), MEM_RELEASE);
VirtualFree(Data, sizeof(InElementType) * ArrayNum, MEM_RELEASE); // ik this does nothing
// VirtualFree(Data, 0, MEM_RELEASE);
}
Data = nullptr;
ArrayNum = 0;
ArrayMax = 0;
}
bool Remove(const int Index, size_t Size = sizeof(InElementType))
{
// return false;
if (Index < ArrayNum)
{
if (Index != ArrayNum - 1)
{
memcpy_s(&at(Index, Size), Size, &at(ArrayNum - 1, Size), Size);
// Data[Index] = Data[ArrayNum - 1];
}
--ArrayNum;
return true;
}
return false;
}
};