2
0

List.h 13 KB

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