List.h 13 KB

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