1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 10:20:45 +00:00

AK: Add Vector::shrink_to_fit()

If there's more capacity than size, the vector is reallocated to have
capacity == size.
This commit is contained in:
Andreas Kling 2022-11-26 20:32:33 +01:00 committed by Linus Groh
parent 35ed82d5e6
commit d77ce7bae9

View File

@ -729,6 +729,18 @@ public:
MUST(try_resize_and_keep_capacity(new_size));
}
void shrink_to_fit()
{
if (size() == capacity())
return;
Vector new_vector;
new_vector.ensure_capacity(size());
for (auto& element : *this) {
new_vector.unchecked_append(move(element));
}
*this = move(new_vector);
}
using ConstIterator = SimpleIterator<Vector const, VisibleType const>;
using Iterator = SimpleIterator<Vector, VisibleType>;
using ReverseIterator = SimpleReverseIterator<Vector, VisibleType>;