LinkedList.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Copyright (c) 2008-2017 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. #if URHO3D_CXX11
  29. #include <initializer_list>
  30. #endif
  31. namespace Urho3D
  32. {
  33. /// Singly-linked list node base class.
  34. struct URHO3D_API LinkedListNode
  35. {
  36. /// Construct.
  37. LinkedListNode() :
  38. next_(0)
  39. {
  40. }
  41. /// Pointer to next node.
  42. LinkedListNode* next_;
  43. };
  44. /// Singly-linked list template class. Elements must inherit from LinkedListNode.
  45. template <class T> class LinkedList
  46. {
  47. public:
  48. /// Construct empty.
  49. LinkedList() :
  50. head_(0)
  51. {
  52. }
  53. #if URHO3D_CXX11
  54. /// Aggregate initialization constructor.
  55. LinkedList(const std::initializer_list<T>& list) : LinkedList()
  56. {
  57. for (auto it = list.begin(); it != list.end(); it++)
  58. {
  59. Insert(*it);
  60. }
  61. }
  62. #endif
  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. }
  79. /// Insert an element at the beginning.
  80. void InsertFront(T* element)
  81. {
  82. if (element)
  83. {
  84. element->next_ = head_;
  85. head_ = element;
  86. }
  87. }
  88. /// Insert an element at the end.
  89. void Insert(T* element)
  90. {
  91. if (head_)
  92. {
  93. T* tail = Last();
  94. element->next_ = tail->next_;
  95. tail->next_ = element;
  96. }
  97. else
  98. {
  99. element->next_ = head_;
  100. head_ = element;
  101. }
  102. }
  103. /// Erase an element. Return true if successful.
  104. bool Erase(T* element)
  105. {
  106. if (element && head_)
  107. {
  108. if (element == head_)
  109. {
  110. head_ = Next(element);
  111. delete element;
  112. return true;
  113. }
  114. else
  115. {
  116. T* tail = head_;
  117. while (tail && tail->next_ != element)
  118. tail = Next(tail);
  119. if (tail)
  120. {
  121. tail->next_ = element->next_;
  122. delete element;
  123. return true;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. /// Erase an element when the previous element is known (optimization.) Return true if successful.
  130. bool Erase(T* element, T* previous)
  131. {
  132. if (previous && previous->next_ == element)
  133. {
  134. previous->next_ = element->next_;
  135. delete element;
  136. return true;
  137. }
  138. else if (!previous)
  139. {
  140. if (head_ == element)
  141. {
  142. head_ = Next(element);
  143. delete element;
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. /// Return first element, or null if empty.
  150. T* First() const { return head_; }
  151. /// Return last element, or null if empty.
  152. T* Last() const
  153. {
  154. T* element = head_;
  155. if (element)
  156. {
  157. while (element->next_)
  158. element = Next(element);
  159. }
  160. return element;
  161. }
  162. /// Return next element, or null if no more elements.
  163. T* Next(T* element) const { return element ? static_cast<T*>(element->next_) : 0; }
  164. /// Return whether is empty.
  165. bool Empty() const { return head_ == 0; }
  166. private:
  167. /// First element.
  168. T* head_;
  169. };
  170. }