LinkedList.h 4.6 KB

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