mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-17 16:28:06 +03:00
AK: Add insert_before/insert_after to InlineLinkedList
This commit is contained in:
parent
9a2a673e51
commit
c6230b746d
Notes:
sideshowbarker
2024-07-19 01:05:57 +09:00
Author: https://github.com/tomuta Commit: https://github.com/SerenityOS/serenity/commit/c6230b746d6 Pull-request: https://github.com/SerenityOS/serenity/pull/4298
@ -120,6 +120,8 @@ public:
|
||||
void append(T*);
|
||||
void remove(T*);
|
||||
void append(InlineLinkedList<T>&);
|
||||
void insert_before(T*, T*);
|
||||
void insert_after(T*, T*);
|
||||
|
||||
bool contains_slow(T* value) const
|
||||
{
|
||||
@ -130,6 +132,17 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
IterationDecision for_each(F func) const
|
||||
{
|
||||
for (T* node = m_head; node; node = node->next()) {
|
||||
IterationDecision decision = func(*node);
|
||||
if (decision != IterationDecision::Continue)
|
||||
return decision;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
}
|
||||
|
||||
using Iterator = InlineLinkedListIterator<T>;
|
||||
friend Iterator;
|
||||
Iterator begin() { return Iterator(m_head); }
|
||||
@ -199,6 +212,50 @@ inline void InlineLinkedList<T>::append(T* node)
|
||||
m_tail = node;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void InlineLinkedList<T>::insert_before(T* before_node, T* node)
|
||||
{
|
||||
ASSERT(before_node);
|
||||
ASSERT(node);
|
||||
ASSERT(before_node != node);
|
||||
ASSERT(!is_empty());
|
||||
if (m_head == before_node) {
|
||||
ASSERT(!before_node->prev());
|
||||
m_head = node;
|
||||
node->set_prev(0);
|
||||
node->set_next(before_node);
|
||||
before_node->set_prev(node);
|
||||
} else {
|
||||
ASSERT(before_node->prev());
|
||||
node->set_prev(before_node->prev());
|
||||
before_node->prev()->set_next(node);
|
||||
node->set_next(before_node);
|
||||
before_node->set_prev(node);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void InlineLinkedList<T>::insert_after(T* after_node, T* node)
|
||||
{
|
||||
ASSERT(after_node);
|
||||
ASSERT(node);
|
||||
ASSERT(after_node != node);
|
||||
ASSERT(!is_empty());
|
||||
if (m_tail == after_node) {
|
||||
ASSERT(!after_node->next());
|
||||
m_tail = node;
|
||||
node->set_prev(after_node);
|
||||
node->set_next(0);
|
||||
after_node->set_next(node);
|
||||
} else {
|
||||
ASSERT(after_node->next());
|
||||
node->set_prev(after_node);
|
||||
node->set_next(after_node->next());
|
||||
after_node->next()->set_prev(node);
|
||||
after_node->set_next(node);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void InlineLinkedList<T>::remove(T* node)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user