List.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. /// 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. class Iterator : public ListIteratorBase
  50. {
  51. public:
  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. class ConstIterator : public ListIteratorBase
  76. {
  77. public:
  78. /// Construct.
  79. ConstIterator()
  80. {
  81. }
  82. /// Construct with a node pointer.
  83. explicit ConstIterator(Node* ptr) :
  84. ListIteratorBase(ptr)
  85. {
  86. }
  87. /// Construct from a non-const iterator.
  88. ConstIterator(const Iterator& rhs) :
  89. ListIteratorBase(rhs.ptr_)
  90. {
  91. }
  92. /// Assign from a non-const iterator.
  93. ConstIterator& operator = (const Iterator& rhs) { ptr_ = rhs.ptr_; return *this; }
  94. /// Preincrement the pointer.
  95. ConstIterator& operator ++ () { GotoNext(); return *this; }
  96. /// Postincrement the pointer.
  97. ConstIterator operator ++ (int) { ConstIterator it = *this; GotoNext(); return it; }
  98. /// Predecrement the pointer.
  99. ConstIterator& operator -- () { GotoPrev(); return *this; }
  100. /// Postdecrement the pointer.
  101. ConstIterator operator -- (int) { ConstIterator it = *this; GotoPrev(); return it; }
  102. /// Point to the node value.
  103. const T* operator -> () const { return &(static_cast<Node*>(ptr_))->value_; }
  104. /// Dereference the node value.
  105. const T& operator * () const { return (static_cast<Node*>(ptr_))->value_; }
  106. };
  107. /// Construct empty.
  108. List()
  109. {
  110. allocator_ = AllocatorInitialize(sizeof(Node));
  111. head_ = tail_ = ReserveNode();
  112. }
  113. /// Construct from another list.
  114. List(const List<T>& list)
  115. {
  116. allocator_ = AllocatorInitialize(sizeof(Node));
  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. Return an 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. while (size_)
  239. EraseNode(Head());
  240. }
  241. /// Resize the list by removing or adding items at the end.
  242. void Resize(unsigned newSize)
  243. {
  244. while (size_ > newSize)
  245. Pop();
  246. while (size_ < newSize)
  247. InsertNode(Tail(), T());
  248. }
  249. /// Return iterator to value, or to the end if not found.
  250. Iterator Find(const T& value)
  251. {
  252. Iterator i = Begin();
  253. while (i != End() && *i != value)
  254. ++i;
  255. return i;
  256. }
  257. /// Return const iterator to value, or to the end if not found.
  258. ConstIterator Find(const T& value) const
  259. {
  260. ConstIterator i = Begin();
  261. while (i != End() && *i != value)
  262. ++i;
  263. return i;
  264. }
  265. /// Return whether contains a specific value.
  266. bool Contains(const T& value) const { return Find(value) != End(); }
  267. /// Return iterator to the first element.
  268. Iterator Begin() { return Iterator(Head()); }
  269. /// Return iterator to the first element.
  270. ConstIterator Begin() const { return ConstIterator(Head()); }
  271. /// Return iterator to the end.
  272. Iterator End() { return Iterator(Tail()); }
  273. /// Return iterator to the end.
  274. ConstIterator End() const { return ConstIterator(Tail()); }
  275. /// Return first element.
  276. T& Front() { return *Begin(); }
  277. /// Return const first element.
  278. const T& Front() const { return *Begin(); }
  279. /// Return last element.
  280. T& Back() { return *(--End()); }
  281. /// Return const last element.
  282. const T& Back() const { return *(--End()); }
  283. /// Return number of elements.
  284. unsigned Size() const { return size_; }
  285. /// Return whether list is empty.
  286. bool Empty() const { return size_ == 0; }
  287. private:
  288. /// Return the head pointer with correct type.
  289. Node* Head() const { return reinterpret_cast<Node*>(head_); }
  290. /// Return the tail pointer with correct type.
  291. Node* Tail() const { return reinterpret_cast<Node*>(tail_); }
  292. /// Allocate and insert a node into the list.
  293. void InsertNode(Node* dest, const T& value)
  294. {
  295. if (!dest)
  296. return;
  297. Node* newNode = ReserveNode(value);
  298. Node* prev = dest->Prev();
  299. newNode->next_ = dest;
  300. newNode->prev_ = prev;
  301. if (prev)
  302. prev->next_ = newNode;
  303. dest->prev_ = newNode;
  304. // Reassign the head node if necessary
  305. if (dest == Head())
  306. head_ = newNode;
  307. ++size_;
  308. }
  309. /// Erase and free a node. Return pointer to the next node, or to the end if could not erase.
  310. Node* EraseNode(Node* toRemove)
  311. {
  312. // The tail node can not be removed
  313. if (!toRemove || toRemove == tail_)
  314. return Tail();
  315. Node* prev = toRemove->Prev();
  316. Node* next = toRemove->Next();
  317. if (prev)
  318. prev->next_ = next;
  319. next->prev_ = prev;
  320. // Reassign the head node if necessary
  321. if (toRemove == Head())
  322. head_ = next;
  323. FreeNode(toRemove);
  324. --size_;
  325. return next;
  326. }
  327. /// Reserve a node.
  328. Node* ReserveNode()
  329. {
  330. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  331. new(newNode) Node();
  332. return newNode;
  333. }
  334. /// Reserve a node with initial value.
  335. Node* ReserveNode(const T& value)
  336. {
  337. Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  338. new(newNode) Node(value);
  339. return newNode;
  340. }
  341. /// Free a node.
  342. void FreeNode(Node* node)
  343. {
  344. (node)->~Node();
  345. AllocatorFree(allocator_, node);
  346. }
  347. };