LinkedList.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #ifdef URHO3D_IS_BUILDING
  24. #include "Urho3D.h"
  25. #else
  26. #include <Urho3D/Urho3D.h>
  27. #endif
  28. #include <initializer_list>
  29. namespace Urho3D
  30. {
  31. /// Singly-linked list node base class.
  32. struct URHO3D_API LinkedListNode
  33. {
  34. /// Construct.
  35. LinkedListNode() :
  36. next_(nullptr)
  37. {
  38. }
  39. /// Pointer to next node.
  40. LinkedListNode* next_;
  41. };
  42. /// Singly-linked list template class. Elements must inherit from LinkedListNode.
  43. template <class T> class LinkedList
  44. {
  45. public:
  46. /// Construct empty.
  47. LinkedList() :
  48. head_(nullptr)
  49. {
  50. }
  51. /// Non-copyable.
  52. LinkedList(const LinkedList<T>& list) = delete;
  53. /// Aggregate initialization constructor.
  54. LinkedList(const std::initializer_list<T>& list) : LinkedList()
  55. {
  56. for (auto it = list.begin(); it != list.end(); it++)
  57. {
  58. Insert(*it);
  59. }
  60. }
  61. /// Non-assignable.
  62. LinkedList<T>& operator =(const LinkedList<T>& list) = delete;
  63. /// Destruct.
  64. ~LinkedList()
  65. {
  66. Clear();
  67. }
  68. /// Remove all elements.
  69. void Clear()
  70. {
  71. T* element = head_;
  72. while (element)
  73. {
  74. T* next = Next(element);
  75. delete element;
  76. element = next;
  77. }
  78. head_ = nullptr;
  79. }
  80. /// Insert an element at the beginning.
  81. void InsertFront(T* element)
  82. {
  83. if (element)
  84. {
  85. element->next_ = head_;
  86. head_ = element;
  87. }
  88. }
  89. /// Insert an element at the end.
  90. void Insert(T* element)
  91. {
  92. if (head_)
  93. {
  94. T* tail = Last();
  95. element->next_ = tail->next_;
  96. tail->next_ = element;
  97. }
  98. else
  99. {
  100. element->next_ = head_;
  101. head_ = element;
  102. }
  103. }
  104. /// Erase an element. Return true if successful.
  105. bool Erase(T* element)
  106. {
  107. if (element && head_)
  108. {
  109. if (element == head_)
  110. {
  111. head_ = Next(element);
  112. delete element;
  113. return true;
  114. }
  115. else
  116. {
  117. T* tail = head_;
  118. while (tail && tail->next_ != element)
  119. tail = Next(tail);
  120. if (tail)
  121. {
  122. tail->next_ = element->next_;
  123. delete element;
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. /// Erase an element when the previous element is known (optimization). Return true if successful.
  131. bool Erase(T* element, T* previous)
  132. {
  133. if (previous && previous->next_ == element)
  134. {
  135. previous->next_ = element->next_;
  136. delete element;
  137. return true;
  138. }
  139. else if (!previous)
  140. {
  141. if (head_ == element)
  142. {
  143. head_ = Next(element);
  144. delete element;
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. /// Return first element, or null if empty.
  151. T* First() const { return head_; }
  152. /// Return last element, or null if empty.
  153. T* Last() const
  154. {
  155. T* element = head_;
  156. if (element)
  157. {
  158. while (element->next_)
  159. element = Next(element);
  160. }
  161. return element;
  162. }
  163. /// Return next element, or null if no more elements.
  164. T* Next(T* element) const { return element ? static_cast<T*>(element->next_) : nullptr; }
  165. /// Return whether is empty.
  166. bool Empty() const { return head_ == nullptr; }
  167. private:
  168. /// First element.
  169. T* head_;
  170. };
  171. }