mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 17:31:58 -05:00
SinglyLinkedList: Correctly pass args to append, insert_before, insert_after
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.
This commit is contained in:
parent
5cee5725e7
commit
73d6c73b48
1 changed files with 9 additions and 21 deletions
|
@ -135,14 +135,10 @@ public:
|
|||
return value;
|
||||
}
|
||||
|
||||
void append(const T& value)
|
||||
template<typename U = T>
|
||||
void append(U&& value)
|
||||
{
|
||||
append(T(value));
|
||||
}
|
||||
|
||||
void append(T&& value)
|
||||
{
|
||||
auto* node = new Node(move(value));
|
||||
auto* node = new Node(forward<U>(value));
|
||||
if (!m_head) {
|
||||
m_head = node;
|
||||
m_tail = node;
|
||||
|
@ -201,14 +197,10 @@ public:
|
|||
delete iterator.m_node;
|
||||
}
|
||||
|
||||
void insert_before(Iterator iterator, const T& value)
|
||||
template<typename U = T>
|
||||
void insert_before(Iterator iterator, U&& value)
|
||||
{
|
||||
insert_before(iterator, T(value));
|
||||
}
|
||||
|
||||
void insert_before(Iterator iterator, T&& value)
|
||||
{
|
||||
auto* node = new Node(move(value));
|
||||
auto* node = new Node(forward<U>(value));
|
||||
node->next = iterator.m_node;
|
||||
if (m_head == iterator.m_node)
|
||||
m_head = node;
|
||||
|
@ -216,19 +208,15 @@ public:
|
|||
iterator.m_prev->next = node;
|
||||
}
|
||||
|
||||
void insert_after(Iterator iterator, const T& value)
|
||||
{
|
||||
insert_after(iterator, T(value));
|
||||
}
|
||||
|
||||
void insert_after(Iterator iterator, T&& value)
|
||||
template<typename U = T>
|
||||
void insert_after(Iterator iterator, U&& value)
|
||||
{
|
||||
if (iterator.is_end()) {
|
||||
append(value);
|
||||
return;
|
||||
}
|
||||
|
||||
auto* node = new Node(move(value));
|
||||
auto* node = new Node(forward<U>(value));
|
||||
node->next = iterator.m_node->next;
|
||||
|
||||
iterator.m_node->next = node;
|
||||
|
|
Loading…
Reference in a new issue