mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 21:10:30 +00:00
AK: Add an iterator class for InlineLinkedList
This makes it possible to iterate over these with range-for. :^)
This commit is contained in:
parent
318068fe1b
commit
bb9909548b
Notes:
sideshowbarker
2024-07-19 12:49:14 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/bb9909548b3
1 changed files with 37 additions and 0 deletions
|
@ -5,6 +5,33 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class InlineLinkedList;
|
||||
|
||||
template<typename T>
|
||||
class InlineLinkedListIterator {
|
||||
public:
|
||||
bool operator!=(const InlineLinkedListIterator& other) const { return m_node != other.m_node; }
|
||||
bool operator==(const InlineLinkedListIterator& other) const { return m_node == other.m_node; }
|
||||
InlineLinkedListIterator& operator++()
|
||||
{
|
||||
m_node = m_node->next();
|
||||
return *this;
|
||||
}
|
||||
T& operator*() { return *m_node; }
|
||||
T* operator->() { return &m_node; }
|
||||
bool is_end() const { return !m_node; }
|
||||
static InlineLinkedListIterator universal_end() { return InlineLinkedListIterator(nullptr); }
|
||||
|
||||
private:
|
||||
friend InlineLinkedList<T>;
|
||||
explicit InlineLinkedListIterator(T* node)
|
||||
: m_node(node)
|
||||
{
|
||||
}
|
||||
T* m_node;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class InlineLinkedListNode {
|
||||
public:
|
||||
|
@ -77,6 +104,16 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
using Iterator = InlineLinkedListIterator<T>;
|
||||
friend Iterator;
|
||||
Iterator begin() { return Iterator(m_head); }
|
||||
Iterator end() { return Iterator::universal_end(); }
|
||||
|
||||
using ConstIterator = InlineLinkedListIterator<const T>;
|
||||
friend ConstIterator;
|
||||
ConstIterator begin() const { return ConstIterator(m_head); }
|
||||
ConstIterator end() const { return ConstIterator::universal_end(); }
|
||||
|
||||
private:
|
||||
T* m_head { nullptr };
|
||||
T* m_tail { nullptr };
|
||||
|
|
Loading…
Reference in a new issue