libluna: Add support for range-based iteration to LinkedList
This commit is contained in:
parent
77dcfab5ef
commit
f3ffdd32d4
@ -210,6 +210,47 @@ template <typename T> class LinkedList
|
||||
return m_count;
|
||||
}
|
||||
|
||||
struct LinkedListIterator
|
||||
{
|
||||
typedef T* PtrT;
|
||||
|
||||
private:
|
||||
LinkedListIterator(PtrT ptr, LinkedList<T>& list) : m_ptr(ptr), m_list(list)
|
||||
{
|
||||
}
|
||||
|
||||
PtrT m_ptr;
|
||||
LinkedList<T>& m_list;
|
||||
|
||||
public:
|
||||
PtrT& operator*()
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
void operator++()
|
||||
{
|
||||
m_ptr = m_list.next(m_ptr).value_or(nullptr);
|
||||
}
|
||||
|
||||
bool operator!=(LinkedListIterator& other)
|
||||
{
|
||||
return m_ptr != other.m_ptr || &m_list != &other.m_list;
|
||||
}
|
||||
|
||||
friend class LinkedList<T>;
|
||||
};
|
||||
|
||||
LinkedListIterator begin()
|
||||
{
|
||||
return { (T*)m_start_node, *this };
|
||||
}
|
||||
|
||||
LinkedListIterator end()
|
||||
{
|
||||
return { nullptr, *this };
|
||||
}
|
||||
|
||||
private:
|
||||
Node* m_start_node = nullptr;
|
||||
Node* m_end_node = nullptr;
|
||||
|
Loading…
Reference in New Issue
Block a user