List.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // Copyright (c) 2008-2013 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. allocator_ = AllocatorInitialize(sizeof(Node));
  116. head_ = tail_ = ReserveNode();
  117. *this = list;
  118. }
  119. /// Destruct.
  120. ~List()
  121. {
  122. Clear();
  123. FreeNode(Tail());
  124. AllocatorUninitialize(allocator_);
  125. }
  126. /// Assign from another list.
  127. List& operator = (const List<T>& rhs)
  128. {
  129. // Clear, then insert the nodes of the other list
  130. Clear();
  131. Insert(End(), rhs);
  132. return *this;
  133. }
  134. /// Add-assign an element.
  135. List& operator += (const T& rhs)
  136. {
  137. Push(rhs);
  138. return *this;
  139. }
  140. /// Add-assign a list.
  141. List& operator += (const List<T>& rhs)
  142. {
  143. Insert(End(), rhs);
  144. return *this;
  145. }
  146. /// Test for equality with another list.
  147. bool operator == (const List<T>& rhs) const
  148. {
  149. if (rhs.size_ != size_)
  150. return false;
  151. ConstIterator i = Begin();
  152. ConstIterator j = rhs.Begin();
  153. while (i != End())
  154. {
  155. if (*i != *j)
  156. return false;
  157. ++i;
  158. ++j;
  159. }
  160. return true;
  161. }
  162. /// Test for inequality with another list.
  163. bool operator != (const List<T>& rhs) const
  164. {
  165. if (rhs.size_ != size_)
  166. return true;
  167. ConstIterator i = Begin();
  168. ConstIterator j = rhs.Begin();
  169. while (i != End())
  170. {
  171. if (*i != *j)
  172. return true;
  173. ++i;
  174. ++j;
  175. }
  176. return false;
  177. }
  178. /// Insert an element to the end.
  179. void Push(const T& value) { InsertNode(Tail(), value); }
  180. /// Insert an element to the beginning.
  181. void PushFront(const T& value) { InsertNode(Head(), value); }
  182. /// Insert an element at position.
  183. void Insert(const Iterator& dest, const T& value) { InsertNode(static_cast<Node*>(dest.ptr_), value); }
  184. /// Insert a list at position.
  185. void Insert(const Iterator& dest, const List<T>& list)
  186. {
  187. Node* destNode = static_cast<Node*>(dest.ptr_);
  188. ConstIterator it = list.Begin();
  189. ConstIterator end = list.End();
  190. while (it != end)
  191. InsertNode(destNode, *it++);
  192. }
  193. /// Insert elements by iterators.
  194. void Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  195. {
  196. Node* destNode = static_cast<Node*>(dest.ptr_);
  197. ConstIterator it = start;
  198. while (it != end)
  199. InsertNode(destNode, *it++);
  200. }
  201. /// Insert elements.
  202. void Insert(const Iterator& dest, const T* start, const T* end)
  203. {
  204. Node* destNode = static_cast<Node*>(dest.ptr_);
  205. const T* ptr = start;
  206. while (ptr != end)
  207. InsertNode(destNode, *ptr++);
  208. }
  209. /// Erase the last element.
  210. void Pop()
  211. {
  212. if (size_)
  213. Erase(--End());
  214. }
  215. /// Erase the first element.
  216. void PopFront()
  217. {
  218. if (size_)
  219. Erase(Begin());
  220. }
  221. /// Erase an element by iterator. Return iterator to the next element.
  222. Iterator Erase(Iterator it)
  223. {
  224. return Iterator(EraseNode(static_cast<Node*>(it.ptr_)));
  225. }
  226. /// Erase a range by iterators. Return an iterator to the next element.
  227. Iterator Erase(const Iterator& start, const Iterator& end)
  228. {
  229. Iterator it = start;
  230. while (it != end)
  231. it = EraseNode(static_cast<Node*>(it.ptr_));
  232. return it;
  233. }
  234. /// Clear the list.
  235. void Clear()
  236. {
  237. if (Size())
  238. {
  239. for (Iterator i = Begin(); i != End(); )
  240. {
  241. FreeNode(static_cast<Node*>(i++.ptr_));
  242. i.ptr_->prev_ = 0;
  243. }
  244. head_ = tail_;
  245. size_ = 0;
  246. }
  247. }
  248. /// Resize the list by removing or adding items at the end.
  249. void Resize(unsigned newSize)
  250. {
  251. while (size_ > newSize)
  252. Pop();
  253. while (size_ < newSize)
  254. InsertNode(Tail(), T());
  255. }
  256. /// Return iterator to value, or to the end if not found.
  257. Iterator Find(const T& value)
  258. {
  259. Iterator it = Begin();
  260. while (it != End() && *it != value)
  261. ++it;
  262. return it;
  263. }
  264. /// Return const iterator to value, or to the end if not found.
  265. ConstIterator Find(const T& value) const
  266. {
  267. ConstIterator it = Begin();
  268. while (it != End() && *it != value)
  269. ++it;
  270. return it;
  271. }
  272. /// Return whether contains a specific value.
  273. bool Contains(const T& value) const { return Find(value) != End(); }
  274. /// Return iterator to the first element.
  275. Iterator Begin() { return Iterator(Head()); }
  276. /// Return iterator to the first element.
  277. ConstIterator Begin() const { return ConstIterator(Head()); }
  278. /// Return iterator to the end.
  279. Iterator End() { return Iterator(Tail()); }
  280. /// Return iterator to the end.
  281. ConstIterator End() const { return ConstIterator(Tail()); }
  282. /// Return first element.
  283. T& Front() { return *Begin(); }
  284. /// Return const first element.
  285. const T& Front() const { return *Begin(); }
  286. /// Return last element.
  287. T& Back() { return *(--End()); }
  288. /// Return const last element.
  289. const T& Back() const { return *(--End()); }
  290. /// Return number of elements.
  291. unsigned Size() const { return size_; }
  292. /// Return whether list is empty.
  293. bool Empty() const { return size_ == 0; }
  294. private:
  295. /// Return the head node.
  296. Node* Head() const { return static_cast<Node*>(head_); }
  297. /// Return the tail node.
  298. Node* Tail() const { return static_cast<Node*>(tail_); }
  299. /// Allocate and insert a node into the list.
  300. void InsertNode(Node* dest, const T& value)
  301. {
  302. if (!dest)
  303. return;
  304. Node* newNode = ReserveNode(value);
  305. Node* prev = dest->Prev();
  306. newNode->next_ = dest;
  307. newNode->prev_ = prev;
  308. if (prev)
  309. prev->next_ = newNode;
  310. dest->prev_ = newNode;
  311. // Reassign the head node if necessary
  312. if (dest == Head())
  313. head_ = newNode;
  314. ++size_;
  315. }
  316. /// Erase and free a node. Return pointer to the next node, or to the end if could not erase.
  317. Node* EraseNode(Node* node)
  318. {
  319. // The tail node can not be removed
  320. if (!node || node == tail_)
  321. return Tail();
  322. Node* prev = node->Prev();
  323. Node* next = node->Next();
  324. if (prev)
  325. prev->next_ = next;
  326. next->prev_ = prev;
  327. // Reassign the head node if necessary
  328. if (node == Head())
  329. head_ = next;
  330. FreeNode(node);
  331. --size_;
  332. return next;
  333. }
  334. /// Reserve a node.
  335. Node* ReserveNode()
  336. {
  337. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  338. new(newNode) Node();
  339. return newNode;
  340. }
  341. /// Reserve a node with initial value.
  342. Node* ReserveNode(const T& value)
  343. {
  344. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  345. new(newNode) Node(value);
  346. return newNode;
  347. }
  348. /// Free a node.
  349. void FreeNode(Node* node)
  350. {
  351. (node)->~Node();
  352. AllocatorFree(allocator_, node);
  353. }
  354. };
  355. }