mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-22 18:46:18 +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:
parent
35ed82d5e6
commit
d77ce7bae9
12
AK/Vector.h
12
AK/Vector.h
|
@ -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>;
|
||||
|
|
Loading…
Reference in a new issue