List.h 13 KB

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