Refactor the List::removeAtAndReplaceByLast() method

This commit is contained in:
Daniel Chappuis 2020-09-04 17:51:37 +02:00
parent 068f65d972
commit 089c9ea2db
2 changed files with 9 additions and 22 deletions

View File

@ -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) /// 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) { void removeAtAndReplaceByLast(uint32 index) {
assert(index >= 0 && index < mSize); assert(index < mSize);
// Call the destructor static_cast<T*>(mBuffer)[index] = static_cast<T*>(mBuffer)[mSize - 1];
(static_cast<T*>(mBuffer)[index]).~T();
// 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<char*>(mBuffer) + index * sizeof(T)) T(static_cast<T*>(mBuffer)[mSize - 1]);
// Call the destructor of the last element // Call the destructor of the last element
(static_cast<T*>(mBuffer)[mSize - 1]).~T(); (static_cast<T*>(mBuffer)[mSize - 1]).~T();
}
mSize--; mSize--;
} }

View File

@ -174,7 +174,7 @@ class OverlappingPairs {
private: private:
MemoryAllocator& mPoolAllocator; MemoryAllocator* mPoolAllocator;
public: public:
@ -192,7 +192,7 @@ class OverlappingPairs {
ConcaveOverlappingPair(uint64 pairId, int32 broadPhaseId1, int32 broadPhaseId2, Entity collider1, Entity collider2, ConcaveOverlappingPair(uint64 pairId, int32 broadPhaseId1, int32 broadPhaseId2, Entity collider1, Entity collider2,
NarrowPhaseAlgorithmType narrowPhaseAlgorithmType, NarrowPhaseAlgorithmType narrowPhaseAlgorithmType,
bool isShape1Convex, MemoryAllocator& poolAllocator, MemoryAllocator& heapAllocator) 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) { isShape1Convex(isShape1Convex), lastFrameCollisionInfos(heapAllocator, 16) {
} }
@ -206,7 +206,7 @@ class OverlappingPairs {
it->second->LastFrameCollisionInfo::~LastFrameCollisionInfo(); it->second->LastFrameCollisionInfo::~LastFrameCollisionInfo();
// Release memory // Release memory
mPoolAllocator.release(it->second, sizeof(LastFrameCollisionInfo)); mPoolAllocator->release(it->second, sizeof(LastFrameCollisionInfo));
} }
lastFrameCollisionInfos.clear(); lastFrameCollisionInfos.clear();
@ -229,7 +229,7 @@ class OverlappingPairs {
auto it = lastFrameCollisionInfos.find(shapesId); auto it = lastFrameCollisionInfos.find(shapesId);
if (it == lastFrameCollisionInfos.end()) { 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 // Add it into the map of collision infos
lastFrameCollisionInfos.add(Pair<uint64, LastFrameCollisionInfo*>(shapesId, lastFrameInfo)); lastFrameCollisionInfos.add(Pair<uint64, LastFrameCollisionInfo*>(shapesId, lastFrameInfo));
@ -258,7 +258,7 @@ class OverlappingPairs {
it->second->LastFrameCollisionInfo::~LastFrameCollisionInfo(); it->second->LastFrameCollisionInfo::~LastFrameCollisionInfo();
// Release memory // Release memory
mPoolAllocator.release(it->second, sizeof(LastFrameCollisionInfo)); mPoolAllocator->release(it->second, sizeof(LastFrameCollisionInfo));
it = lastFrameCollisionInfos.remove(it); it = lastFrameCollisionInfos.remove(it);
} }
@ -271,11 +271,6 @@ class OverlappingPairs {
} }
} }
} }
/// Destructor
virtual ~ConcaveOverlappingPair() {
}
}; };
private: private: