summaryrefslogtreecommitdiff
path: root/AK/Vector.h
diff options
context:
space:
mode:
authorLenny Maiorani <lenny@colorado.edu>2021-01-15 16:04:16 -0700
committerAndreas Kling <kling@serenityos.org>2021-01-31 10:48:12 +0100
commitbe5311be99bc96cd23273ce4517b806783ee4492 (patch)
tree6051bae48137f012eae361ad76ff50843b04af84 /AK/Vector.h
parent537bedbf38889d5946199a9bfc6e2e0df66503c5 (diff)
downloadserenity-be5311be99bc96cd23273ce4517b806783ee4492.zip
Vector: Correctly pass args to insert, insert_before_matching, prepend
Problem: - Using regular functions rather than function templates results in the arguments not being deduced. This then requires the same function to be written multiple times and for `move` to be used rather than `forward`. Solution: - Collapse multiple function overloads to a single function template with a deduced argument. This allows the argument to be a forwarding reference and bind to either an l-value or r-value and forward the value. Note: - `append` is not being changed because there are several overloads for appending single values and concatenating vectors. This conflation needs to be addressed first.
Diffstat (limited to 'AK/Vector.h')
-rw-r--r--AK/Vector.h40
1 files changed, 14 insertions, 26 deletions
diff --git a/AK/Vector.h b/AK/Vector.h
index 6e7f5b1886..97b3b790cf 100644
--- a/AK/Vector.h
+++ b/AK/Vector.h
@@ -278,11 +278,12 @@ public:
m_size -= count;
}
- void insert(size_t index, T&& value)
+ template<typename U = T>
+ void insert(size_t index, U&& value)
{
ASSERT(index <= size());
if (index == size())
- return append(move(value));
+ return append(forward<U>(value));
grow_capacity(size() + 1);
++m_size;
if constexpr (Traits<T>::is_trivial()) {
@@ -293,26 +294,21 @@ public:
at(i - 1).~T();
}
}
- new (slot(index)) T(move(value));
+ new (slot(index)) T(forward<U>(value));
}
- void insert(size_t index, const T& value)
- {
- insert(index, T(value));
- }
-
- template<typename C>
- void insert_before_matching(T&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
+ template<typename C, typename U = T>
+ void insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
{
for (size_t i = first_index; i < size(); ++i) {
if (callback(at(i))) {
- insert(i, move(value));
+ insert(i, forward<U>(value));
if (inserted_index)
*inserted_index = i;
return;
}
}
- append(move(value));
+ append(forward<U>(value));
if (inserted_index)
*inserted_index = size() - 1;
}
@@ -404,18 +400,14 @@ public:
}
}
- ALWAYS_INLINE void unchecked_append(T&& value)
+ template<typename U = T>
+ ALWAYS_INLINE void unchecked_append(U&& value)
{
ASSERT((size() + 1) <= capacity());
- new (slot(m_size)) T(move(value));
+ new (slot(m_size)) T(forward<U>(value));
++m_size;
}
- ALWAYS_INLINE void unchecked_append(const T& value)
- {
- unchecked_append(T(value));
- }
-
template<class... Args>
void empend(Args&&... args)
{
@@ -436,14 +428,10 @@ public:
append(T(value));
}
- void prepend(T&& value)
- {
- insert(0, move(value));
- }
-
- void prepend(const T& value)
+ template<typename U = T>
+ void prepend(U&& value)
{
- insert(0, value);
+ insert(0, forward<U>(value));
}
void prepend(Vector&& other)