summaryrefslogtreecommitdiff
path: root/AK/Vector.h
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-07-20 16:10:52 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-20 16:10:52 +0200
commit67654ec529a1d4c00e20aabbc8565400ad0c0e06 (patch)
tree8300a675c06535760e4f98e966cf0166a91c65e7 /AK/Vector.h
parent26c29e59ecf582817488062ec17aba5e79e2cb80 (diff)
downloadserenity-67654ec529a1d4c00e20aabbc8565400ad0c0e06.zip
AK: Add Vector::prepend(Vector&&).
Also included a good boy unit test.
Diffstat (limited to 'AK/Vector.h')
-rw-r--r--AK/Vector.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/AK/Vector.h b/AK/Vector.h
index 09568a9adc..f8e85cb197 100644
--- a/AK/Vector.h
+++ b/AK/Vector.h
@@ -349,6 +349,31 @@ public:
++m_size;
}
+ void prepend(Vector&& other)
+ {
+ if (other.is_empty())
+ return;
+
+ if (is_empty()) {
+ *this = move(other);
+ return;
+ }
+
+ auto other_size = other.size();
+ grow_capacity(size() + other_size);
+
+ for (int i = size() + other_size - 1; i > other.size(); --i) {
+ new (slot(i)) T(move(at(i - other_size)));
+ at(i - other_size).~T();
+ }
+
+ Vector tmp = move(other);
+ for (int i = 0; i < tmp.size(); ++i)
+ new (slot(i)) T(move(tmp.at(i)));
+
+ m_size += other_size;
+ }
+
void append(const T* values, int count)
{
if (!count)