From 57ef2743a44ea125f332add15a26777e78092ec4 Mon Sep 17 00:00:00 2001 From: AdventureT Date: Fri, 8 Dec 2023 16:04:21 +0100 Subject: [PATCH] Fixed TArray --- Toshi/Include/TKernel/TArray.h | 44 ++++++++++++++-------------------- Toshi/Include/TKernel/TMath.h | 4 ++++ 2 files changed, 22 insertions(+), 26 deletions(-) create mode 100644 Toshi/Include/TKernel/TMath.h diff --git a/Toshi/Include/TKernel/TArray.h b/Toshi/Include/TKernel/TArray.h index dcd64a2..4c0fffd 100644 --- a/Toshi/Include/TKernel/TArray.h +++ b/Toshi/Include/TKernel/TArray.h @@ -2,6 +2,7 @@ #include #include "TMemory.h" #include "TSystemTools.h" +#include "TMath.h" TOSHI_NAMESPACE_BEGIN @@ -59,8 +60,9 @@ class TArray m_iIndex++; TASSERT(m_poArray); - if (m_poArray->m_iNumElements <= m_iIndex || m_iIndex == 0) + if (m_poArray->m_iNumElements <= m_iIndex || m_iIndex == 0) { m_iIndex = -1; + } return m_iIndex; } @@ -148,12 +150,10 @@ class TArray m_iNumAllocElements = a_iSize; m_iNumElements = 0; - if (m_iNumAllocElements > 0) - { - m_pData = TSTATICCAST(T*, Tmemalign(alignof(T), m_iNumAllocElements * sizeof(T))); + if (m_iNumAllocElements > 0) { + m_pData = TSTATICCAST(T*, tmemalign(alignof(T), m_iNumAllocElements * sizeof(T))); } - else - { + else { TASSERT(m_iGrowSize != 0); m_pData = TNULL; } @@ -161,18 +161,15 @@ class TArray ~TArray() { - if (m_pData) - { + if (m_pData) { tfree(m_pData); } } void Clear() { - if (m_iNumAllocElements < 0) - { - if (m_pData) - { + if (m_iNumAllocElements < 0) { + if (m_pData) { tfree(m_pData); m_pData = TNULL; } @@ -242,35 +239,30 @@ class TArray private: void GrowBy(TINT a_iGrowBy) { - if (m_iNumAllocElements < m_iNumElements + a_iGrowBy) - { + if (m_iNumAllocElements < m_iNumElements + a_iGrowBy) { TASSERT(m_iGrowSize != 0); - - - auto iNewSize = m_iNumAllocElements + m_iGrowSize < m_iNumElements + a_iGrowBy ? TMath::Max(m_iNumAllocElements + m_iGrowSize, m_iNumElements + a_iGrowBy); + TINT iNewSize = TMAX(m_iNumAllocElements + m_iGrowSize, m_iNumElements + a_iGrowBy); Resize(iNewSize); } } void Resize(TINT a_iNewSize) { - if (a_iNewSize != 0) - { - T* pNewBuffer = TSTATICCAST(T*, TMemalign(alignof(T), a_iNewSize * sizeof(T))); - size_t uiCopySize = TMath::Min(m_iNumElements, a_iNewSize); + if (a_iNewSize != 0) { + T* pNewBuffer = TSTATICCAST(T*, tmemalign(alignof(T), a_iNewSize * sizeof(T))); + size_t uiCopySize = TMIN(m_iNumElements, a_iNewSize); - TUtil::MemCopy(pNewBuffer, m_pData, sizeof(T) * uiCopySize); + TSystem::MemCopy(pNewBuffer, m_pData, sizeof(T) * uiCopySize); m_iNumAllocElements = a_iNewSize; TASSERT(m_iNumElements <= m_iNumAllocElements); - if (m_pData) TFree(m_pData); + if (m_pData) tfree(m_pData); m_pData = pNewBuffer; } - else - { - if (m_pData) TFree(m_pData); + else { + if (m_pData) tfree(m_pData); m_pData = TNULL; m_iNumAllocElements = 0; m_iNumElements = 0; diff --git a/Toshi/Include/TKernel/TMath.h b/Toshi/Include/TKernel/TMath.h new file mode 100644 index 0000000..aa43594 --- /dev/null +++ b/Toshi/Include/TKernel/TMath.h @@ -0,0 +1,4 @@ +#pragma once + +#define TMIN(x,y) (((a)<(b))?(a):(b)) +#define TMAX(x,y) (((a)>(b))?(a):(b)) \ No newline at end of file