From 9343ed56e7570803bf87e7a9a427aa85ff8eab9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 6 Jan 2022 16:08:11 +0100 Subject: [PATCH] Havok: Implement more hkArray functions --- .../Common/Base/Container/Array/hkArray.h | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lib/hkStubs/Havok/Common/Base/Container/Array/hkArray.h b/lib/hkStubs/Havok/Common/Base/Container/Array/hkArray.h index 1db2335e..33f9f6f4 100644 --- a/lib/hkStubs/Havok/Common/Base/Container/Array/hkArray.h +++ b/lib/hkStubs/Havok/Common/Base/Container/Array/hkArray.h @@ -92,6 +92,9 @@ public: hkBool32 shrinkExact = false); protected: + HK_FORCE_INLINE hkArrayBase& copyFromArray(hkMemoryAllocator& allocator, + const hkArrayBase& src); + T* m_data; int m_size; int m_capacityAndFlags; @@ -240,6 +243,24 @@ inline hkBool hkArrayBase::tryPushBack(const T& t) { return false; } +template +inline hkResult hkArrayBase::_reserve(hkMemoryAllocator& alloc, int n) { + const int capacity = getCapacity(); + if (capacity < n) { + int newCapacity = 2 * capacity; + int newSize = n < newCapacity ? newCapacity : n; + return hkArrayUtil::_reserve(alloc, this, newSize, sizeof(T)); + } + return HK_SUCCESS; +} + +template +inline hkResult hkArrayBase::_reserveExactly(hkMemoryAllocator& alloc, int n) { + if (getCapacity() < n) + return hkArrayUtil::_reserve(alloc, this, n, sizeof(T)); + return HK_SUCCESS; +} + template inline typename hkArrayBase::iterator hkArrayBase::begin() { return m_data; @@ -286,6 +307,48 @@ inline void hkArrayBase::setDataUserFree(T* ptr, int size, int capacity) { _setDataUnchecked(ptr, size, capacity | DONT_DEALLOCATE_FLAG); } +template +inline hkArrayBase& hkArrayBase::copyFromArray(hkMemoryAllocator& allocator, + const hkArrayBase& src) { + if constexpr (std::is_pod_v) { + if (getCapacity() < src.getSize()) { + if ((m_capacityAndFlags & DONT_DEALLOCATE_FLAG) == 0) { + allocator._bufFree(m_data, getCapacity()); + } + const int n = src.getSize(); + m_data = allocator._bufAlloc(n); + m_capacityAndFlags = n; + } + m_size = src.getSize(); + copy(m_data, src.m_data, m_size); + + } else { + const int oldSize = m_size; + const int newSize = src.getSize(); + const int copiedSize = newSize > oldSize ? oldSize : newSize; + + _reserve(allocator, newSize); + hkArrayUtil::destruct(m_data + newSize, oldSize - newSize); + copy(m_data, src.m_data, copiedSize); + hkArrayUtil::constructWithArray(m_data + copiedSize, newSize - copiedSize, + src.m_data + copiedSize); + m_size = newSize; + } + return *this; +} + +template +inline hkArray& hkArray::operator=(const hkArrayBase& a) { + this->copyFromArray(Allocator().get(), a); + return *this; +} + +template +inline hkArray& hkArray::operator=(const hkArray& a) { + this->copyFromArray(Allocator().get(), a); + return *this; +} + template inline void hkArray::clearAndDeallocate() { this->_clearAndDeallocate(AllocatorType().get());