List.h 13 KB

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