LinkedList.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #ifdef URHO3D_IS_BUILDING
  5. #include "Urho3D.h"
  6. #else
  7. #include <Urho3D/Urho3D.h>
  8. #endif
  9. #include <initializer_list>
  10. namespace Urho3D
  11. {
  12. /// Singly-linked list node base class.
  13. struct URHO3D_API LinkedListNode
  14. {
  15. /// Construct.
  16. LinkedListNode() :
  17. next_(nullptr)
  18. {
  19. }
  20. /// Pointer to next node.
  21. LinkedListNode* next_;
  22. };
  23. /// Singly-linked list template class. Elements must inherit from LinkedListNode.
  24. template <class T> class LinkedList
  25. {
  26. public:
  27. /// Construct empty.
  28. LinkedList() :
  29. head_(nullptr)
  30. {
  31. }
  32. /// Non-copyable.
  33. LinkedList(const LinkedList<T>& list) = delete;
  34. /// Aggregate initialization constructor.
  35. LinkedList(const std::initializer_list<T>& list) : LinkedList()
  36. {
  37. for (auto it = list.begin(); it != list.end(); it++)
  38. {
  39. Insert(*it);
  40. }
  41. }
  42. /// Non-assignable.
  43. LinkedList<T>& operator =(const LinkedList<T>& list) = delete;
  44. /// Destruct.
  45. ~LinkedList()
  46. {
  47. Clear();
  48. }
  49. /// Remove all elements.
  50. void Clear()
  51. {
  52. T* element = head_;
  53. while (element)
  54. {
  55. T* next = Next(element);
  56. delete element;
  57. element = next;
  58. }
  59. head_ = nullptr;
  60. }
  61. /// Insert an element at the beginning.
  62. void InsertFront(T* element)
  63. {
  64. if (element)
  65. {
  66. element->next_ = head_;
  67. head_ = element;
  68. }
  69. }
  70. /// Insert an element at the end.
  71. void Insert(T* element)
  72. {
  73. if (head_)
  74. {
  75. T* tail = Last();
  76. element->next_ = tail->next_;
  77. tail->next_ = element;
  78. }
  79. else
  80. {
  81. element->next_ = head_;
  82. head_ = element;
  83. }
  84. }
  85. /// Erase an element. Return true if successful.
  86. bool Erase(T* element)
  87. {
  88. if (element && head_)
  89. {
  90. if (element == head_)
  91. {
  92. head_ = Next(element);
  93. delete element;
  94. return true;
  95. }
  96. else
  97. {
  98. T* tail = head_;
  99. while (tail && tail->next_ != element)
  100. tail = Next(tail);
  101. if (tail)
  102. {
  103. tail->next_ = element->next_;
  104. delete element;
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }
  111. /// Erase an element when the previous element is known (optimization). Return true if successful.
  112. bool Erase(T* element, T* previous)
  113. {
  114. if (previous && previous->next_ == element)
  115. {
  116. previous->next_ = element->next_;
  117. delete element;
  118. return true;
  119. }
  120. else if (!previous)
  121. {
  122. if (head_ == element)
  123. {
  124. head_ = Next(element);
  125. delete element;
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. /// Return first element, or null if empty.
  132. T* First() const { return head_; }
  133. /// Return last element, or null if empty.
  134. T* Last() const
  135. {
  136. T* element = head_;
  137. if (element)
  138. {
  139. while (element->next_)
  140. element = Next(element);
  141. }
  142. return element;
  143. }
  144. /// Return next element, or null if no more elements.
  145. T* Next(T* element) const { return element ? static_cast<T*>(element->next_) : nullptr; }
  146. /// Return whether is empty.
  147. bool Empty() const { return head_ == nullptr; }
  148. private:
  149. /// First element.
  150. T* head_;
  151. };
  152. }