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

View File

@ -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<uint64, LastFrameCollisionInfo*>(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: