Fix issue in List

This commit is contained in:
Daniel Chappuis 2020-08-11 18:14:14 +02:00
parent 089d227434
commit 5dd48c195c

View File

@ -232,6 +232,11 @@ class List {
/// Copy constructor /// Copy constructor
List(const List<T>& list) : mBuffer(nullptr), mSize(0), mCapacity(0), mAllocator(list.mAllocator) { List(const List<T>& list) : mBuffer(nullptr), mSize(0), mCapacity(0), mAllocator(list.mAllocator) {
// If we need to allocate more memory
if (list.mCapacity > 0) {
reserve(list.mCapacity);
}
// All all the elements of the list to the current one // All all the elements of the list to the current one
addRange(list); addRange(list);
} }
@ -368,6 +373,27 @@ class List {
return Iterator(mBuffer, index, mSize); return Iterator(mBuffer, index, mSize);
} }
/// Remove an element from the list at a given index and replace it by the last one of the list (if any)
void removeAtAndReplaceByLast(uint index) {
assert(index >= 0 && index < mSize);
// Call the destructor
(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
(static_cast<T*>(mBuffer)[mSize - 1]).~T();
}
mSize--;
}
/// Append another list at the end of the current one /// Append another list at the end of the current one
void addRange(const List<T>& list) { void addRange(const List<T>& list) {