summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-03 18:38:16 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-03 19:14:50 +0200
commitbaadde122b9bf3e77e51b003670d68bbd477afe2 (patch)
treeb5e74120704cb7536e6918bd41a9431d8ee73d83 /AK
parente7abbc66556e0fbb6227e48b9df0734ef6ab2c96 (diff)
downloadserenity-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.h16
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()); }