| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #pragma once
- #ifdef URHO3D_IS_BUILDING
- #include "Urho3D.h"
- #else
- #include <Urho3D/Urho3D.h>
- #endif
- #include <initializer_list>
- namespace Urho3D
- {
- /// Singly-linked list node base class.
- struct URHO3D_API LinkedListNode
- {
- /// Construct.
- LinkedListNode() :
- next_(nullptr)
- {
- }
- /// Pointer to next node.
- LinkedListNode* next_;
- };
- /// Singly-linked list template class. Elements must inherit from LinkedListNode.
- template <class T> class LinkedList
- {
- public:
- /// Construct empty.
- LinkedList() :
- head_(nullptr)
- {
- }
- /// Non-copyable.
- LinkedList(const LinkedList<T>& list) = delete;
- /// Aggregate initialization constructor.
- LinkedList(const std::initializer_list<T>& list) : LinkedList()
- {
- for (auto it = list.begin(); it != list.end(); it++)
- {
- Insert(*it);
- }
- }
- /// Non-assignable.
- LinkedList<T>& operator =(const LinkedList<T>& list) = delete;
- /// Destruct.
- ~LinkedList()
- {
- Clear();
- }
- /// Remove all elements.
- void Clear()
- {
- T* element = head_;
- while (element)
- {
- T* next = Next(element);
- delete element;
- element = next;
- }
- head_ = nullptr;
- }
- /// Insert an element at the beginning.
- void InsertFront(T* element)
- {
- if (element)
- {
- element->next_ = head_;
- head_ = element;
- }
- }
- /// Insert an element at the end.
- void Insert(T* element)
- {
- if (head_)
- {
- T* tail = Last();
- element->next_ = tail->next_;
- tail->next_ = element;
- }
- else
- {
- element->next_ = head_;
- head_ = element;
- }
- }
- /// Erase an element. Return true if successful.
- bool Erase(T* element)
- {
- if (element && head_)
- {
- if (element == head_)
- {
- head_ = Next(element);
- delete element;
- return true;
- }
- else
- {
- T* tail = head_;
- while (tail && tail->next_ != element)
- tail = Next(tail);
- if (tail)
- {
- tail->next_ = element->next_;
- delete element;
- return true;
- }
- }
- }
- return false;
- }
- /// Erase an element when the previous element is known (optimization). Return true if successful.
- bool Erase(T* element, T* previous)
- {
- if (previous && previous->next_ == element)
- {
- previous->next_ = element->next_;
- delete element;
- return true;
- }
- else if (!previous)
- {
- if (head_ == element)
- {
- head_ = Next(element);
- delete element;
- return true;
- }
- }
- return false;
- }
- /// Return first element, or null if empty.
- T* First() const { return head_; }
- /// Return last element, or null if empty.
- T* Last() const
- {
- T* element = head_;
- if (element)
- {
- while (element->next_)
- element = Next(element);
- }
- return element;
- }
- /// Return next element, or null if no more elements.
- T* Next(T* element) const { return element ? static_cast<T*>(element->next_) : nullptr; }
- /// Return whether is empty.
- bool Empty() const { return head_ == nullptr; }
- private:
- /// First element.
- T* head_;
- };
- }
|