List.h 12 KB

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