LinkedList.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Copyright (c) 2008-2014 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. namespace Atomic
  24. {
  25. /// Singly-linked list node base class.
  26. struct ATOMIC_API LinkedListNode
  27. {
  28. /// Construct.
  29. LinkedListNode() :
  30. next_(0)
  31. {
  32. }
  33. /// Pointer to next node.
  34. LinkedListNode* next_;
  35. };
  36. /// Singly-linked list template class. Elements must inherit from LinkedListNode.
  37. template <class T> class LinkedList
  38. {
  39. public:
  40. /// Construct empty.
  41. LinkedList() :
  42. head_(0)
  43. {
  44. }
  45. /// Destruct.
  46. ~LinkedList()
  47. {
  48. Clear();
  49. }
  50. /// Remove all elements.
  51. void Clear()
  52. {
  53. T* element = head_;
  54. while (element)
  55. {
  56. T* next = Next(element);
  57. delete element;
  58. element = next;
  59. }
  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_) : 0; }
  146. /// Return whether is empty.
  147. bool Empty() const { return head_ == 0; }
  148. private:
  149. /// First element.
  150. T* head_;
  151. };
  152. }