List.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #include "ListBase.h"
  25. /// Doubly-linked list template class.
  26. template <class T> class List : public ListBase
  27. {
  28. public:
  29. /// %List node.
  30. struct Node : public ListNodeBase
  31. {
  32. /// Construct undefined.
  33. Node()
  34. {
  35. }
  36. /// Construct with value.
  37. Node(const T& value) :
  38. value_(value)
  39. {
  40. }
  41. /// Node value.
  42. T value_;
  43. /// Return next node.
  44. Node* Next() const { return static_cast<Node*>(next_); }
  45. /// Return previous node.
  46. Node* Prev() { return static_cast<Node*>(prev_); }
  47. };
  48. /// %List iterator.
  49. struct Iterator : public ListIteratorBase
  50. {
  51. /// Construct.
  52. Iterator()
  53. {
  54. }
  55. /// Construct with a node pointer.
  56. explicit Iterator(Node* ptr) :
  57. ListIteratorBase(ptr)
  58. {
  59. }
  60. /// Preincrement the pointer.
  61. Iterator& operator ++ () { GotoNext(); return *this; }
  62. /// Postincrement the pointer.
  63. Iterator operator ++ (int) { Iterator it = *this; GotoNext(); return it; }
  64. /// Predecrement the pointer.
  65. Iterator& operator -- () { GotoPrev(); return *this; }
  66. /// Postdecrement the pointer.
  67. Iterator operator -- (int) { Iterator it = *this; GotoPrev(); return it; }
  68. /// Point to the node value.
  69. T* operator -> () const { return &(static_cast<Node*>(ptr_))->value_; }
  70. /// Dereference the node value.
  71. T& operator * () const { return (static_cast<Node*>(ptr_))->value_; }
  72. };
  73. /// %List const iterator.
  74. struct ConstIterator : public ListIteratorBase
  75. {
  76. /// Construct.
  77. ConstIterator()
  78. {
  79. }
  80. /// Construct with a node pointer.
  81. explicit ConstIterator(Node* ptr) :
  82. ListIteratorBase(ptr)
  83. {
  84. }
  85. /// Construct from a non-const iterator.
  86. ConstIterator(const Iterator& rhs) :
  87. ListIteratorBase(rhs.ptr_)
  88. {
  89. }
  90. /// Assign from a non-const iterator.
  91. ConstIterator& operator = (const Iterator& rhs) { ptr_ = rhs.ptr_; return *this; }
  92. /// Preincrement the pointer.
  93. ConstIterator& operator ++ () { GotoNext(); return *this; }
  94. /// Postincrement the pointer.
  95. ConstIterator operator ++ (int) { ConstIterator it = *this; GotoNext(); return it; }
  96. /// Predecrement the pointer.
  97. ConstIterator& operator -- () { GotoPrev(); return *this; }
  98. /// Postdecrement the pointer.
  99. ConstIterator operator -- (int) { ConstIterator it = *this; GotoPrev(); return it; }
  100. /// Point to the node value.
  101. const T* operator -> () const { return &(static_cast<Node*>(ptr_))->value_; }
  102. /// Dereference the node value.
  103. const T& operator * () const { return (static_cast<Node*>(ptr_))->value_; }
  104. };
  105. /// Construct empty.
  106. List()
  107. {
  108. allocator_ = AllocatorInitialize(sizeof(Node));
  109. head_ = tail_ = ReserveNode();
  110. }
  111. /// Construct from another list.
  112. List(const List<T>& list)
  113. {
  114. allocator_ = AllocatorInitialize(sizeof(Node));
  115. head_ = tail_ = ReserveNode();
  116. *this = list;
  117. }
  118. /// Destruct.
  119. ~List()
  120. {
  121. Clear();
  122. FreeNode(Tail());
  123. AllocatorUninitialize(allocator_);
  124. }
  125. /// Assign from another list.
  126. List& operator = (const List<T>& rhs)
  127. {
  128. // Clear, then insert the nodes of the other list
  129. Clear();
  130. Insert(End(), rhs);
  131. return *this;
  132. }
  133. /// Add-assign an element.
  134. List& operator += (const T& rhs)
  135. {
  136. Push(rhs);
  137. return *this;
  138. }
  139. /// Add-assign a list.
  140. List& operator += (const List<T>& rhs)
  141. {
  142. Insert(End(), rhs);
  143. return *this;
  144. }
  145. /// Test for equality with another list.
  146. bool operator == (const List<T>& rhs) const
  147. {
  148. if (rhs.size_ != size_)
  149. return false;
  150. ConstIterator i = Begin();
  151. ConstIterator j = rhs.Begin();
  152. while (i != End())
  153. {
  154. if (*i != *j)
  155. return false;
  156. ++i;
  157. ++j;
  158. }
  159. return true;
  160. }
  161. /// Test for inequality with another list.
  162. bool operator != (const List<T>& rhs) const
  163. {
  164. if (rhs.size_ != size_)
  165. return true;
  166. ConstIterator i = Begin();
  167. ConstIterator j = rhs.Begin();
  168. while (i != End())
  169. {
  170. if (*i != *j)
  171. return true;
  172. ++i;
  173. ++j;
  174. }
  175. return false;
  176. }
  177. /// Insert an element to the end.
  178. void Push(const T& value) { InsertNode(Tail(), value); }
  179. /// Insert an element to the beginning.
  180. void PushFront(const T& value) { InsertNode(Head(), value); }
  181. /// Insert an element at position.
  182. void Insert(const Iterator& dest, const T& value) { InsertNode(static_cast<Node*>(dest.ptr_), value); }
  183. /// Insert a list at position.
  184. void Insert(const Iterator& dest, const List<T>& list)
  185. {
  186. Node* destNode = static_cast<Node*>(dest.ptr_);
  187. ConstIterator it = list.Begin();
  188. ConstIterator end = list.End();
  189. while (it != end)
  190. InsertNode(destNode, *it++);
  191. }
  192. /// Insert elements by iterators.
  193. void Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  194. {
  195. Node* destNode = static_cast<Node*>(dest.ptr_);
  196. ConstIterator it = start;
  197. while (it != end)
  198. InsertNode(destNode, *it++);
  199. }
  200. /// Insert elements.
  201. void Insert(const Iterator& dest, const T* start, const T* end)
  202. {
  203. Node* destNode = static_cast<Node*>(dest.ptr_);
  204. const T* ptr = start;
  205. while (ptr != end)
  206. InsertNode(destNode, *ptr++);
  207. }
  208. /// Erase the last element.
  209. void Pop()
  210. {
  211. if (size_)
  212. Erase(--End());
  213. }
  214. /// Erase the first element.
  215. void PopFront()
  216. {
  217. if (size_)
  218. Erase(Begin());
  219. }
  220. /// Erase an element. Return an iterator to the next element.
  221. Iterator Erase(Iterator it)
  222. {
  223. return Iterator(EraseNode(static_cast<Node*>(it.ptr_)));
  224. }
  225. /// Erase a range by iterators. Return an iterator to the next element.
  226. Iterator Erase(const Iterator& start, const Iterator& end)
  227. {
  228. Iterator it = start;
  229. while (it != end)
  230. it = EraseNode(static_cast<Node*>(it.ptr_));
  231. return it;
  232. }
  233. /// Clear the list.
  234. void Clear()
  235. {
  236. while (size_)
  237. EraseNode(Head());
  238. }
  239. /// Resize the list by removing or adding items at the end.
  240. void Resize(unsigned newSize)
  241. {
  242. while (size_ > newSize)
  243. Pop();
  244. while (size_ < newSize)
  245. InsertNode(Tail(), T());
  246. }
  247. /// Return iterator to value, or to the end if not found.
  248. Iterator Find(const T& value)
  249. {
  250. Iterator it = Begin();
  251. while (it != End() && *it != value)
  252. ++it;
  253. return it;
  254. }
  255. /// Return const iterator to value, or to the end if not found.
  256. ConstIterator Find(const T& value) const
  257. {
  258. ConstIterator it = Begin();
  259. while (it != End() && *it != value)
  260. ++it;
  261. return it;
  262. }
  263. /// Return whether contains a specific value.
  264. bool Contains(const T& value) const { return Find(value) != End(); }
  265. /// Return iterator to the first element.
  266. Iterator Begin() { return Iterator(Head()); }
  267. /// Return iterator to the first element.
  268. ConstIterator Begin() const { return ConstIterator(Head()); }
  269. /// Return iterator to the end.
  270. Iterator End() { return Iterator(Tail()); }
  271. /// Return iterator to the end.
  272. ConstIterator End() const { return ConstIterator(Tail()); }
  273. /// Return first element.
  274. T& Front() { return *Begin(); }
  275. /// Return const first element.
  276. const T& Front() const { return *Begin(); }
  277. /// Return last element.
  278. T& Back() { return *(--End()); }
  279. /// Return const last element.
  280. const T& Back() const { return *(--End()); }
  281. /// Return number of elements.
  282. unsigned Size() const { return size_; }
  283. /// Return whether list is empty.
  284. bool Empty() const { return size_ == 0; }
  285. private:
  286. /// Return the head node.
  287. Node* Head() const { return reinterpret_cast<Node*>(head_); }
  288. /// Return the tail node.
  289. Node* Tail() const { return reinterpret_cast<Node*>(tail_); }
  290. /// Allocate and insert a node into the list.
  291. void InsertNode(Node* dest, const T& value)
  292. {
  293. if (!dest)
  294. return;
  295. Node* newNode = ReserveNode(value);
  296. Node* prev = dest->Prev();
  297. newNode->next_ = dest;
  298. newNode->prev_ = prev;
  299. if (prev)
  300. prev->next_ = newNode;
  301. dest->prev_ = newNode;
  302. // Reassign the head node if necessary
  303. if (dest == Head())
  304. head_ = newNode;
  305. ++size_;
  306. }
  307. /// Erase and free a node. Return pointer to the next node, or to the end if could not erase.
  308. Node* EraseNode(Node* node)
  309. {
  310. // The tail node can not be removed
  311. if (!node || node == tail_)
  312. return Tail();
  313. Node* prev = node->Prev();
  314. Node* next = node->Next();
  315. if (prev)
  316. prev->next_ = next;
  317. next->prev_ = prev;
  318. // Reassign the head node if necessary
  319. if (node == Head())
  320. head_ = next;
  321. FreeNode(node);
  322. --size_;
  323. return next;
  324. }
  325. /// Reserve a node.
  326. Node* ReserveNode()
  327. {
  328. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  329. new(newNode) Node();
  330. return newNode;
  331. }
  332. /// Reserve a node with initial value.
  333. Node* ReserveNode(const T& value)
  334. {
  335. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  336. new(newNode) Node(value);
  337. return newNode;
  338. }
  339. /// Free a node.
  340. void FreeNode(Node* node)
  341. {
  342. (node)->~Node();
  343. AllocatorFree(allocator_, node);
  344. }
  345. };