LinkedList.h 4.4 KB

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