intrusive_red_black_tree.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/parent_of_member.h"
  6. #include "common/tree.h"
  7. namespace Common {
  8. namespace impl {
  9. class IntrusiveRedBlackTreeImpl;
  10. }
  11. struct IntrusiveRedBlackTreeNode {
  12. public:
  13. using EntryType = RBEntry<IntrusiveRedBlackTreeNode>;
  14. constexpr IntrusiveRedBlackTreeNode() = default;
  15. void SetEntry(const EntryType& new_entry) {
  16. entry = new_entry;
  17. }
  18. [[nodiscard]] EntryType& GetEntry() {
  19. return entry;
  20. }
  21. [[nodiscard]] const EntryType& GetEntry() const {
  22. return entry;
  23. }
  24. private:
  25. EntryType entry{};
  26. friend class impl::IntrusiveRedBlackTreeImpl;
  27. template <class, class, class>
  28. friend class IntrusiveRedBlackTree;
  29. };
  30. template <class T, class Traits, class Comparator>
  31. class IntrusiveRedBlackTree;
  32. namespace impl {
  33. class IntrusiveRedBlackTreeImpl {
  34. private:
  35. template <class, class, class>
  36. friend class ::Common::IntrusiveRedBlackTree;
  37. using RootType = RBHead<IntrusiveRedBlackTreeNode>;
  38. RootType root;
  39. public:
  40. template <bool Const>
  41. class Iterator;
  42. using value_type = IntrusiveRedBlackTreeNode;
  43. using size_type = size_t;
  44. using difference_type = ptrdiff_t;
  45. using pointer = value_type*;
  46. using const_pointer = const value_type*;
  47. using reference = value_type&;
  48. using const_reference = const value_type&;
  49. using iterator = Iterator<false>;
  50. using const_iterator = Iterator<true>;
  51. template <bool Const>
  52. class Iterator {
  53. public:
  54. using iterator_category = std::bidirectional_iterator_tag;
  55. using value_type = typename IntrusiveRedBlackTreeImpl::value_type;
  56. using difference_type = typename IntrusiveRedBlackTreeImpl::difference_type;
  57. using pointer = std::conditional_t<Const, IntrusiveRedBlackTreeImpl::const_pointer,
  58. IntrusiveRedBlackTreeImpl::pointer>;
  59. using reference = std::conditional_t<Const, IntrusiveRedBlackTreeImpl::const_reference,
  60. IntrusiveRedBlackTreeImpl::reference>;
  61. private:
  62. pointer node;
  63. public:
  64. explicit Iterator(pointer n) : node(n) {}
  65. bool operator==(const Iterator& rhs) const {
  66. return this->node == rhs.node;
  67. }
  68. bool operator!=(const Iterator& rhs) const {
  69. return !(*this == rhs);
  70. }
  71. pointer operator->() const {
  72. return this->node;
  73. }
  74. reference operator*() const {
  75. return *this->node;
  76. }
  77. Iterator& operator++() {
  78. this->node = GetNext(this->node);
  79. return *this;
  80. }
  81. Iterator& operator--() {
  82. this->node = GetPrev(this->node);
  83. return *this;
  84. }
  85. Iterator operator++(int) {
  86. const Iterator it{*this};
  87. ++(*this);
  88. return it;
  89. }
  90. Iterator operator--(int) {
  91. const Iterator it{*this};
  92. --(*this);
  93. return it;
  94. }
  95. operator Iterator<true>() const {
  96. return Iterator<true>(this->node);
  97. }
  98. };
  99. private:
  100. // Define accessors using RB_* functions.
  101. bool EmptyImpl() const {
  102. return root.IsEmpty();
  103. }
  104. IntrusiveRedBlackTreeNode* GetMinImpl() const {
  105. return RB_MIN(const_cast<RootType*>(&root));
  106. }
  107. IntrusiveRedBlackTreeNode* GetMaxImpl() const {
  108. return RB_MAX(const_cast<RootType*>(&root));
  109. }
  110. IntrusiveRedBlackTreeNode* RemoveImpl(IntrusiveRedBlackTreeNode* node) {
  111. return RB_REMOVE(&root, node);
  112. }
  113. public:
  114. static IntrusiveRedBlackTreeNode* GetNext(IntrusiveRedBlackTreeNode* node) {
  115. return RB_NEXT(node);
  116. }
  117. static IntrusiveRedBlackTreeNode* GetPrev(IntrusiveRedBlackTreeNode* node) {
  118. return RB_PREV(node);
  119. }
  120. static const IntrusiveRedBlackTreeNode* GetNext(const IntrusiveRedBlackTreeNode* node) {
  121. return static_cast<const IntrusiveRedBlackTreeNode*>(
  122. GetNext(const_cast<IntrusiveRedBlackTreeNode*>(node)));
  123. }
  124. static const IntrusiveRedBlackTreeNode* GetPrev(const IntrusiveRedBlackTreeNode* node) {
  125. return static_cast<const IntrusiveRedBlackTreeNode*>(
  126. GetPrev(const_cast<IntrusiveRedBlackTreeNode*>(node)));
  127. }
  128. public:
  129. constexpr IntrusiveRedBlackTreeImpl() {}
  130. // Iterator accessors.
  131. iterator begin() {
  132. return iterator(this->GetMinImpl());
  133. }
  134. const_iterator begin() const {
  135. return const_iterator(this->GetMinImpl());
  136. }
  137. iterator end() {
  138. return iterator(static_cast<IntrusiveRedBlackTreeNode*>(nullptr));
  139. }
  140. const_iterator end() const {
  141. return const_iterator(static_cast<const IntrusiveRedBlackTreeNode*>(nullptr));
  142. }
  143. const_iterator cbegin() const {
  144. return this->begin();
  145. }
  146. const_iterator cend() const {
  147. return this->end();
  148. }
  149. iterator iterator_to(reference ref) {
  150. return iterator(&ref);
  151. }
  152. const_iterator iterator_to(const_reference ref) const {
  153. return const_iterator(&ref);
  154. }
  155. // Content management.
  156. bool empty() const {
  157. return this->EmptyImpl();
  158. }
  159. reference back() {
  160. return *this->GetMaxImpl();
  161. }
  162. const_reference back() const {
  163. return *this->GetMaxImpl();
  164. }
  165. reference front() {
  166. return *this->GetMinImpl();
  167. }
  168. const_reference front() const {
  169. return *this->GetMinImpl();
  170. }
  171. iterator erase(iterator it) {
  172. auto cur = std::addressof(*it);
  173. auto next = GetNext(cur);
  174. this->RemoveImpl(cur);
  175. return iterator(next);
  176. }
  177. };
  178. } // namespace impl
  179. template <typename T>
  180. concept HasLightCompareType = requires {
  181. { std::is_same<typename T::LightCompareType, void>::value }
  182. ->std::convertible_to<bool>;
  183. };
  184. namespace impl {
  185. template <typename T, typename Default>
  186. consteval auto* GetLightCompareType() {
  187. if constexpr (HasLightCompareType<T>) {
  188. return static_cast<typename T::LightCompareType*>(nullptr);
  189. } else {
  190. return static_cast<Default*>(nullptr);
  191. }
  192. }
  193. } // namespace impl
  194. template <typename T, typename Default>
  195. using LightCompareType = std::remove_pointer_t<decltype(impl::GetLightCompareType<T, Default>())>;
  196. template <class T, class Traits, class Comparator>
  197. class IntrusiveRedBlackTree {
  198. public:
  199. using ImplType = impl::IntrusiveRedBlackTreeImpl;
  200. private:
  201. ImplType impl{};
  202. public:
  203. template <bool Const>
  204. class Iterator;
  205. using value_type = T;
  206. using size_type = size_t;
  207. using difference_type = ptrdiff_t;
  208. using pointer = T*;
  209. using const_pointer = const T*;
  210. using reference = T&;
  211. using const_reference = const T&;
  212. using iterator = Iterator<false>;
  213. using const_iterator = Iterator<true>;
  214. using light_value_type = LightCompareType<Comparator, value_type>;
  215. using const_light_pointer = const light_value_type*;
  216. using const_light_reference = const light_value_type&;
  217. template <bool Const>
  218. class Iterator {
  219. public:
  220. friend class IntrusiveRedBlackTree<T, Traits, Comparator>;
  221. using ImplIterator =
  222. std::conditional_t<Const, ImplType::const_iterator, ImplType::iterator>;
  223. using iterator_category = std::bidirectional_iterator_tag;
  224. using value_type = typename IntrusiveRedBlackTree::value_type;
  225. using difference_type = typename IntrusiveRedBlackTree::difference_type;
  226. using pointer = std::conditional_t<Const, IntrusiveRedBlackTree::const_pointer,
  227. IntrusiveRedBlackTree::pointer>;
  228. using reference = std::conditional_t<Const, IntrusiveRedBlackTree::const_reference,
  229. IntrusiveRedBlackTree::reference>;
  230. private:
  231. ImplIterator iterator;
  232. private:
  233. explicit Iterator(ImplIterator it) : iterator(it) {}
  234. explicit Iterator(typename std::conditional<Const, ImplType::const_iterator,
  235. ImplType::iterator>::type::pointer ptr)
  236. : iterator(ptr) {}
  237. ImplIterator GetImplIterator() const {
  238. return this->iterator;
  239. }
  240. public:
  241. bool operator==(const Iterator& rhs) const {
  242. return this->iterator == rhs.iterator;
  243. }
  244. bool operator!=(const Iterator& rhs) const {
  245. return !(*this == rhs);
  246. }
  247. pointer operator->() const {
  248. return Traits::GetParent(std::addressof(*this->iterator));
  249. }
  250. reference operator*() const {
  251. return *Traits::GetParent(std::addressof(*this->iterator));
  252. }
  253. Iterator& operator++() {
  254. ++this->iterator;
  255. return *this;
  256. }
  257. Iterator& operator--() {
  258. --this->iterator;
  259. return *this;
  260. }
  261. Iterator operator++(int) {
  262. const Iterator it{*this};
  263. ++this->iterator;
  264. return it;
  265. }
  266. Iterator operator--(int) {
  267. const Iterator it{*this};
  268. --this->iterator;
  269. return it;
  270. }
  271. operator Iterator<true>() const {
  272. return Iterator<true>(this->iterator);
  273. }
  274. };
  275. private:
  276. static int CompareImpl(const IntrusiveRedBlackTreeNode* lhs,
  277. const IntrusiveRedBlackTreeNode* rhs) {
  278. return Comparator::Compare(*Traits::GetParent(lhs), *Traits::GetParent(rhs));
  279. }
  280. static int LightCompareImpl(const void* elm, const IntrusiveRedBlackTreeNode* rhs) {
  281. return Comparator::Compare(*static_cast<const_light_pointer>(elm), *Traits::GetParent(rhs));
  282. }
  283. // Define accessors using RB_* functions.
  284. IntrusiveRedBlackTreeNode* InsertImpl(IntrusiveRedBlackTreeNode* node) {
  285. return RB_INSERT(&impl.root, node, CompareImpl);
  286. }
  287. IntrusiveRedBlackTreeNode* FindImpl(const IntrusiveRedBlackTreeNode* node) const {
  288. return RB_FIND(const_cast<ImplType::RootType*>(&impl.root),
  289. const_cast<IntrusiveRedBlackTreeNode*>(node), CompareImpl);
  290. }
  291. IntrusiveRedBlackTreeNode* NFindImpl(const IntrusiveRedBlackTreeNode* node) const {
  292. return RB_NFIND(const_cast<ImplType::RootType*>(&impl.root),
  293. const_cast<IntrusiveRedBlackTreeNode*>(node), CompareImpl);
  294. }
  295. IntrusiveRedBlackTreeNode* FindLightImpl(const_light_pointer lelm) const {
  296. return RB_FIND_LIGHT(const_cast<ImplType::RootType*>(&impl.root),
  297. static_cast<const void*>(lelm), LightCompareImpl);
  298. }
  299. IntrusiveRedBlackTreeNode* NFindLightImpl(const_light_pointer lelm) const {
  300. return RB_NFIND_LIGHT(const_cast<ImplType::RootType*>(&impl.root),
  301. static_cast<const void*>(lelm), LightCompareImpl);
  302. }
  303. public:
  304. constexpr IntrusiveRedBlackTree() = default;
  305. // Iterator accessors.
  306. iterator begin() {
  307. return iterator(this->impl.begin());
  308. }
  309. const_iterator begin() const {
  310. return const_iterator(this->impl.begin());
  311. }
  312. iterator end() {
  313. return iterator(this->impl.end());
  314. }
  315. const_iterator end() const {
  316. return const_iterator(this->impl.end());
  317. }
  318. const_iterator cbegin() const {
  319. return this->begin();
  320. }
  321. const_iterator cend() const {
  322. return this->end();
  323. }
  324. iterator iterator_to(reference ref) {
  325. return iterator(this->impl.iterator_to(*Traits::GetNode(std::addressof(ref))));
  326. }
  327. const_iterator iterator_to(const_reference ref) const {
  328. return const_iterator(this->impl.iterator_to(*Traits::GetNode(std::addressof(ref))));
  329. }
  330. // Content management.
  331. bool empty() const {
  332. return this->impl.empty();
  333. }
  334. reference back() {
  335. return *Traits::GetParent(std::addressof(this->impl.back()));
  336. }
  337. const_reference back() const {
  338. return *Traits::GetParent(std::addressof(this->impl.back()));
  339. }
  340. reference front() {
  341. return *Traits::GetParent(std::addressof(this->impl.front()));
  342. }
  343. const_reference front() const {
  344. return *Traits::GetParent(std::addressof(this->impl.front()));
  345. }
  346. iterator erase(iterator it) {
  347. return iterator(this->impl.erase(it.GetImplIterator()));
  348. }
  349. iterator insert(reference ref) {
  350. ImplType::pointer node = Traits::GetNode(std::addressof(ref));
  351. this->InsertImpl(node);
  352. return iterator(node);
  353. }
  354. iterator find(const_reference ref) const {
  355. return iterator(this->FindImpl(Traits::GetNode(std::addressof(ref))));
  356. }
  357. iterator nfind(const_reference ref) const {
  358. return iterator(this->NFindImpl(Traits::GetNode(std::addressof(ref))));
  359. }
  360. iterator find_light(const_light_reference ref) const {
  361. return iterator(this->FindLightImpl(std::addressof(ref)));
  362. }
  363. iterator nfind_light(const_light_reference ref) const {
  364. return iterator(this->NFindLightImpl(std::addressof(ref)));
  365. }
  366. };
  367. template <auto T, class Derived = impl::GetParentType<T>>
  368. class IntrusiveRedBlackTreeMemberTraits;
  369. template <class Parent, IntrusiveRedBlackTreeNode Parent::*Member, class Derived>
  370. class IntrusiveRedBlackTreeMemberTraits<Member, Derived> {
  371. public:
  372. template <class Comparator>
  373. using TreeType = IntrusiveRedBlackTree<Derived, IntrusiveRedBlackTreeMemberTraits, Comparator>;
  374. using TreeTypeImpl = impl::IntrusiveRedBlackTreeImpl;
  375. private:
  376. template <class, class, class>
  377. friend class IntrusiveRedBlackTree;
  378. friend class impl::IntrusiveRedBlackTreeImpl;
  379. static constexpr IntrusiveRedBlackTreeNode* GetNode(Derived* parent) {
  380. return std::addressof(parent->*Member);
  381. }
  382. static constexpr IntrusiveRedBlackTreeNode const* GetNode(Derived const* parent) {
  383. return std::addressof(parent->*Member);
  384. }
  385. static constexpr Derived* GetParent(IntrusiveRedBlackTreeNode* node) {
  386. return GetParentPointer<Member, Derived>(node);
  387. }
  388. static constexpr Derived const* GetParent(const IntrusiveRedBlackTreeNode* node) {
  389. return GetParentPointer<Member, Derived>(node);
  390. }
  391. private:
  392. static constexpr TypedStorage<Derived> DerivedStorage = {};
  393. static_assert(GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage));
  394. };
  395. template <auto T, class Derived = impl::GetParentType<T>>
  396. class IntrusiveRedBlackTreeMemberTraitsDeferredAssert;
  397. template <class Parent, IntrusiveRedBlackTreeNode Parent::*Member, class Derived>
  398. class IntrusiveRedBlackTreeMemberTraitsDeferredAssert<Member, Derived> {
  399. public:
  400. template <class Comparator>
  401. using TreeType =
  402. IntrusiveRedBlackTree<Derived, IntrusiveRedBlackTreeMemberTraitsDeferredAssert, Comparator>;
  403. using TreeTypeImpl = impl::IntrusiveRedBlackTreeImpl;
  404. static constexpr bool IsValid() {
  405. TypedStorage<Derived> DerivedStorage = {};
  406. return GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage);
  407. }
  408. private:
  409. template <class, class, class>
  410. friend class IntrusiveRedBlackTree;
  411. friend class impl::IntrusiveRedBlackTreeImpl;
  412. static constexpr IntrusiveRedBlackTreeNode* GetNode(Derived* parent) {
  413. return std::addressof(parent->*Member);
  414. }
  415. static constexpr IntrusiveRedBlackTreeNode const* GetNode(Derived const* parent) {
  416. return std::addressof(parent->*Member);
  417. }
  418. static constexpr Derived* GetParent(IntrusiveRedBlackTreeNode* node) {
  419. return GetParentPointer<Member, Derived>(node);
  420. }
  421. static constexpr Derived const* GetParent(const IntrusiveRedBlackTreeNode* node) {
  422. return GetParentPointer<Member, Derived>(node);
  423. }
  424. };
  425. template <class Derived>
  426. class IntrusiveRedBlackTreeBaseNode : public IntrusiveRedBlackTreeNode {
  427. public:
  428. constexpr Derived* GetPrev() {
  429. return static_cast<Derived*>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this));
  430. }
  431. constexpr const Derived* GetPrev() const {
  432. return static_cast<const Derived*>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this));
  433. }
  434. constexpr Derived* GetNext() {
  435. return static_cast<Derived*>(impl::IntrusiveRedBlackTreeImpl::GetNext(this));
  436. }
  437. constexpr const Derived* GetNext() const {
  438. return static_cast<const Derived*>(impl::IntrusiveRedBlackTreeImpl::GetNext(this));
  439. }
  440. };
  441. template <class Derived>
  442. class IntrusiveRedBlackTreeBaseTraits {
  443. public:
  444. template <class Comparator>
  445. using TreeType = IntrusiveRedBlackTree<Derived, IntrusiveRedBlackTreeBaseTraits, Comparator>;
  446. using TreeTypeImpl = impl::IntrusiveRedBlackTreeImpl;
  447. private:
  448. template <class, class, class>
  449. friend class IntrusiveRedBlackTree;
  450. friend class impl::IntrusiveRedBlackTreeImpl;
  451. static constexpr IntrusiveRedBlackTreeNode* GetNode(Derived* parent) {
  452. return static_cast<IntrusiveRedBlackTreeNode*>(parent);
  453. }
  454. static constexpr IntrusiveRedBlackTreeNode const* GetNode(Derived const* parent) {
  455. return static_cast<const IntrusiveRedBlackTreeNode*>(parent);
  456. }
  457. static constexpr Derived* GetParent(IntrusiveRedBlackTreeNode* node) {
  458. return static_cast<Derived*>(node);
  459. }
  460. static constexpr Derived const* GetParent(const IntrusiveRedBlackTreeNode* node) {
  461. return static_cast<const Derived*>(node);
  462. }
  463. };
  464. } // namespace Common