List.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Allocator.h>
  7. #include <AnKi/Util/Forward.h>
  8. #include <functional>
  9. namespace anki
  10. {
  11. /// @addtogroup util_containers
  12. /// @{
  13. namespace detail
  14. {
  15. /// List node.
  16. /// @internal
  17. template<typename T>
  18. class ListNode
  19. {
  20. public:
  21. T m_value;
  22. ListNode* m_prev = nullptr;
  23. ListNode* m_next = nullptr;
  24. template<typename... TArgs>
  25. ListNode(TArgs&&... args)
  26. : m_value(std::forward<TArgs>(args)...)
  27. {
  28. }
  29. T& getListNodeValue()
  30. {
  31. return m_value;
  32. }
  33. const T& getListNodeValue() const
  34. {
  35. return m_value;
  36. }
  37. };
  38. /// List bidirectional iterator.
  39. /// @internal
  40. template<typename TNodePointer, typename TValuePointer, typename TValueReference, typename TListPointer>
  41. class ListIterator
  42. {
  43. template<typename, typename>
  44. friend class ListBase;
  45. template<typename>
  46. friend class anki::List;
  47. template<typename, typename, typename, typename>
  48. friend class ListIterator;
  49. public:
  50. ListIterator() = default;
  51. ListIterator(const ListIterator& b)
  52. : m_node(b.m_node)
  53. , m_list(b.m_list)
  54. {
  55. }
  56. /// Allow conversion from iterator to const iterator.
  57. template<typename YNodePointer, typename YValuePointer, typename YValueReference, typename YList>
  58. ListIterator(const ListIterator<YNodePointer, YValuePointer, YValueReference, YList>& b)
  59. : m_node(b.m_node)
  60. , m_list(b.m_list)
  61. {
  62. }
  63. ListIterator(TNodePointer node, TListPointer list)
  64. : m_node(node)
  65. , m_list(list)
  66. {
  67. ANKI_ASSERT(list);
  68. }
  69. ListIterator& operator=(const ListIterator& b)
  70. {
  71. m_node = b.m_node;
  72. m_list = b.m_list;
  73. return *this;
  74. }
  75. TValueReference operator*() const
  76. {
  77. ANKI_ASSERT(m_node);
  78. return m_node->getListNodeValue();
  79. }
  80. TValuePointer operator->() const
  81. {
  82. ANKI_ASSERT(m_node);
  83. return &m_node->getListNodeValue();
  84. }
  85. ListIterator& operator++()
  86. {
  87. ANKI_ASSERT(m_node);
  88. m_node = m_node->m_next;
  89. return *this;
  90. }
  91. ListIterator operator++(int)
  92. {
  93. ANKI_ASSERT(m_node);
  94. ListIterator out = *this;
  95. ++(*this);
  96. return out;
  97. }
  98. ListIterator& operator--();
  99. ListIterator operator--(int)
  100. {
  101. ANKI_ASSERT(m_node);
  102. ListIterator out = *this;
  103. --(*this);
  104. return out;
  105. }
  106. ListIterator operator+(U n) const
  107. {
  108. ListIterator it = *this;
  109. while(n-- != 0)
  110. {
  111. ++it;
  112. }
  113. return it;
  114. }
  115. ListIterator operator-(U n) const
  116. {
  117. ListIterator it = *this;
  118. while(n-- != 0)
  119. {
  120. --it;
  121. }
  122. return it;
  123. }
  124. ListIterator& operator+=(U n)
  125. {
  126. while(n-- != 0)
  127. {
  128. ++(*this);
  129. }
  130. return *this;
  131. }
  132. ListIterator& operator-=(U n)
  133. {
  134. while(n-- != 0)
  135. {
  136. --(*this);
  137. }
  138. return *this;
  139. }
  140. template<typename YNodePointer, typename YValuePointer, typename YValueReference, typename YList>
  141. Bool operator==(const ListIterator<YNodePointer, YValuePointer, YValueReference, YList>& b) const
  142. {
  143. ANKI_ASSERT(m_list == b.m_list && "Comparing iterators from different lists");
  144. return m_node == b.m_node;
  145. }
  146. template<typename YNodePointer, typename YValuePointer, typename YValueReference, typename YList>
  147. Bool operator!=(const ListIterator<YNodePointer, YValuePointer, YValueReference, YList>& b) const
  148. {
  149. return !(*this == b);
  150. }
  151. private:
  152. TNodePointer m_node = nullptr;
  153. TListPointer m_list = nullptr; ///< Used to go back from the end
  154. };
  155. /// Double linked list base.
  156. /// @internal
  157. template<typename T, typename TNode>
  158. class ListBase
  159. {
  160. template<typename, typename, typename, typename>
  161. friend class ListIterator;
  162. public:
  163. using Value = T;
  164. using Reference = Value&;
  165. using ConstReference = const Value&;
  166. using Pointer = Value*;
  167. using ConstPointer = const Value*;
  168. using Iterator = ListIterator<TNode*, Pointer, Reference, ListBase*>;
  169. using ConstIterator = ListIterator<const TNode*, ConstPointer, ConstReference, const ListBase*>;
  170. ListBase() = default;
  171. ListBase(const ListBase&) = delete; // Non-copyable
  172. ListBase& operator=(const ListBase&) = delete; // Non-copyable
  173. /// Compare with another list.
  174. Bool operator==(const ListBase& b) const;
  175. /// Get first element.
  176. ConstReference getFront() const
  177. {
  178. ANKI_ASSERT(!isEmpty());
  179. return m_head->getListNodeValue();
  180. }
  181. /// Get first element.
  182. Reference getFront()
  183. {
  184. ANKI_ASSERT(!isEmpty());
  185. return m_head->getListNodeValue();
  186. }
  187. /// Get last element.
  188. ConstReference getBack() const
  189. {
  190. ANKI_ASSERT(!isEmpty());
  191. return m_tail->getListNodeValue();
  192. }
  193. /// Get last element.
  194. Reference getBack()
  195. {
  196. ANKI_ASSERT(!isEmpty());
  197. return m_tail->getListNodeValue();
  198. }
  199. /// Get begin.
  200. Iterator getBegin()
  201. {
  202. return Iterator(m_head, this);
  203. }
  204. /// Get begin.
  205. ConstIterator getBegin() const
  206. {
  207. return ConstIterator(m_head, this);
  208. }
  209. /// Get end.
  210. Iterator getEnd()
  211. {
  212. return Iterator(nullptr, this);
  213. }
  214. /// Get end.
  215. ConstIterator getEnd() const
  216. {
  217. return ConstIterator(nullptr, this);
  218. }
  219. /// Get begin.
  220. Iterator begin()
  221. {
  222. return getBegin();
  223. }
  224. /// Get begin.
  225. ConstIterator begin() const
  226. {
  227. return getBegin();
  228. }
  229. /// Get end.
  230. Iterator end()
  231. {
  232. return getEnd();
  233. }
  234. /// Get end.
  235. ConstIterator end() const
  236. {
  237. return getEnd();
  238. }
  239. /// Return true if list is empty.
  240. Bool isEmpty() const
  241. {
  242. return m_head == nullptr;
  243. }
  244. /// Iterate the list using lambda.
  245. template<typename TFunc>
  246. ANKI_USE_RESULT Error iterateForward(TFunc func);
  247. /// Iterate the list backwards using lambda.
  248. template<typename TFunc>
  249. ANKI_USE_RESULT Error iterateBackward(TFunc func);
  250. /// Find item.
  251. Iterator find(const Value& a);
  252. /// Sort the list.
  253. /// @note It's almost 300 slower than std::list::sort, at some point replace the algorithm.
  254. template<typename TCompFunc = std::less<Value>>
  255. void sort(TCompFunc compFunc = TCompFunc());
  256. /// Compute the size of elements in the list.
  257. PtrSize getSize() const;
  258. protected:
  259. TNode* m_head = nullptr;
  260. TNode* m_tail = nullptr;
  261. void move(ListBase& b)
  262. {
  263. m_head = b.m_head;
  264. b.m_head = nullptr;
  265. m_tail = b.m_tail;
  266. b.m_tail = nullptr;
  267. }
  268. void pushBackNode(TNode* node);
  269. void pushFrontNode(TNode* node);
  270. void insertNode(TNode* pos, TNode* node);
  271. void removeNode(TNode* node);
  272. void popBack();
  273. void popFront();
  274. private:
  275. /// Used in sort.
  276. TNode* swap(TNode* one, TNode* two);
  277. };
  278. } // end namespace detail
  279. /// Double linked list.
  280. template<typename T>
  281. class List : public detail::ListBase<T, detail::ListNode<T>>
  282. {
  283. private:
  284. using Base = detail::ListBase<T, detail::ListNode<T>>;
  285. using Node = detail::ListNode<T>;
  286. public:
  287. using typename Base::Iterator;
  288. /// Default constructor.
  289. List()
  290. : Base()
  291. {
  292. }
  293. /// Move.
  294. List(List&& b)
  295. : List()
  296. {
  297. move(b);
  298. }
  299. /// You need to manually destroy the list. @see List::destroy
  300. ~List()
  301. {
  302. ANKI_ASSERT(Base::isEmpty() && "Requires manual destruction");
  303. }
  304. /// Move.
  305. List& operator=(List&& b)
  306. {
  307. move(b);
  308. return *this;
  309. }
  310. /// Destroy the list.
  311. template<typename TAllocator>
  312. void destroy(TAllocator alloc);
  313. /// Copy an element at the end of the list.
  314. template<typename TAllocator>
  315. Iterator pushBack(TAllocator alloc, const T& x)
  316. {
  317. Node* node = alloc.template newInstance<Node>(x);
  318. Base::pushBackNode(node);
  319. return Iterator(node, this);
  320. }
  321. /// Construct an element at the end of the list.
  322. template<typename TAllocator, typename... TArgs>
  323. Iterator emplaceBack(TAllocator alloc, TArgs&&... args)
  324. {
  325. Node* node = alloc.template newInstance<Node>(std::forward<TArgs>(args)...);
  326. Base::pushBackNode(node);
  327. return Iterator(node, this);
  328. }
  329. /// Copy an element at the beginning of the list.
  330. template<typename TAllocator>
  331. Iterator pushFront(TAllocator alloc, const T& x)
  332. {
  333. Node* node = alloc.template newInstance<Node>(x);
  334. Base::pushFrontNode(node);
  335. return Iterator(node, this);
  336. }
  337. /// Construct element at the beginning of the list.
  338. template<typename TAllocator, typename... TArgs>
  339. Iterator emplaceFront(TAllocator alloc, TArgs&&... args)
  340. {
  341. Node* node = alloc.template newInstance<Node>(std::forward<TArgs>(args)...);
  342. Base::pushFrontNode(node);
  343. return Iterator(node, this);
  344. }
  345. /// Copy an element at the given position of the list.
  346. template<typename TAllocator>
  347. Iterator insert(TAllocator alloc, Iterator pos, const T& x)
  348. {
  349. Node* node = alloc.template newInstance<Node>(x);
  350. Base::insertNode(pos.m_node, node);
  351. return Iterator(node, this);
  352. }
  353. /// Construct element at the the given position.
  354. template<typename TAllocator, typename... TArgs>
  355. Iterator emplace(TAllocator alloc, Iterator pos, TArgs&&... args)
  356. {
  357. Node* node = alloc.template newInstance<Node>(std::forward<TArgs>(args)...);
  358. Base::insertNode(pos.m_node, node);
  359. return Iterator(node, this);
  360. }
  361. /// Pop a value from the back of the list.
  362. template<typename TAllocator>
  363. void popBack(TAllocator alloc)
  364. {
  365. ANKI_ASSERT(Base::m_tail);
  366. Node* node = Base::m_tail;
  367. Base::popBack();
  368. alloc.deleteInstance(node);
  369. }
  370. /// Pop a value from the front of the list.
  371. template<typename TAllocator>
  372. void popFront(TAllocator alloc)
  373. {
  374. ANKI_ASSERT(Base::m_head);
  375. Node* node = Base::m_head;
  376. Base::popFront();
  377. alloc.deleteInstance(node);
  378. }
  379. /// Erase an element.
  380. template<typename TAllocator>
  381. void erase(TAllocator alloc, Iterator pos)
  382. {
  383. ANKI_ASSERT(pos.m_node);
  384. ANKI_ASSERT(pos.m_list == this);
  385. Base::removeNode(pos.m_node);
  386. alloc.deleteInstance(pos.m_node);
  387. }
  388. private:
  389. void move(List& b)
  390. {
  391. ANKI_ASSERT(Base::isEmpty() && "Requires manual destruction");
  392. Base::move(b);
  393. }
  394. };
  395. /// List with automatic destruction.
  396. template<typename T>
  397. class ListAuto : public List<T>
  398. {
  399. public:
  400. using Base = List<T>;
  401. using Value = T;
  402. using typename Base::Iterator;
  403. /// Construct using an allocator.
  404. ListAuto(GenericMemoryPoolAllocator<T> alloc)
  405. : Base()
  406. , m_alloc(alloc)
  407. {
  408. }
  409. /// Move.
  410. ListAuto(ListAuto&& b)
  411. : Base()
  412. {
  413. move(b);
  414. }
  415. /// Destroy.
  416. ~ListAuto()
  417. {
  418. Base::destroy(m_alloc);
  419. }
  420. /// Move.
  421. ListAuto& operator=(ListAuto&& b)
  422. {
  423. move(b);
  424. return *this;
  425. }
  426. /// Copy an element at the end of the list.
  427. Iterator pushBack(const Value& x)
  428. {
  429. return Base::pushBack(m_alloc, x);
  430. }
  431. /// Construct an element at the end of the list.
  432. template<typename... TArgs>
  433. Iterator emplaceBack(TArgs&&... args)
  434. {
  435. return Base::emplaceBack(m_alloc, std::forward<TArgs>(args)...);
  436. }
  437. /// Construct element at the beginning of the list.
  438. template<typename... TArgs>
  439. Iterator emplaceFront(TArgs&&... args)
  440. {
  441. return Base::emplaceFront(m_alloc, std::forward<TArgs>(args)...);
  442. }
  443. /// Construct element at the the given position.
  444. template<typename... TArgs>
  445. Iterator emplace(Iterator pos, TArgs&&... args)
  446. {
  447. return Base::emplace(m_alloc, pos, std::forward(args)...);
  448. }
  449. /// Pop a value from the back of the list.
  450. void popBack()
  451. {
  452. Base::popBack(m_alloc);
  453. }
  454. /// Pop a value from the front of the list.
  455. void popFront()
  456. {
  457. Base::popFront(m_alloc);
  458. }
  459. /// Erase an element.
  460. void erase(Iterator position)
  461. {
  462. Base::erase(m_alloc, position);
  463. }
  464. /// Destroy the list.
  465. void destroy()
  466. {
  467. Base::destroy(m_alloc);
  468. }
  469. private:
  470. GenericMemoryPoolAllocator<T> m_alloc;
  471. void move(ListAuto& b)
  472. {
  473. Base::move(b);
  474. m_alloc = b.m_alloc;
  475. }
  476. };
  477. /// The classes that will use the IntrusiveList need to inherit from this one.
  478. template<typename TClass>
  479. class IntrusiveListEnabled
  480. {
  481. template<typename, typename, typename, typename>
  482. friend class detail::ListIterator;
  483. template<typename, typename>
  484. friend class detail::ListBase;
  485. template<typename>
  486. friend class List;
  487. template<typename>
  488. friend class IntrusiveList;
  489. friend TClass;
  490. private:
  491. TClass* m_prev;
  492. TClass* m_next;
  493. IntrusiveListEnabled()
  494. : m_prev(nullptr)
  495. , m_next(nullptr)
  496. {
  497. }
  498. TClass& getListNodeValue()
  499. {
  500. return *static_cast<TClass*>(this);
  501. }
  502. const TClass& getListNodeValue() const
  503. {
  504. return *static_cast<const TClass*>(this);
  505. }
  506. };
  507. /// List that doesn't perform any allocations. To work the T nodes will have to inherit from IntrusiveListEnabled.
  508. template<typename T>
  509. class IntrusiveList : public detail::ListBase<T, T>
  510. {
  511. template<typename, typename, typename, typename>
  512. friend class detail::ListIterator;
  513. private:
  514. using Base = detail::ListBase<T, T>;
  515. public:
  516. using typename Base::Iterator;
  517. /// Default constructor.
  518. IntrusiveList()
  519. : Base()
  520. {
  521. }
  522. /// Move.
  523. IntrusiveList(IntrusiveList&& b)
  524. : IntrusiveList()
  525. {
  526. Base::move(b);
  527. }
  528. ~IntrusiveList() = default;
  529. /// Move.
  530. IntrusiveList& operator=(IntrusiveList&& b)
  531. {
  532. Base::move(b);
  533. return *this;
  534. }
  535. /// Copy an element at the end of the list.
  536. Iterator pushBack(T* x)
  537. {
  538. Base::pushBackNode(x);
  539. return Iterator(x, this);
  540. }
  541. /// Copy an element at the beginning of the list.
  542. Iterator pushFront(T* x)
  543. {
  544. Base::pushFrontNode(x);
  545. return Iterator(x, this);
  546. }
  547. /// Copy an element at the given position of the list.
  548. Iterator insert(Iterator pos, T* x)
  549. {
  550. Base::insertNode(pos.m_node, x);
  551. return Iterator(x, this);
  552. }
  553. /// Pop a value from the back of the list.
  554. T* popBack()
  555. {
  556. T* tail = Base::m_tail;
  557. Base::popBack();
  558. return tail;
  559. }
  560. /// Pop a value from the front of the list.
  561. T* popFront()
  562. {
  563. T* head = Base::m_head;
  564. Base::popFront();
  565. return head;
  566. }
  567. /// Erase an element.
  568. void erase(Iterator pos)
  569. {
  570. Base::removeNode(pos.m_node);
  571. }
  572. /// Erase an element.
  573. void erase(T* x)
  574. {
  575. Base::removeNode(x);
  576. }
  577. };
  578. /// @}
  579. } // end namespace anki
  580. #include <AnKi/Util/List.inl.h>