intrusive_red_black_tree.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 } -> std::convertible_to<bool>;
  182. };
  183. namespace impl {
  184. template <typename T, typename Default>
  185. consteval auto* GetLightCompareType() {
  186. if constexpr (HasLightCompareType<T>) {
  187. return static_cast<typename T::LightCompareType*>(nullptr);
  188. } else {
  189. return static_cast<Default*>(nullptr);
  190. }
  191. }
  192. } // namespace impl
  193. template <typename T, typename Default>
  194. using LightCompareType = std::remove_pointer_t<decltype(impl::GetLightCompareType<T, Default>())>;
  195. template <class T, class Traits, class Comparator>
  196. class IntrusiveRedBlackTree {
  197. public:
  198. using ImplType = impl::IntrusiveRedBlackTreeImpl;
  199. private:
  200. ImplType impl{};
  201. public:
  202. template <bool Const>
  203. class Iterator;
  204. using value_type = T;
  205. using size_type = size_t;
  206. using difference_type = ptrdiff_t;
  207. using pointer = T*;
  208. using const_pointer = const T*;
  209. using reference = T&;
  210. using const_reference = const T&;
  211. using iterator = Iterator<false>;
  212. using const_iterator = Iterator<true>;
  213. using light_value_type = LightCompareType<Comparator, value_type>;
  214. using const_light_pointer = const light_value_type*;
  215. using const_light_reference = const light_value_type&;
  216. template <bool Const>
  217. class Iterator {
  218. public:
  219. friend class IntrusiveRedBlackTree<T, Traits, Comparator>;
  220. using ImplIterator =
  221. std::conditional_t<Const, ImplType::const_iterator, ImplType::iterator>;
  222. using iterator_category = std::bidirectional_iterator_tag;
  223. using value_type = typename IntrusiveRedBlackTree::value_type;
  224. using difference_type = typename IntrusiveRedBlackTree::difference_type;
  225. using pointer = std::conditional_t<Const, IntrusiveRedBlackTree::const_pointer,
  226. IntrusiveRedBlackTree::pointer>;
  227. using reference = std::conditional_t<Const, IntrusiveRedBlackTree::const_reference,
  228. IntrusiveRedBlackTree::reference>;
  229. private:
  230. ImplIterator iterator;
  231. private:
  232. explicit Iterator(ImplIterator it) : iterator(it) {}
  233. explicit Iterator(typename std::conditional<Const, ImplType::const_iterator,
  234. ImplType::iterator>::type::pointer ptr)
  235. : iterator(ptr) {}
  236. ImplIterator GetImplIterator() const {
  237. return this->iterator;
  238. }
  239. public:
  240. bool operator==(const Iterator& rhs) const {
  241. return this->iterator == rhs.iterator;
  242. }
  243. bool operator!=(const Iterator& rhs) const {
  244. return !(*this == rhs);
  245. }
  246. pointer operator->() const {
  247. return Traits::GetParent(std::addressof(*this->iterator));
  248. }
  249. reference operator*() const {
  250. return *Traits::GetParent(std::addressof(*this->iterator));
  251. }
  252. Iterator& operator++() {
  253. ++this->iterator;
  254. return *this;
  255. }
  256. Iterator& operator--() {
  257. --this->iterator;
  258. return *this;
  259. }
  260. Iterator operator++(int) {
  261. const Iterator it{*this};
  262. ++this->iterator;
  263. return it;
  264. }
  265. Iterator operator--(int) {
  266. const Iterator it{*this};
  267. --this->iterator;
  268. return it;
  269. }
  270. operator Iterator<true>() const {
  271. return Iterator<true>(this->iterator);
  272. }
  273. };
  274. private:
  275. static int CompareImpl(const IntrusiveRedBlackTreeNode* lhs,
  276. const IntrusiveRedBlackTreeNode* rhs) {
  277. return Comparator::Compare(*Traits::GetParent(lhs), *Traits::GetParent(rhs));
  278. }
  279. static int LightCompareImpl(const void* elm, const IntrusiveRedBlackTreeNode* rhs) {
  280. return Comparator::Compare(*static_cast<const_light_pointer>(elm), *Traits::GetParent(rhs));
  281. }
  282. // Define accessors using RB_* functions.
  283. IntrusiveRedBlackTreeNode* InsertImpl(IntrusiveRedBlackTreeNode* node) {
  284. return RB_INSERT(&impl.root, node, CompareImpl);
  285. }
  286. IntrusiveRedBlackTreeNode* FindImpl(const IntrusiveRedBlackTreeNode* node) const {
  287. return RB_FIND(const_cast<ImplType::RootType*>(&impl.root),
  288. const_cast<IntrusiveRedBlackTreeNode*>(node), CompareImpl);
  289. }
  290. IntrusiveRedBlackTreeNode* NFindImpl(const IntrusiveRedBlackTreeNode* node) const {
  291. return RB_NFIND(const_cast<ImplType::RootType*>(&impl.root),
  292. const_cast<IntrusiveRedBlackTreeNode*>(node), CompareImpl);
  293. }
  294. IntrusiveRedBlackTreeNode* FindLightImpl(const_light_pointer lelm) const {
  295. return RB_FIND_LIGHT(const_cast<ImplType::RootType*>(&impl.root),
  296. static_cast<const void*>(lelm), LightCompareImpl);
  297. }
  298. IntrusiveRedBlackTreeNode* NFindLightImpl(const_light_pointer lelm) const {
  299. return RB_NFIND_LIGHT(const_cast<ImplType::RootType*>(&impl.root),
  300. static_cast<const void*>(lelm), LightCompareImpl);
  301. }
  302. public:
  303. constexpr IntrusiveRedBlackTree() = default;
  304. // Iterator accessors.
  305. iterator begin() {
  306. return iterator(this->impl.begin());
  307. }
  308. const_iterator begin() const {
  309. return const_iterator(this->impl.begin());
  310. }
  311. iterator end() {
  312. return iterator(this->impl.end());
  313. }
  314. const_iterator end() const {
  315. return const_iterator(this->impl.end());
  316. }
  317. const_iterator cbegin() const {
  318. return this->begin();
  319. }
  320. const_iterator cend() const {
  321. return this->end();
  322. }
  323. iterator iterator_to(reference ref) {
  324. return iterator(this->impl.iterator_to(*Traits::GetNode(std::addressof(ref))));
  325. }
  326. const_iterator iterator_to(const_reference ref) const {
  327. return const_iterator(this->impl.iterator_to(*Traits::GetNode(std::addressof(ref))));
  328. }
  329. // Content management.
  330. bool empty() const {
  331. return this->impl.empty();
  332. }
  333. reference back() {
  334. return *Traits::GetParent(std::addressof(this->impl.back()));
  335. }
  336. const_reference back() const {
  337. return *Traits::GetParent(std::addressof(this->impl.back()));
  338. }
  339. reference front() {
  340. return *Traits::GetParent(std::addressof(this->impl.front()));
  341. }
  342. const_reference front() const {
  343. return *Traits::GetParent(std::addressof(this->impl.front()));
  344. }
  345. iterator erase(iterator it) {
  346. return iterator(this->impl.erase(it.GetImplIterator()));
  347. }
  348. iterator insert(reference ref) {
  349. ImplType::pointer node = Traits::GetNode(std::addressof(ref));
  350. this->InsertImpl(node);
  351. return iterator(node);
  352. }
  353. iterator find(const_reference ref) const {
  354. return iterator(this->FindImpl(Traits::GetNode(std::addressof(ref))));
  355. }
  356. iterator nfind(const_reference ref) const {
  357. return iterator(this->NFindImpl(Traits::GetNode(std::addressof(ref))));
  358. }
  359. iterator find_light(const_light_reference ref) const {
  360. return iterator(this->FindLightImpl(std::addressof(ref)));
  361. }
  362. iterator nfind_light(const_light_reference ref) const {
  363. return iterator(this->NFindLightImpl(std::addressof(ref)));
  364. }
  365. };
  366. template <auto T, class Derived = impl::GetParentType<T>>
  367. class IntrusiveRedBlackTreeMemberTraits;
  368. template <class Parent, IntrusiveRedBlackTreeNode Parent::*Member, class Derived>
  369. class IntrusiveRedBlackTreeMemberTraits<Member, Derived> {
  370. public:
  371. template <class Comparator>
  372. using TreeType = IntrusiveRedBlackTree<Derived, IntrusiveRedBlackTreeMemberTraits, Comparator>;
  373. using TreeTypeImpl = impl::IntrusiveRedBlackTreeImpl;
  374. private:
  375. template <class, class, class>
  376. friend class IntrusiveRedBlackTree;
  377. friend class impl::IntrusiveRedBlackTreeImpl;
  378. static constexpr IntrusiveRedBlackTreeNode* GetNode(Derived* parent) {
  379. return std::addressof(parent->*Member);
  380. }
  381. static constexpr IntrusiveRedBlackTreeNode const* GetNode(Derived const* parent) {
  382. return std::addressof(parent->*Member);
  383. }
  384. static constexpr Derived* GetParent(IntrusiveRedBlackTreeNode* node) {
  385. return GetParentPointer<Member, Derived>(node);
  386. }
  387. static constexpr Derived const* GetParent(const IntrusiveRedBlackTreeNode* node) {
  388. return GetParentPointer<Member, Derived>(node);
  389. }
  390. private:
  391. static constexpr TypedStorage<Derived> DerivedStorage = {};
  392. };
  393. template <auto T, class Derived = impl::GetParentType<T>>
  394. class IntrusiveRedBlackTreeMemberTraitsDeferredAssert;
  395. template <class Parent, IntrusiveRedBlackTreeNode Parent::*Member, class Derived>
  396. class IntrusiveRedBlackTreeMemberTraitsDeferredAssert<Member, Derived> {
  397. public:
  398. template <class Comparator>
  399. using TreeType =
  400. IntrusiveRedBlackTree<Derived, IntrusiveRedBlackTreeMemberTraitsDeferredAssert, Comparator>;
  401. using TreeTypeImpl = impl::IntrusiveRedBlackTreeImpl;
  402. static constexpr bool IsValid() {
  403. TypedStorage<Derived> DerivedStorage = {};
  404. return GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage);
  405. }
  406. private:
  407. template <class, class, class>
  408. friend class IntrusiveRedBlackTree;
  409. friend class impl::IntrusiveRedBlackTreeImpl;
  410. static constexpr IntrusiveRedBlackTreeNode* GetNode(Derived* parent) {
  411. return std::addressof(parent->*Member);
  412. }
  413. static constexpr IntrusiveRedBlackTreeNode const* GetNode(Derived const* parent) {
  414. return std::addressof(parent->*Member);
  415. }
  416. static constexpr Derived* GetParent(IntrusiveRedBlackTreeNode* node) {
  417. return GetParentPointer<Member, Derived>(node);
  418. }
  419. static constexpr Derived const* GetParent(const IntrusiveRedBlackTreeNode* node) {
  420. return GetParentPointer<Member, Derived>(node);
  421. }
  422. };
  423. template <class Derived>
  424. class IntrusiveRedBlackTreeBaseNode : public IntrusiveRedBlackTreeNode {
  425. public:
  426. constexpr Derived* GetPrev() {
  427. return static_cast<Derived*>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this));
  428. }
  429. constexpr const Derived* GetPrev() const {
  430. return static_cast<const Derived*>(impl::IntrusiveRedBlackTreeImpl::GetPrev(this));
  431. }
  432. constexpr Derived* GetNext() {
  433. return static_cast<Derived*>(impl::IntrusiveRedBlackTreeImpl::GetNext(this));
  434. }
  435. constexpr const Derived* GetNext() const {
  436. return static_cast<const Derived*>(impl::IntrusiveRedBlackTreeImpl::GetNext(this));
  437. }
  438. };
  439. template <class Derived>
  440. class IntrusiveRedBlackTreeBaseTraits {
  441. public:
  442. template <class Comparator>
  443. using TreeType = IntrusiveRedBlackTree<Derived, IntrusiveRedBlackTreeBaseTraits, Comparator>;
  444. using TreeTypeImpl = impl::IntrusiveRedBlackTreeImpl;
  445. private:
  446. template <class, class, class>
  447. friend class IntrusiveRedBlackTree;
  448. friend class impl::IntrusiveRedBlackTreeImpl;
  449. static constexpr IntrusiveRedBlackTreeNode* GetNode(Derived* parent) {
  450. return static_cast<IntrusiveRedBlackTreeNode*>(parent);
  451. }
  452. static constexpr IntrusiveRedBlackTreeNode const* GetNode(Derived const* parent) {
  453. return static_cast<const IntrusiveRedBlackTreeNode*>(parent);
  454. }
  455. static constexpr Derived* GetParent(IntrusiveRedBlackTreeNode* node) {
  456. return static_cast<Derived*>(node);
  457. }
  458. static constexpr Derived const* GetParent(const IntrusiveRedBlackTreeNode* node) {
  459. return static_cast<const Derived*>(node);
  460. }
  461. };
  462. } // namespace Common