mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-10-26 06:49:12 +03: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
Notes:
sideshowbarker
2024-07-17 05:19:06 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d77ce7bae9 Pull-request: https://github.com/SerenityOS/serenity/pull/16215 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/trflynn89
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 New Issue
Block a user