intrusive_list.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "common/common_funcs.h"
  5. #include "common/parent_of_member.h"
  6. namespace Common {
  7. // Forward declare implementation class for Node.
  8. namespace impl {
  9. class IntrusiveListImpl;
  10. }
  11. class IntrusiveListNode {
  12. YUZU_NON_COPYABLE(IntrusiveListNode);
  13. private:
  14. friend class impl::IntrusiveListImpl;
  15. IntrusiveListNode* m_prev;
  16. IntrusiveListNode* m_next;
  17. public:
  18. constexpr IntrusiveListNode() : m_prev(this), m_next(this) {}
  19. constexpr bool IsLinked() const {
  20. return m_next != this;
  21. }
  22. private:
  23. constexpr void LinkPrev(IntrusiveListNode* node) {
  24. // We can't link an already linked node.
  25. ASSERT(!node->IsLinked());
  26. this->SplicePrev(node, node);
  27. }
  28. constexpr void SplicePrev(IntrusiveListNode* first, IntrusiveListNode* last) {
  29. // Splice a range into the list.
  30. auto last_prev = last->m_prev;
  31. first->m_prev = m_prev;
  32. last_prev->m_next = this;
  33. m_prev->m_next = first;
  34. m_prev = last_prev;
  35. }
  36. constexpr void LinkNext(IntrusiveListNode* node) {
  37. // We can't link an already linked node.
  38. ASSERT(!node->IsLinked());
  39. return this->SpliceNext(node, node);
  40. }
  41. constexpr void SpliceNext(IntrusiveListNode* first, IntrusiveListNode* last) {
  42. // Splice a range into the list.
  43. auto last_prev = last->m_prev;
  44. first->m_prev = this;
  45. last_prev->m_next = m_next;
  46. m_next->m_prev = last_prev;
  47. m_next = first;
  48. }
  49. constexpr void Unlink() {
  50. this->Unlink(m_next);
  51. }
  52. constexpr void Unlink(IntrusiveListNode* last) {
  53. // Unlink a node from a next node.
  54. auto last_prev = last->m_prev;
  55. m_prev->m_next = last;
  56. last->m_prev = m_prev;
  57. last_prev->m_next = this;
  58. m_prev = last_prev;
  59. }
  60. constexpr IntrusiveListNode* GetPrev() {
  61. return m_prev;
  62. }
  63. constexpr const IntrusiveListNode* GetPrev() const {
  64. return m_prev;
  65. }
  66. constexpr IntrusiveListNode* GetNext() {
  67. return m_next;
  68. }
  69. constexpr const IntrusiveListNode* GetNext() const {
  70. return m_next;
  71. }
  72. };
  73. // DEPRECATED: static_assert(std::is_literal_type<IntrusiveListNode>::value);
  74. namespace impl {
  75. class IntrusiveListImpl {
  76. YUZU_NON_COPYABLE(IntrusiveListImpl);
  77. private:
  78. IntrusiveListNode m_root_node;
  79. public:
  80. template <bool Const>
  81. class Iterator;
  82. using value_type = IntrusiveListNode;
  83. using size_type = size_t;
  84. using difference_type = ptrdiff_t;
  85. using pointer = value_type*;
  86. using const_pointer = const value_type*;
  87. using reference = value_type&;
  88. using const_reference = const value_type&;
  89. using iterator = Iterator<false>;
  90. using const_iterator = Iterator<true>;
  91. using reverse_iterator = std::reverse_iterator<iterator>;
  92. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  93. template <bool Const>
  94. class Iterator {
  95. public:
  96. using iterator_category = std::bidirectional_iterator_tag;
  97. using value_type = typename IntrusiveListImpl::value_type;
  98. using difference_type = typename IntrusiveListImpl::difference_type;
  99. using pointer =
  100. std::conditional_t<Const, IntrusiveListImpl::const_pointer, IntrusiveListImpl::pointer>;
  101. using reference = std::conditional_t<Const, IntrusiveListImpl::const_reference,
  102. IntrusiveListImpl::reference>;
  103. private:
  104. pointer m_node;
  105. public:
  106. constexpr explicit Iterator(pointer n) : m_node(n) {}
  107. constexpr bool operator==(const Iterator& rhs) const {
  108. return m_node == rhs.m_node;
  109. }
  110. constexpr pointer operator->() const {
  111. return m_node;
  112. }
  113. constexpr reference operator*() const {
  114. return *m_node;
  115. }
  116. constexpr Iterator& operator++() {
  117. m_node = m_node->m_next;
  118. return *this;
  119. }
  120. constexpr Iterator& operator--() {
  121. m_node = m_node->m_prev;
  122. return *this;
  123. }
  124. constexpr Iterator operator++(int) {
  125. const Iterator it{*this};
  126. ++(*this);
  127. return it;
  128. }
  129. constexpr Iterator operator--(int) {
  130. const Iterator it{*this};
  131. --(*this);
  132. return it;
  133. }
  134. constexpr operator Iterator<true>() const {
  135. return Iterator<true>(m_node);
  136. }
  137. constexpr Iterator<false> GetNonConstIterator() const {
  138. return Iterator<false>(const_cast<IntrusiveListImpl::pointer>(m_node));
  139. }
  140. };
  141. public:
  142. constexpr IntrusiveListImpl() : m_root_node() {}
  143. // Iterator accessors.
  144. constexpr iterator begin() {
  145. return iterator(m_root_node.GetNext());
  146. }
  147. constexpr const_iterator begin() const {
  148. return const_iterator(m_root_node.GetNext());
  149. }
  150. constexpr iterator end() {
  151. return iterator(std::addressof(m_root_node));
  152. }
  153. constexpr const_iterator end() const {
  154. return const_iterator(std::addressof(m_root_node));
  155. }
  156. constexpr iterator iterator_to(reference v) {
  157. // Only allow iterator_to for values in lists.
  158. ASSERT(v.IsLinked());
  159. return iterator(std::addressof(v));
  160. }
  161. constexpr const_iterator iterator_to(const_reference v) const {
  162. // Only allow iterator_to for values in lists.
  163. ASSERT(v.IsLinked());
  164. return const_iterator(std::addressof(v));
  165. }
  166. // Content management.
  167. constexpr bool empty() const {
  168. return !m_root_node.IsLinked();
  169. }
  170. constexpr size_type size() const {
  171. return static_cast<size_type>(std::distance(this->begin(), this->end()));
  172. }
  173. constexpr reference back() {
  174. return *m_root_node.GetPrev();
  175. }
  176. constexpr const_reference back() const {
  177. return *m_root_node.GetPrev();
  178. }
  179. constexpr reference front() {
  180. return *m_root_node.GetNext();
  181. }
  182. constexpr const_reference front() const {
  183. return *m_root_node.GetNext();
  184. }
  185. constexpr void push_back(reference node) {
  186. m_root_node.LinkPrev(std::addressof(node));
  187. }
  188. constexpr void push_front(reference node) {
  189. m_root_node.LinkNext(std::addressof(node));
  190. }
  191. constexpr void pop_back() {
  192. m_root_node.GetPrev()->Unlink();
  193. }
  194. constexpr void pop_front() {
  195. m_root_node.GetNext()->Unlink();
  196. }
  197. constexpr iterator insert(const_iterator pos, reference node) {
  198. pos.GetNonConstIterator()->LinkPrev(std::addressof(node));
  199. return iterator(std::addressof(node));
  200. }
  201. constexpr void splice(const_iterator pos, IntrusiveListImpl& o) {
  202. splice_impl(pos, o.begin(), o.end());
  203. }
  204. constexpr void splice(const_iterator pos, IntrusiveListImpl& o, const_iterator first) {
  205. const_iterator last(first);
  206. std::advance(last, 1);
  207. splice_impl(pos, first, last);
  208. }
  209. constexpr void splice(const_iterator pos, IntrusiveListImpl& o, const_iterator first,
  210. const_iterator last) {
  211. splice_impl(pos, first, last);
  212. }
  213. constexpr iterator erase(const_iterator pos) {
  214. if (pos == this->end()) {
  215. return this->end();
  216. }
  217. iterator it(pos.GetNonConstIterator());
  218. (it++)->Unlink();
  219. return it;
  220. }
  221. constexpr void clear() {
  222. while (!this->empty()) {
  223. this->pop_front();
  224. }
  225. }
  226. private:
  227. constexpr void splice_impl(const_iterator _pos, const_iterator _first, const_iterator _last) {
  228. if (_first == _last) {
  229. return;
  230. }
  231. iterator pos(_pos.GetNonConstIterator());
  232. iterator first(_first.GetNonConstIterator());
  233. iterator last(_last.GetNonConstIterator());
  234. first->Unlink(std::addressof(*last));
  235. pos->SplicePrev(std::addressof(*first), std::addressof(*first));
  236. }
  237. };
  238. } // namespace impl
  239. template <class T, class Traits>
  240. class IntrusiveList {
  241. YUZU_NON_COPYABLE(IntrusiveList);
  242. private:
  243. impl::IntrusiveListImpl m_impl;
  244. public:
  245. template <bool Const>
  246. class Iterator;
  247. using value_type = T;
  248. using size_type = size_t;
  249. using difference_type = ptrdiff_t;
  250. using pointer = value_type*;
  251. using const_pointer = const value_type*;
  252. using reference = value_type&;
  253. using const_reference = const value_type&;
  254. using iterator = Iterator<false>;
  255. using const_iterator = Iterator<true>;
  256. using reverse_iterator = std::reverse_iterator<iterator>;
  257. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  258. template <bool Const>
  259. class Iterator {
  260. public:
  261. friend class Common::IntrusiveList<T, Traits>;
  262. using ImplIterator =
  263. std::conditional_t<Const, Common::impl::IntrusiveListImpl::const_iterator,
  264. Common::impl::IntrusiveListImpl::iterator>;
  265. using iterator_category = std::bidirectional_iterator_tag;
  266. using value_type = typename IntrusiveList::value_type;
  267. using difference_type = typename IntrusiveList::difference_type;
  268. using pointer =
  269. std::conditional_t<Const, IntrusiveList::const_pointer, IntrusiveList::pointer>;
  270. using reference =
  271. std::conditional_t<Const, IntrusiveList::const_reference, IntrusiveList::reference>;
  272. private:
  273. ImplIterator m_iterator;
  274. private:
  275. constexpr explicit Iterator(ImplIterator it) : m_iterator(it) {}
  276. constexpr ImplIterator GetImplIterator() const {
  277. return m_iterator;
  278. }
  279. public:
  280. constexpr bool operator==(const Iterator& rhs) const {
  281. return m_iterator == rhs.m_iterator;
  282. }
  283. constexpr pointer operator->() const {
  284. return std::addressof(Traits::GetParent(*m_iterator));
  285. }
  286. constexpr reference operator*() const {
  287. return Traits::GetParent(*m_iterator);
  288. }
  289. constexpr Iterator& operator++() {
  290. ++m_iterator;
  291. return *this;
  292. }
  293. constexpr Iterator& operator--() {
  294. --m_iterator;
  295. return *this;
  296. }
  297. constexpr Iterator operator++(int) {
  298. const Iterator it{*this};
  299. ++m_iterator;
  300. return it;
  301. }
  302. constexpr Iterator operator--(int) {
  303. const Iterator it{*this};
  304. --m_iterator;
  305. return it;
  306. }
  307. constexpr operator Iterator<true>() const {
  308. return Iterator<true>(m_iterator);
  309. }
  310. };
  311. private:
  312. static constexpr IntrusiveListNode& GetNode(reference ref) {
  313. return Traits::GetNode(ref);
  314. }
  315. static constexpr IntrusiveListNode const& GetNode(const_reference ref) {
  316. return Traits::GetNode(ref);
  317. }
  318. static constexpr reference GetParent(IntrusiveListNode& node) {
  319. return Traits::GetParent(node);
  320. }
  321. static constexpr const_reference GetParent(IntrusiveListNode const& node) {
  322. return Traits::GetParent(node);
  323. }
  324. public:
  325. constexpr IntrusiveList() : m_impl() {}
  326. // Iterator accessors.
  327. constexpr iterator begin() {
  328. return iterator(m_impl.begin());
  329. }
  330. constexpr const_iterator begin() const {
  331. return const_iterator(m_impl.begin());
  332. }
  333. constexpr iterator end() {
  334. return iterator(m_impl.end());
  335. }
  336. constexpr const_iterator end() const {
  337. return const_iterator(m_impl.end());
  338. }
  339. constexpr const_iterator cbegin() const {
  340. return this->begin();
  341. }
  342. constexpr const_iterator cend() const {
  343. return this->end();
  344. }
  345. constexpr reverse_iterator rbegin() {
  346. return reverse_iterator(this->end());
  347. }
  348. constexpr const_reverse_iterator rbegin() const {
  349. return const_reverse_iterator(this->end());
  350. }
  351. constexpr reverse_iterator rend() {
  352. return reverse_iterator(this->begin());
  353. }
  354. constexpr const_reverse_iterator rend() const {
  355. return const_reverse_iterator(this->begin());
  356. }
  357. constexpr const_reverse_iterator crbegin() const {
  358. return this->rbegin();
  359. }
  360. constexpr const_reverse_iterator crend() const {
  361. return this->rend();
  362. }
  363. constexpr iterator iterator_to(reference v) {
  364. return iterator(m_impl.iterator_to(GetNode(v)));
  365. }
  366. constexpr const_iterator iterator_to(const_reference v) const {
  367. return const_iterator(m_impl.iterator_to(GetNode(v)));
  368. }
  369. // Content management.
  370. constexpr bool empty() const {
  371. return m_impl.empty();
  372. }
  373. constexpr size_type size() const {
  374. return m_impl.size();
  375. }
  376. constexpr reference back() {
  377. return GetParent(m_impl.back());
  378. }
  379. constexpr const_reference back() const {
  380. return GetParent(m_impl.back());
  381. }
  382. constexpr reference front() {
  383. return GetParent(m_impl.front());
  384. }
  385. constexpr const_reference front() const {
  386. return GetParent(m_impl.front());
  387. }
  388. constexpr void push_back(reference ref) {
  389. m_impl.push_back(GetNode(ref));
  390. }
  391. constexpr void push_front(reference ref) {
  392. m_impl.push_front(GetNode(ref));
  393. }
  394. constexpr void pop_back() {
  395. m_impl.pop_back();
  396. }
  397. constexpr void pop_front() {
  398. m_impl.pop_front();
  399. }
  400. constexpr iterator insert(const_iterator pos, reference ref) {
  401. return iterator(m_impl.insert(pos.GetImplIterator(), GetNode(ref)));
  402. }
  403. constexpr void splice(const_iterator pos, IntrusiveList& o) {
  404. m_impl.splice(pos.GetImplIterator(), o.m_impl);
  405. }
  406. constexpr void splice(const_iterator pos, IntrusiveList& o, const_iterator first) {
  407. m_impl.splice(pos.GetImplIterator(), o.m_impl, first.GetImplIterator());
  408. }
  409. constexpr void splice(const_iterator pos, IntrusiveList& o, const_iterator first,
  410. const_iterator last) {
  411. m_impl.splice(pos.GetImplIterator(), o.m_impl, first.GetImplIterator(),
  412. last.GetImplIterator());
  413. }
  414. constexpr iterator erase(const_iterator pos) {
  415. return iterator(m_impl.erase(pos.GetImplIterator()));
  416. }
  417. constexpr void clear() {
  418. m_impl.clear();
  419. }
  420. };
  421. template <auto T, class Derived = Common::impl::GetParentType<T>>
  422. class IntrusiveListMemberTraits;
  423. template <class Parent, IntrusiveListNode Parent::*Member, class Derived>
  424. class IntrusiveListMemberTraits<Member, Derived> {
  425. public:
  426. using ListType = IntrusiveList<Derived, IntrusiveListMemberTraits>;
  427. private:
  428. friend class IntrusiveList<Derived, IntrusiveListMemberTraits>;
  429. static constexpr IntrusiveListNode& GetNode(Derived& parent) {
  430. return parent.*Member;
  431. }
  432. static constexpr IntrusiveListNode const& GetNode(Derived const& parent) {
  433. return parent.*Member;
  434. }
  435. static Derived& GetParent(IntrusiveListNode& node) {
  436. return Common::GetParentReference<Member, Derived>(std::addressof(node));
  437. }
  438. static Derived const& GetParent(IntrusiveListNode const& node) {
  439. return Common::GetParentReference<Member, Derived>(std::addressof(node));
  440. }
  441. };
  442. template <auto T, class Derived = Common::impl::GetParentType<T>>
  443. class IntrusiveListMemberTraitsByNonConstexprOffsetOf;
  444. template <class Parent, IntrusiveListNode Parent::*Member, class Derived>
  445. class IntrusiveListMemberTraitsByNonConstexprOffsetOf<Member, Derived> {
  446. public:
  447. using ListType = IntrusiveList<Derived, IntrusiveListMemberTraitsByNonConstexprOffsetOf>;
  448. private:
  449. friend class IntrusiveList<Derived, IntrusiveListMemberTraitsByNonConstexprOffsetOf>;
  450. static constexpr IntrusiveListNode& GetNode(Derived& parent) {
  451. return parent.*Member;
  452. }
  453. static constexpr IntrusiveListNode const& GetNode(Derived const& parent) {
  454. return parent.*Member;
  455. }
  456. static Derived& GetParent(IntrusiveListNode& node) {
  457. return *reinterpret_cast<Derived*>(reinterpret_cast<char*>(std::addressof(node)) -
  458. GetOffset());
  459. }
  460. static Derived const& GetParent(IntrusiveListNode const& node) {
  461. return *reinterpret_cast<const Derived*>(
  462. reinterpret_cast<const char*>(std::addressof(node)) - GetOffset());
  463. }
  464. static uintptr_t GetOffset() {
  465. return reinterpret_cast<uintptr_t>(std::addressof(reinterpret_cast<Derived*>(0)->*Member));
  466. }
  467. };
  468. template <class Derived>
  469. class IntrusiveListBaseNode : public IntrusiveListNode {};
  470. template <class Derived>
  471. class IntrusiveListBaseTraits {
  472. public:
  473. using ListType = IntrusiveList<Derived, IntrusiveListBaseTraits>;
  474. private:
  475. friend class IntrusiveList<Derived, IntrusiveListBaseTraits>;
  476. static constexpr IntrusiveListNode& GetNode(Derived& parent) {
  477. return static_cast<IntrusiveListNode&>(
  478. static_cast<IntrusiveListBaseNode<Derived>&>(parent));
  479. }
  480. static constexpr IntrusiveListNode const& GetNode(Derived const& parent) {
  481. return static_cast<const IntrusiveListNode&>(
  482. static_cast<const IntrusiveListBaseNode<Derived>&>(parent));
  483. }
  484. static constexpr Derived& GetParent(IntrusiveListNode& node) {
  485. return static_cast<Derived&>(static_cast<IntrusiveListBaseNode<Derived>&>(node));
  486. }
  487. static constexpr Derived const& GetParent(IntrusiveListNode const& node) {
  488. return static_cast<const Derived&>(
  489. static_cast<const IntrusiveListBaseNode<Derived>&>(node));
  490. }
  491. };
  492. } // namespace Common