10#ifndef SEMF_UTILS_CORE_LISTS_LINKEDLIST_H_
11#define SEMF_UTILS_CORE_LISTS_LINKEDLIST_H_
99 if (m_previous !=
nullptr || m_next !=
nullptr)
109 T* m_previous =
nullptr;
163 m_node = m_node->LinkedList::Node::next();
175 m_node = m_node->LinkedList::Node::next();
186 m_node = m_node->LinkedList::Node::previous();
198 temp.m_node = temp.m_node->LinkedList::Node::previous();
209 return m_node == that.m_node;
219 return m_node != that.m_node;
253 : m_node(iterator.m_node)
286 m_node = m_node->LinkedList::Node::next();
298 m_node = m_node->LinkedList::Node::next();
309 m_node = m_node->LinkedList::Node::previous();
321 m_node = m_node->LinkedList::Node::previous();
332 return m_node == other.m_node;
342 return m_node != other.m_node;
347 const T* m_node =
nullptr;
434 ConstIterator
end()
const;
470 Iterator
insert(Iterator position, T& element);
490 Iterator
erase(Iterator first, Iterator last);
532 void pushEmpty(T& element);
534 T* m_front =
nullptr;
562 return *m_end.previous();
568 return *m_end.previous();
574 return m_front ?
Iterator(m_front) : end();
592 return Iterator(
reinterpret_cast<T*
>(&m_end));
623 m_end.setPrevious(m_front);
631 if (&(*position) == m_front)
636 else if (m_size == 0)
639 m_end.setPrevious(&element);
645 T* nex = &(*position);
646 nex->LinkedList::Node::setPrevious(&element);
647 T* pre = &(*position--);
648 pre->LinkedList::Node::setNext(&element);
656 if (&(*position) == m_front)
661 else if (&(*position) == m_end.previous())
670 pre->LinkedList::Node::setNext(nex);
671 nex->LinkedList::Node::setPrevious(pre);
681 while (first != last)
683 first = erase(first);
691 element.LinkedList::Node::setNext(
nullptr);
695 back().LinkedList::Node::setNext(&element);
696 element.LinkedList::Node::setPrevious(m_end.previous());
697 element.LinkedList::Node::setNext(
reinterpret_cast<T*
>(&m_end));
698 m_end.setPrevious(&element);
710 element.LinkedList::Node::setPrevious(
nullptr);
713 element.LinkedList::Node::setNext(m_front);
714 m_front->LinkedList::Node::setPrevious(element);
734 m_end.setPrevious(&(*newPrev));
735 newPrev->LinkedList::Node::setNext(
reinterpret_cast<T*
>(&m_end));
749 m_front = m_front->LinkedList::Node::next();
757 this->m_front = list.m_front;
758 this->m_end = list.m_end;
759 this->m_size = list.m_size;
765 element.LinkedList::Node::setPrevious(
nullptr);
766 element.LinkedList::Node::setNext(
reinterpret_cast<T*
>(&m_end));
767 m_end.setPrevious(&element);
Implementation of a bidirectional constant iterator for LinkedList.
ConstIterator(const Iterator &iterator)
Copy constructor.
ConstIterator & operator--()
Iterates to the previous element in the list.
std::ptrdiff_t difference_type
const T * operator->() const
Returns the pointer into the element the iterator's position.
std::bidirectional_iterator_tag iterator_category
const T & operator*() const
Returns the reference of the element the iterator's position.
ConstIterator operator--(int)
Iterates to the previous element in the list.
bool operator==(const ConstIterator &other) const
Compares this element with that element.
ConstIterator(const T *element)
Constructor with member variable initialization.
ConstIterator & operator++()
Iterates to the next element in the list.
ConstIterator operator++(int)
Iterates to the next element in the list.
bool operator!=(const ConstIterator &other) const
Compares this element with that element.
virtual ~ConstIterator()=default
Implementation of a bidirectional iterator for LinkedList.
Iterator operator++(int)
Iterates to the next element in the list.
std::ptrdiff_t difference_type
T & operator*() const
Returns the reference of the element the iterator's position.
std::bidirectional_iterator_tag iterator_category
Iterator(T *element)
Constructor with member variable initialization.
bool operator!=(const Iterator &that) const
Compares this element with that element.
virtual ~Iterator()=default
Iterator & operator++()
Iterates to the next element in the list.
T * operator->() const
Returns the pointer into the element the iterator's position.
Iterator operator--(int)
Iterates to the previous element in the list.
Iterator & operator--()
Iterates to the previous element in the list.
bool operator==(const Iterator &that) const
Compares this element with that element.
Implements the previous() and next() functionality for every element in a list.
bool isInAList()
Returns if a node is part of a LinkedList.
void setPrevious(T *previous)
Sets a pointer to the previous element in a list.
T * previous() const
Returns a pointer to the previous element in a list.
void setNext(T *next)
Sets a pointer to the next element in a list.
T * next() const
Returns a pointer to the next element in a list.
LinkedList is an managed double linked list implementation.
void popFront()
Removes the first element in the list, effectively reducing the list size by one.
void clear()
Removes all elements from the list and leaving the list with a size of 0.
LinkedList()
Creates an empty List.
ConstIterator end() const
Returns an iterator referring to the past-the-end element in the list.
size_t size() const
Returns the number of elements in the list.
bool empty() const
Returns whether the list is empty (i.e. whether its size is 0).
Iterator begin()
Returns an iterator pointing to the first element in the list.
T & front()
Returns a reference to the first element in the list.
void popBack()
Removes the last element in the list, effectively reducing the list size by one.
LinkedList< T > & operator=(const LinkedList< T > &list)
Assigns new contents to the list, replacing its current contents, and modifying its size accordingly.
Iterator insert(Iterator position, T &element)
The list is extended by inserting new elements before the element at the specified position.
const T & front() const
Returns a reference to the first element in the list.
void pushFront(T &element)
Adds a new element to the beginning of the list, before its current first element.
ConstIterator cbegin() const
Returns an iterator pointing to the first element in the list.
const T & back() const
Returns a reference to the last element in the list.
T & back()
Returns a reference to the last element in the list.
Iterator erase(Iterator first, Iterator last)
Removes multiple elements from the list.
ConstIterator cend() const
Returns an iterator referring to the past-the-end element in the list.
void pushBack(T &element)
Adds a new element to the end of the list, after its current last element.
Iterator erase(Iterator position)
Removes a single element from the list.
Iterator end()
Returns an iterator referring to the past-the-end element in the list.
ConstIterator begin() const
Returns an iterator pointing to the first element in the list.