List.h 13 KB

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