From 089c9ea2db1ccdc37a7a02efc030b105e29aafeb Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Fri, 4 Sep 2020 17:51:37 +0200 Subject: [PATCH] Refactor the List::removeAtAndReplaceByLast() method --- include/reactphysics3d/containers/List.h | 16 ++++------------ include/reactphysics3d/engine/OverlappingPairs.h | 15 +++++---------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/include/reactphysics3d/containers/List.h b/include/reactphysics3d/containers/List.h index d0c5a731..1bfb10d7 100755 --- a/include/reactphysics3d/containers/List.h +++ b/include/reactphysics3d/containers/List.h @@ -376,20 +376,12 @@ class List { /// Remove an element from the list at a given index and replace it by the last one of the list (if any) void removeAtAndReplaceByLast(uint32 index) { - assert(index >= 0 && index < mSize); + assert(index < mSize); - // Call the destructor - (static_cast(mBuffer)[index]).~T(); + static_cast(mBuffer)[index] = static_cast(mBuffer)[mSize - 1]; - // If there is another element in the list - if (mSize > 1 && index < (mSize - 1)) { - - // Copy the last element of the list at the location of the removed element - new (static_cast(mBuffer) + index * sizeof(T)) T(static_cast(mBuffer)[mSize - 1]); - - // Call the destructor of the last element - (static_cast(mBuffer)[mSize - 1]).~T(); - } + // Call the destructor of the last element + (static_cast(mBuffer)[mSize - 1]).~T(); mSize--; } diff --git a/include/reactphysics3d/engine/OverlappingPairs.h b/include/reactphysics3d/engine/OverlappingPairs.h index e00dcd54..cdde8865 100644 --- a/include/reactphysics3d/engine/OverlappingPairs.h +++ b/include/reactphysics3d/engine/OverlappingPairs.h @@ -174,7 +174,7 @@ class OverlappingPairs { private: - MemoryAllocator& mPoolAllocator; + MemoryAllocator* mPoolAllocator; public: @@ -192,7 +192,7 @@ class OverlappingPairs { ConcaveOverlappingPair(uint64 pairId, int32 broadPhaseId1, int32 broadPhaseId2, Entity collider1, Entity collider2, NarrowPhaseAlgorithmType narrowPhaseAlgorithmType, bool isShape1Convex, MemoryAllocator& poolAllocator, MemoryAllocator& heapAllocator) - : OverlappingPair(pairId, broadPhaseId1, broadPhaseId2, collider1, collider2, narrowPhaseAlgorithmType), mPoolAllocator(poolAllocator), + : OverlappingPair(pairId, broadPhaseId1, broadPhaseId2, collider1, collider2, narrowPhaseAlgorithmType), mPoolAllocator(&poolAllocator), isShape1Convex(isShape1Convex), lastFrameCollisionInfos(heapAllocator, 16) { } @@ -206,7 +206,7 @@ class OverlappingPairs { it->second->LastFrameCollisionInfo::~LastFrameCollisionInfo(); // Release memory - mPoolAllocator.release(it->second, sizeof(LastFrameCollisionInfo)); + mPoolAllocator->release(it->second, sizeof(LastFrameCollisionInfo)); } lastFrameCollisionInfos.clear(); @@ -229,7 +229,7 @@ class OverlappingPairs { auto it = lastFrameCollisionInfos.find(shapesId); if (it == lastFrameCollisionInfos.end()) { - LastFrameCollisionInfo* lastFrameInfo = new (mPoolAllocator.allocate(sizeof(LastFrameCollisionInfo))) LastFrameCollisionInfo(); + LastFrameCollisionInfo* lastFrameInfo = new (mPoolAllocator->allocate(sizeof(LastFrameCollisionInfo))) LastFrameCollisionInfo(); // Add it into the map of collision infos lastFrameCollisionInfos.add(Pair(shapesId, lastFrameInfo)); @@ -258,7 +258,7 @@ class OverlappingPairs { it->second->LastFrameCollisionInfo::~LastFrameCollisionInfo(); // Release memory - mPoolAllocator.release(it->second, sizeof(LastFrameCollisionInfo)); + mPoolAllocator->release(it->second, sizeof(LastFrameCollisionInfo)); it = lastFrameCollisionInfos.remove(it); } @@ -271,11 +271,6 @@ class OverlappingPairs { } } } - - /// Destructor - virtual ~ConcaveOverlappingPair() { - - } }; private: