mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
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.
This commit is contained in:
parent
537bedbf38
commit
be5311be99
1 changed files with 14 additions and 26 deletions
40
AK/Vector.h
40
AK/Vector.h
|
@ -278,11 +278,12 @@ public:
|
||||||
m_size -= count;
|
m_size -= count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert(size_t index, T&& value)
|
template<typename U = T>
|
||||||
|
void insert(size_t index, U&& value)
|
||||||
{
|
{
|
||||||
ASSERT(index <= size());
|
ASSERT(index <= size());
|
||||||
if (index == size())
|
if (index == size())
|
||||||
return append(move(value));
|
return append(forward<U>(value));
|
||||||
grow_capacity(size() + 1);
|
grow_capacity(size() + 1);
|
||||||
++m_size;
|
++m_size;
|
||||||
if constexpr (Traits<T>::is_trivial()) {
|
if constexpr (Traits<T>::is_trivial()) {
|
||||||
|
@ -293,26 +294,21 @@ public:
|
||||||
at(i - 1).~T();
|
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)
|
template<typename C, typename U = T>
|
||||||
{
|
void insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
for (size_t i = first_index; i < size(); ++i) {
|
for (size_t i = first_index; i < size(); ++i) {
|
||||||
if (callback(at(i))) {
|
if (callback(at(i))) {
|
||||||
insert(i, move(value));
|
insert(i, forward<U>(value));
|
||||||
if (inserted_index)
|
if (inserted_index)
|
||||||
*inserted_index = i;
|
*inserted_index = i;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
append(move(value));
|
append(forward<U>(value));
|
||||||
if (inserted_index)
|
if (inserted_index)
|
||||||
*inserted_index = size() - 1;
|
*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());
|
ASSERT((size() + 1) <= capacity());
|
||||||
new (slot(m_size)) T(move(value));
|
new (slot(m_size)) T(forward<U>(value));
|
||||||
++m_size;
|
++m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void unchecked_append(const T& value)
|
|
||||||
{
|
|
||||||
unchecked_append(T(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class... Args>
|
template<class... Args>
|
||||||
void empend(Args&&... args)
|
void empend(Args&&... args)
|
||||||
{
|
{
|
||||||
|
@ -436,14 +428,10 @@ public:
|
||||||
append(T(value));
|
append(T(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepend(T&& value)
|
template<typename U = T>
|
||||||
|
void prepend(U&& value)
|
||||||
{
|
{
|
||||||
insert(0, move(value));
|
insert(0, forward<U>(value));
|
||||||
}
|
|
||||||
|
|
||||||
void prepend(const T& value)
|
|
||||||
{
|
|
||||||
insert(0, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepend(Vector&& other)
|
void prepend(Vector&& other)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue