List.h 13 KB

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