diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-03 18:38:16 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-03 19:14:50 +0200 |
commit | baadde122b9bf3e77e51b003670d68bbd477afe2 (patch) | |
tree | b5e74120704cb7536e6918bd41a9431d8ee73d83 /AK | |
parent | e7abbc66556e0fbb6227e48b9df0734ef6ab2c96 (diff) | |
download | serenity-baadde122b9bf3e77e51b003670d68bbd477afe2.zip |
AK: Add Vector::resize_and_keep_capacity()
This function is just like resize() except it does not deallocate the
vector buffer when shrinking.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Vector.h | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/AK/Vector.h b/AK/Vector.h index a6218d1461..1b40a8f509 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -539,14 +539,17 @@ public: m_capacity = new_capacity; } - void shrink(size_t new_size) + void shrink(size_t new_size, bool keep_capacity = false) { ASSERT(new_size <= size()); if (new_size == size()) return; if (!new_size) { - clear(); + if (keep_capacity) + clear_with_capacity(); + else + clear(); return; } @@ -555,10 +558,10 @@ public: m_size = new_size; } - void resize(size_t new_size) + void resize(size_t new_size, bool keep_capacity = false) { if (new_size <= size()) - return shrink(new_size); + return shrink(new_size, keep_capacity); ensure_capacity(new_size); for (size_t i = size(); i < new_size; ++i) @@ -566,6 +569,11 @@ public: m_size = new_size; } + void resize_and_keep_capacity(size_t new_size) + { + return resize(new_size, true); + } + using Iterator = VectorIterator<Vector, T>; Iterator begin() { return Iterator(*this, 0); } Iterator end() { return Iterator(*this, size()); } |