#pragma once #include "inc.h" struct FMemory { static inline void* (*Realloc)(void* Original, SIZE_T Count, uint32_t Alignment /* = DEFAULT_ALIGNMENT */); }; template //, 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, int Size = sizeof(InElementType)) const { return *(InElementType*)(__int64(Data) + (static_cast(Size) * i)); } inline InElementType at(int i, int Size = sizeof(InElementType)) const { return *(InElementType*)(__int64(Data) + (static_cast(Size) * i)); } inline InElementType* AtPtr(int i, int Size = sizeof(InElementType)) const { return (InElementType*)(__int64(Data) + (static_cast(Size) * i)); } inline int Num() const { return ArrayNum; } inline int size() const { return ArrayNum; } FORCEINLINE SizeType CalculateSlackGrow(SizeType NumElements, SizeType NumAllocatedElements, SIZE_T NumBytesPerElement) const { return ArrayMax - NumElements; } void Reserve(int Number, int 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); } FORCENOINLINE void ResizeGrow(int32 OldNum, int 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, ArrayMax * Size, 0); } FORCEINLINE int32 AddUninitialized(int32 Count = 1, int 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, int 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(Args)...); return Index; } /* int Add(const InElementType& New, int Size = sizeof(InElementType)) { return Emplace(New, Size); } */ int Add(const InElementType& New, int 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, sizeof(InElementType) * ArrayNum, MEM_RELEASE); } ArrayNum = 0; ArrayMax = 0; } bool Remove(const int Index) { if (Index < ArrayNum) { if (Index != ArrayNum - 1) Data[Index] = Data[ArrayNum - 1]; --ArrayNum; return true; } return false; }; };