mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-24 16:40:21 +00:00
AK: Add DoublyLinkedList::prepend()
Also make it possible to remove() with a value-type Iterator.
This commit is contained in:
parent
7dc9c90f83
commit
61f298faf3
Notes:
sideshowbarker
2024-07-19 10:58:58 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/61f298faf3e
1 changed files with 27 additions and 1 deletions
|
@ -95,6 +95,16 @@ public:
|
|||
append_node(new Node(value));
|
||||
}
|
||||
|
||||
void prepend(T&& value)
|
||||
{
|
||||
prepend_node(new Node(move(value)));
|
||||
}
|
||||
|
||||
void prepend(const T& value)
|
||||
{
|
||||
prepend_node(new Node(value));
|
||||
}
|
||||
|
||||
bool contains_slow(const T& value) const
|
||||
{
|
||||
for (auto* node = m_head; node; node = node->next) {
|
||||
|
@ -132,7 +142,7 @@ public:
|
|||
return end();
|
||||
}
|
||||
|
||||
void remove(Iterator& it)
|
||||
void remove(Iterator it)
|
||||
{
|
||||
ASSERT(it.m_node);
|
||||
auto* node = it.m_node;
|
||||
|
@ -163,11 +173,27 @@ private:
|
|||
return;
|
||||
}
|
||||
ASSERT(m_tail);
|
||||
ASSERT(!node->next);
|
||||
m_tail->next = node;
|
||||
node->prev = m_tail;
|
||||
m_tail = node;
|
||||
}
|
||||
|
||||
void prepend_node(Node* node)
|
||||
{
|
||||
if (!m_head) {
|
||||
ASSERT(!m_tail);
|
||||
m_head = node;
|
||||
m_tail = node;
|
||||
return;
|
||||
}
|
||||
ASSERT(m_tail);
|
||||
ASSERT(!node->prev);
|
||||
m_head->prev = node;
|
||||
node->next = m_head;
|
||||
m_head = node;
|
||||
}
|
||||
|
||||
Node* head() { return m_head; }
|
||||
const Node* head() const { return m_head; }
|
||||
|
||||
|
|
Loading…
Reference in a new issue