List.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. Swap(rhs);
  190. return *this;
  191. }
  192. /// Add-assign an element.
  193. List& operator +=(const T& rhs)
  194. {
  195. Push(rhs);
  196. return *this;
  197. }
  198. /// Add-assign a list.
  199. List& operator +=(const List<T>& rhs)
  200. {
  201. Insert(End(), rhs);
  202. return *this;
  203. }
  204. /// Test for equality with another list.
  205. bool operator ==(const List<T>& rhs) const
  206. {
  207. if (rhs.size_ != size_)
  208. return false;
  209. ConstIterator i = Begin();
  210. ConstIterator j = rhs.Begin();
  211. while (i != End())
  212. {
  213. if (*i != *j)
  214. return false;
  215. ++i;
  216. ++j;
  217. }
  218. return true;
  219. }
  220. /// Test for inequality with another list.
  221. bool operator !=(const List<T>& rhs) const
  222. {
  223. if (rhs.size_ != size_)
  224. return true;
  225. ConstIterator i = Begin();
  226. ConstIterator j = rhs.Begin();
  227. while (i != End())
  228. {
  229. if (*i != *j)
  230. return true;
  231. ++i;
  232. ++j;
  233. }
  234. return false;
  235. }
  236. /// Insert an element to the end.
  237. void Push(const T& value) { InsertNode(Tail(), value); }
  238. /// Insert an element to the beginning.
  239. void PushFront(const T& value) { InsertNode(Head(), value); }
  240. /// Insert an element at position.
  241. void Insert(const Iterator& dest, const T& value) { InsertNode(static_cast<Node*>(dest.ptr_), value); }
  242. /// Insert a list at position.
  243. void Insert(const Iterator& dest, const List<T>& list)
  244. {
  245. auto* destNode = static_cast<Node*>(dest.ptr_);
  246. ConstIterator it = list.Begin();
  247. ConstIterator end = list.End();
  248. while (it != end)
  249. InsertNode(destNode, *it++);
  250. }
  251. /// Insert elements by iterators.
  252. void Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  253. {
  254. auto* destNode = static_cast<Node*>(dest.ptr_);
  255. ConstIterator it = start;
  256. while (it != end)
  257. InsertNode(destNode, *it++);
  258. }
  259. /// Insert elements.
  260. void Insert(const Iterator& dest, const T* start, const T* end)
  261. {
  262. auto* destNode = static_cast<Node*>(dest.ptr_);
  263. const T* ptr = start;
  264. while (ptr != end)
  265. InsertNode(destNode, *ptr++);
  266. }
  267. /// Erase the last element.
  268. void Pop()
  269. {
  270. if (size_)
  271. Erase(--End());
  272. }
  273. /// Erase the first element.
  274. void PopFront()
  275. {
  276. if (size_)
  277. Erase(Begin());
  278. }
  279. /// Erase an element by iterator. Return iterator to the next element.
  280. Iterator Erase(Iterator it)
  281. {
  282. return Iterator(EraseNode(static_cast<Node*>(it.ptr_)));
  283. }
  284. /// Erase a range by iterators. Return an iterator to the next element.
  285. Iterator Erase(const Iterator& start, const Iterator& end)
  286. {
  287. Iterator it = start;
  288. while (it != end)
  289. it = Erase(it);
  290. return it;
  291. }
  292. /// Clear the list.
  293. void Clear()
  294. {
  295. if (Size())
  296. {
  297. for (Iterator i = Begin(); i != End();)
  298. {
  299. FreeNode(static_cast<Node*>(i++.ptr_));
  300. i.ptr_->prev_ = 0;
  301. }
  302. head_ = tail_;
  303. size_ = 0;
  304. }
  305. }
  306. /// Resize the list by removing or adding items at the end.
  307. void Resize(unsigned newSize)
  308. {
  309. while (size_ > newSize)
  310. Pop();
  311. while (size_ < newSize)
  312. InsertNode(Tail(), T());
  313. }
  314. /// Return iterator to value, or to the end if not found.
  315. Iterator Find(const T& value)
  316. {
  317. Iterator it = Begin();
  318. while (it != End() && *it != value)
  319. ++it;
  320. return it;
  321. }
  322. /// Return const iterator to value, or to the end if not found.
  323. ConstIterator Find(const T& value) const
  324. {
  325. ConstIterator it = Begin();
  326. while (it != End() && *it != value)
  327. ++it;
  328. return it;
  329. }
  330. /// Return whether contains a specific value.
  331. bool Contains(const T& value) const { return Find(value) != End(); }
  332. /// Return iterator to the first element.
  333. Iterator Begin() { return Iterator(Head()); }
  334. /// Return iterator to the first element.
  335. ConstIterator Begin() const { return ConstIterator(Head()); }
  336. /// Return iterator to the end.
  337. Iterator End() { return Iterator(Tail()); }
  338. /// Return iterator to the end.
  339. ConstIterator End() const { return ConstIterator(Tail()); }
  340. /// Return first element.
  341. T& Front() { return *Begin(); }
  342. /// Return const first element.
  343. const T& Front() const { return *Begin(); }
  344. /// Return last element.
  345. T& Back() { return *(--End()); }
  346. /// Return const last element.
  347. const T& Back() const { return *(--End()); }
  348. /// Return number of elements.
  349. unsigned Size() const { return size_; }
  350. /// Return whether list is empty.
  351. bool Empty() const { return size_ == 0; }
  352. private:
  353. /// Return the head node.
  354. Node* Head() const { return static_cast<Node*>(head_); }
  355. /// Return the tail node.
  356. Node* Tail() const { return static_cast<Node*>(tail_); }
  357. /// Allocate and insert a node into the list.
  358. void InsertNode(Node* dest, const T& value)
  359. {
  360. if (!dest)
  361. return;
  362. Node* newNode = ReserveNode(value);
  363. Node* prev = dest->Prev();
  364. newNode->next_ = dest;
  365. newNode->prev_ = prev;
  366. if (prev)
  367. prev->next_ = newNode;
  368. dest->prev_ = newNode;
  369. // Reassign the head node if necessary
  370. if (dest == Head())
  371. head_ = newNode;
  372. ++size_;
  373. }
  374. /// Erase and free a node. Return pointer to the next node, or to the end if could not erase.
  375. Node* EraseNode(Node* node)
  376. {
  377. // The tail node can not be removed
  378. if (!node || node == tail_)
  379. return Tail();
  380. Node* prev = node->Prev();
  381. Node* next = node->Next();
  382. if (prev)
  383. prev->next_ = next;
  384. next->prev_ = prev;
  385. // Reassign the head node if necessary
  386. if (node == Head())
  387. head_ = next;
  388. FreeNode(node);
  389. --size_;
  390. return next;
  391. }
  392. /// Reserve a node.
  393. Node* ReserveNode()
  394. {
  395. auto* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  396. new(newNode) Node();
  397. return newNode;
  398. }
  399. /// Reserve a node with initial value.
  400. Node* ReserveNode(const T& value)
  401. {
  402. auto* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
  403. new(newNode) Node(value);
  404. return newNode;
  405. }
  406. /// Free a node.
  407. void FreeNode(Node* node)
  408. {
  409. (node)->~Node();
  410. AllocatorFree(allocator_, node);
  411. }
  412. };
  413. template <class T> typename Urho3D::List<T>::ConstIterator begin(const Urho3D::List<T>& v) { return v.Begin(); }
  414. template <class T> typename Urho3D::List<T>::ConstIterator end(const Urho3D::List<T>& v) { return v.End(); }
  415. template <class T> typename Urho3D::List<T>::Iterator begin(Urho3D::List<T>& v) { return v.Begin(); }
  416. template <class T> typename Urho3D::List<T>::Iterator end(Urho3D::List<T>& v) { return v.End(); }
  417. }