LinkedList.h 4.3 KB

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