mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +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
1 changed files with 37 additions and 0 deletions
|
@ -5,6 +5,33 @@
|
||||||
|
|
||||||
namespace AK {
|
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>
|
template<typename T>
|
||||||
class InlineLinkedListNode {
|
class InlineLinkedListNode {
|
||||||
public:
|
public:
|
||||||
|
@ -77,6 +104,16 @@ public:
|
||||||
return false;
|
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:
|
private:
|
||||||
T* m_head { nullptr };
|
T* m_head { nullptr };
|
||||||
T* m_tail { nullptr };
|
T* m_tail { nullptr };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue