diff options
Diffstat (limited to 'src/common/tree.h')
| -rw-r--r-- | src/common/tree.h | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/src/common/tree.h b/src/common/tree.h index f77859209..f4fc43de3 100644 --- a/src/common/tree.h +++ b/src/common/tree.h | |||
| @@ -103,12 +103,12 @@ concept IsRBEntry = CheckRBEntry<T>::value; | |||
| 103 | 103 | ||
| 104 | template <typename T> | 104 | template <typename T> |
| 105 | concept HasRBEntry = requires(T& t, const T& ct) { | 105 | concept HasRBEntry = requires(T& t, const T& ct) { |
| 106 | { t.GetRBEntry() } -> std::same_as<RBEntry<T>&>; | 106 | { t.GetRBEntry() } -> std::same_as<RBEntry<T>&>; |
| 107 | { ct.GetRBEntry() } -> std::same_as<const RBEntry<T>&>; | 107 | { ct.GetRBEntry() } -> std::same_as<const RBEntry<T>&>; |
| 108 | }; | 108 | }; |
| 109 | 109 | ||
| 110 | template <typename T> | 110 | template <typename T> |
| 111 | requires HasRBEntry<T> | 111 | requires HasRBEntry<T> |
| 112 | class RBHead { | 112 | class RBHead { |
| 113 | private: | 113 | private: |
| 114 | T* m_rbh_root = nullptr; | 114 | T* m_rbh_root = nullptr; |
| @@ -130,90 +130,90 @@ public: | |||
| 130 | }; | 130 | }; |
| 131 | 131 | ||
| 132 | template <typename T> | 132 | template <typename T> |
| 133 | requires HasRBEntry<T> | 133 | requires HasRBEntry<T> |
| 134 | [[nodiscard]] constexpr RBEntry<T>& RB_ENTRY(T* t) { | 134 | [[nodiscard]] constexpr RBEntry<T>& RB_ENTRY(T* t) { |
| 135 | return t->GetRBEntry(); | 135 | return t->GetRBEntry(); |
| 136 | } | 136 | } |
| 137 | template <typename T> | 137 | template <typename T> |
| 138 | requires HasRBEntry<T> | 138 | requires HasRBEntry<T> |
| 139 | [[nodiscard]] constexpr const RBEntry<T>& RB_ENTRY(const T* t) { | 139 | [[nodiscard]] constexpr const RBEntry<T>& RB_ENTRY(const T* t) { |
| 140 | return t->GetRBEntry(); | 140 | return t->GetRBEntry(); |
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | template <typename T> | 143 | template <typename T> |
| 144 | requires HasRBEntry<T> | 144 | requires HasRBEntry<T> |
| 145 | [[nodiscard]] constexpr T* RB_LEFT(T* t) { | 145 | [[nodiscard]] constexpr T* RB_LEFT(T* t) { |
| 146 | return RB_ENTRY(t).Left(); | 146 | return RB_ENTRY(t).Left(); |
| 147 | } | 147 | } |
| 148 | template <typename T> | 148 | template <typename T> |
| 149 | requires HasRBEntry<T> | 149 | requires HasRBEntry<T> |
| 150 | [[nodiscard]] constexpr const T* RB_LEFT(const T* t) { | 150 | [[nodiscard]] constexpr const T* RB_LEFT(const T* t) { |
| 151 | return RB_ENTRY(t).Left(); | 151 | return RB_ENTRY(t).Left(); |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | template <typename T> | 154 | template <typename T> |
| 155 | requires HasRBEntry<T> | 155 | requires HasRBEntry<T> |
| 156 | [[nodiscard]] constexpr T* RB_RIGHT(T* t) { | 156 | [[nodiscard]] constexpr T* RB_RIGHT(T* t) { |
| 157 | return RB_ENTRY(t).Right(); | 157 | return RB_ENTRY(t).Right(); |
| 158 | } | 158 | } |
| 159 | template <typename T> | 159 | template <typename T> |
| 160 | requires HasRBEntry<T> | 160 | requires HasRBEntry<T> |
| 161 | [[nodiscard]] constexpr const T* RB_RIGHT(const T* t) { | 161 | [[nodiscard]] constexpr const T* RB_RIGHT(const T* t) { |
| 162 | return RB_ENTRY(t).Right(); | 162 | return RB_ENTRY(t).Right(); |
| 163 | } | 163 | } |
| 164 | 164 | ||
| 165 | template <typename T> | 165 | template <typename T> |
| 166 | requires HasRBEntry<T> | 166 | requires HasRBEntry<T> |
| 167 | [[nodiscard]] constexpr T* RB_PARENT(T* t) { | 167 | [[nodiscard]] constexpr T* RB_PARENT(T* t) { |
| 168 | return RB_ENTRY(t).Parent(); | 168 | return RB_ENTRY(t).Parent(); |
| 169 | } | 169 | } |
| 170 | template <typename T> | 170 | template <typename T> |
| 171 | requires HasRBEntry<T> | 171 | requires HasRBEntry<T> |
| 172 | [[nodiscard]] constexpr const T* RB_PARENT(const T* t) { | 172 | [[nodiscard]] constexpr const T* RB_PARENT(const T* t) { |
| 173 | return RB_ENTRY(t).Parent(); | 173 | return RB_ENTRY(t).Parent(); |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | template <typename T> | 176 | template <typename T> |
| 177 | requires HasRBEntry<T> | 177 | requires HasRBEntry<T> |
| 178 | constexpr void RB_SET_LEFT(T* t, T* e) { | 178 | constexpr void RB_SET_LEFT(T* t, T* e) { |
| 179 | RB_ENTRY(t).SetLeft(e); | 179 | RB_ENTRY(t).SetLeft(e); |
| 180 | } | 180 | } |
| 181 | template <typename T> | 181 | template <typename T> |
| 182 | requires HasRBEntry<T> | 182 | requires HasRBEntry<T> |
| 183 | constexpr void RB_SET_RIGHT(T* t, T* e) { | 183 | constexpr void RB_SET_RIGHT(T* t, T* e) { |
| 184 | RB_ENTRY(t).SetRight(e); | 184 | RB_ENTRY(t).SetRight(e); |
| 185 | } | 185 | } |
| 186 | template <typename T> | 186 | template <typename T> |
| 187 | requires HasRBEntry<T> | 187 | requires HasRBEntry<T> |
| 188 | constexpr void RB_SET_PARENT(T* t, T* e) { | 188 | constexpr void RB_SET_PARENT(T* t, T* e) { |
| 189 | RB_ENTRY(t).SetParent(e); | 189 | RB_ENTRY(t).SetParent(e); |
| 190 | } | 190 | } |
| 191 | 191 | ||
| 192 | template <typename T> | 192 | template <typename T> |
| 193 | requires HasRBEntry<T> | 193 | requires HasRBEntry<T> |
| 194 | [[nodiscard]] constexpr bool RB_IS_BLACK(const T* t) { | 194 | [[nodiscard]] constexpr bool RB_IS_BLACK(const T* t) { |
| 195 | return RB_ENTRY(t).IsBlack(); | 195 | return RB_ENTRY(t).IsBlack(); |
| 196 | } | 196 | } |
| 197 | template <typename T> | 197 | template <typename T> |
| 198 | requires HasRBEntry<T> | 198 | requires HasRBEntry<T> |
| 199 | [[nodiscard]] constexpr bool RB_IS_RED(const T* t) { | 199 | [[nodiscard]] constexpr bool RB_IS_RED(const T* t) { |
| 200 | return RB_ENTRY(t).IsRed(); | 200 | return RB_ENTRY(t).IsRed(); |
| 201 | } | 201 | } |
| 202 | 202 | ||
| 203 | template <typename T> | 203 | template <typename T> |
| 204 | requires HasRBEntry<T> | 204 | requires HasRBEntry<T> |
| 205 | [[nodiscard]] constexpr RBColor RB_COLOR(const T* t) { | 205 | [[nodiscard]] constexpr RBColor RB_COLOR(const T* t) { |
| 206 | return RB_ENTRY(t).Color(); | 206 | return RB_ENTRY(t).Color(); |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | template <typename T> | 209 | template <typename T> |
| 210 | requires HasRBEntry<T> | 210 | requires HasRBEntry<T> |
| 211 | constexpr void RB_SET_COLOR(T* t, RBColor c) { | 211 | constexpr void RB_SET_COLOR(T* t, RBColor c) { |
| 212 | RB_ENTRY(t).SetColor(c); | 212 | RB_ENTRY(t).SetColor(c); |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | template <typename T> | 215 | template <typename T> |
| 216 | requires HasRBEntry<T> | 216 | requires HasRBEntry<T> |
| 217 | constexpr void RB_SET(T* elm, T* parent) { | 217 | constexpr void RB_SET(T* elm, T* parent) { |
| 218 | auto& rb_entry = RB_ENTRY(elm); | 218 | auto& rb_entry = RB_ENTRY(elm); |
| 219 | rb_entry.SetParent(parent); | 219 | rb_entry.SetParent(parent); |
| @@ -223,14 +223,14 @@ constexpr void RB_SET(T* elm, T* parent) { | |||
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | template <typename T> | 225 | template <typename T> |
| 226 | requires HasRBEntry<T> | 226 | requires HasRBEntry<T> |
| 227 | constexpr void RB_SET_BLACKRED(T* black, T* red) { | 227 | constexpr void RB_SET_BLACKRED(T* black, T* red) { |
| 228 | RB_SET_COLOR(black, RBColor::RB_BLACK); | 228 | RB_SET_COLOR(black, RBColor::RB_BLACK); |
| 229 | RB_SET_COLOR(red, RBColor::RB_RED); | 229 | RB_SET_COLOR(red, RBColor::RB_RED); |
| 230 | } | 230 | } |
| 231 | 231 | ||
| 232 | template <typename T> | 232 | template <typename T> |
| 233 | requires HasRBEntry<T> | 233 | requires HasRBEntry<T> |
| 234 | constexpr void RB_ROTATE_LEFT(RBHead<T>& head, T* elm, T*& tmp) { | 234 | constexpr void RB_ROTATE_LEFT(RBHead<T>& head, T* elm, T*& tmp) { |
| 235 | tmp = RB_RIGHT(elm); | 235 | tmp = RB_RIGHT(elm); |
| 236 | if (RB_SET_RIGHT(elm, RB_LEFT(tmp)); RB_RIGHT(elm) != nullptr) { | 236 | if (RB_SET_RIGHT(elm, RB_LEFT(tmp)); RB_RIGHT(elm) != nullptr) { |
| @@ -252,7 +252,7 @@ constexpr void RB_ROTATE_LEFT(RBHead<T>& head, T* elm, T*& tmp) { | |||
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | template <typename T> | 254 | template <typename T> |
| 255 | requires HasRBEntry<T> | 255 | requires HasRBEntry<T> |
| 256 | constexpr void RB_ROTATE_RIGHT(RBHead<T>& head, T* elm, T*& tmp) { | 256 | constexpr void RB_ROTATE_RIGHT(RBHead<T>& head, T* elm, T*& tmp) { |
| 257 | tmp = RB_LEFT(elm); | 257 | tmp = RB_LEFT(elm); |
| 258 | if (RB_SET_LEFT(elm, RB_RIGHT(tmp)); RB_LEFT(elm) != nullptr) { | 258 | if (RB_SET_LEFT(elm, RB_RIGHT(tmp)); RB_LEFT(elm) != nullptr) { |
| @@ -274,7 +274,7 @@ constexpr void RB_ROTATE_RIGHT(RBHead<T>& head, T* elm, T*& tmp) { | |||
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | template <typename T> | 276 | template <typename T> |
| 277 | requires HasRBEntry<T> | 277 | requires HasRBEntry<T> |
| 278 | constexpr void RB_REMOVE_COLOR(RBHead<T>& head, T* parent, T* elm) { | 278 | constexpr void RB_REMOVE_COLOR(RBHead<T>& head, T* parent, T* elm) { |
| 279 | T* tmp; | 279 | T* tmp; |
| 280 | while ((elm == nullptr || RB_IS_BLACK(elm)) && elm != head.Root()) { | 280 | while ((elm == nullptr || RB_IS_BLACK(elm)) && elm != head.Root()) { |
| @@ -358,7 +358,7 @@ constexpr void RB_REMOVE_COLOR(RBHead<T>& head, T* parent, T* elm) { | |||
| 358 | } | 358 | } |
| 359 | 359 | ||
| 360 | template <typename T> | 360 | template <typename T> |
| 361 | requires HasRBEntry<T> | 361 | requires HasRBEntry<T> |
| 362 | constexpr T* RB_REMOVE(RBHead<T>& head, T* elm) { | 362 | constexpr T* RB_REMOVE(RBHead<T>& head, T* elm) { |
| 363 | T* child = nullptr; | 363 | T* child = nullptr; |
| 364 | T* parent = nullptr; | 364 | T* parent = nullptr; |
| @@ -451,7 +451,7 @@ constexpr T* RB_REMOVE(RBHead<T>& head, T* elm) { | |||
| 451 | } | 451 | } |
| 452 | 452 | ||
| 453 | template <typename T> | 453 | template <typename T> |
| 454 | requires HasRBEntry<T> | 454 | requires HasRBEntry<T> |
| 455 | constexpr void RB_INSERT_COLOR(RBHead<T>& head, T* elm) { | 455 | constexpr void RB_INSERT_COLOR(RBHead<T>& head, T* elm) { |
| 456 | T *parent = nullptr, *tmp = nullptr; | 456 | T *parent = nullptr, *tmp = nullptr; |
| 457 | while ((parent = RB_PARENT(elm)) != nullptr && RB_IS_RED(parent)) { | 457 | while ((parent = RB_PARENT(elm)) != nullptr && RB_IS_RED(parent)) { |
| @@ -499,7 +499,7 @@ constexpr void RB_INSERT_COLOR(RBHead<T>& head, T* elm) { | |||
| 499 | } | 499 | } |
| 500 | 500 | ||
| 501 | template <typename T, typename Compare> | 501 | template <typename T, typename Compare> |
| 502 | requires HasRBEntry<T> | 502 | requires HasRBEntry<T> |
| 503 | constexpr T* RB_INSERT(RBHead<T>& head, T* elm, Compare cmp) { | 503 | constexpr T* RB_INSERT(RBHead<T>& head, T* elm, Compare cmp) { |
| 504 | T* parent = nullptr; | 504 | T* parent = nullptr; |
| 505 | T* tmp = head.Root(); | 505 | T* tmp = head.Root(); |
| @@ -534,7 +534,7 @@ constexpr T* RB_INSERT(RBHead<T>& head, T* elm, Compare cmp) { | |||
| 534 | } | 534 | } |
| 535 | 535 | ||
| 536 | template <typename T, typename Compare> | 536 | template <typename T, typename Compare> |
| 537 | requires HasRBEntry<T> | 537 | requires HasRBEntry<T> |
| 538 | constexpr T* RB_FIND(RBHead<T>& head, T* elm, Compare cmp) { | 538 | constexpr T* RB_FIND(RBHead<T>& head, T* elm, Compare cmp) { |
| 539 | T* tmp = head.Root(); | 539 | T* tmp = head.Root(); |
| 540 | 540 | ||
| @@ -553,7 +553,7 @@ constexpr T* RB_FIND(RBHead<T>& head, T* elm, Compare cmp) { | |||
| 553 | } | 553 | } |
| 554 | 554 | ||
| 555 | template <typename T, typename Compare> | 555 | template <typename T, typename Compare> |
| 556 | requires HasRBEntry<T> | 556 | requires HasRBEntry<T> |
| 557 | constexpr T* RB_NFIND(RBHead<T>& head, T* elm, Compare cmp) { | 557 | constexpr T* RB_NFIND(RBHead<T>& head, T* elm, Compare cmp) { |
| 558 | T* tmp = head.Root(); | 558 | T* tmp = head.Root(); |
| 559 | T* res = nullptr; | 559 | T* res = nullptr; |
| @@ -574,7 +574,7 @@ constexpr T* RB_NFIND(RBHead<T>& head, T* elm, Compare cmp) { | |||
| 574 | } | 574 | } |
| 575 | 575 | ||
| 576 | template <typename T, typename U, typename Compare> | 576 | template <typename T, typename U, typename Compare> |
| 577 | requires HasRBEntry<T> | 577 | requires HasRBEntry<T> |
| 578 | constexpr T* RB_FIND_KEY(RBHead<T>& head, const U& key, Compare cmp) { | 578 | constexpr T* RB_FIND_KEY(RBHead<T>& head, const U& key, Compare cmp) { |
| 579 | T* tmp = head.Root(); | 579 | T* tmp = head.Root(); |
| 580 | 580 | ||
| @@ -593,7 +593,7 @@ constexpr T* RB_FIND_KEY(RBHead<T>& head, const U& key, Compare cmp) { | |||
| 593 | } | 593 | } |
| 594 | 594 | ||
| 595 | template <typename T, typename U, typename Compare> | 595 | template <typename T, typename U, typename Compare> |
| 596 | requires HasRBEntry<T> | 596 | requires HasRBEntry<T> |
| 597 | constexpr T* RB_NFIND_KEY(RBHead<T>& head, const U& key, Compare cmp) { | 597 | constexpr T* RB_NFIND_KEY(RBHead<T>& head, const U& key, Compare cmp) { |
| 598 | T* tmp = head.Root(); | 598 | T* tmp = head.Root(); |
| 599 | T* res = nullptr; | 599 | T* res = nullptr; |
| @@ -614,7 +614,7 @@ constexpr T* RB_NFIND_KEY(RBHead<T>& head, const U& key, Compare cmp) { | |||
| 614 | } | 614 | } |
| 615 | 615 | ||
| 616 | template <typename T, typename Compare> | 616 | template <typename T, typename Compare> |
| 617 | requires HasRBEntry<T> | 617 | requires HasRBEntry<T> |
| 618 | constexpr T* RB_FIND_EXISTING(RBHead<T>& head, T* elm, Compare cmp) { | 618 | constexpr T* RB_FIND_EXISTING(RBHead<T>& head, T* elm, Compare cmp) { |
| 619 | T* tmp = head.Root(); | 619 | T* tmp = head.Root(); |
| 620 | 620 | ||
| @@ -631,7 +631,7 @@ constexpr T* RB_FIND_EXISTING(RBHead<T>& head, T* elm, Compare cmp) { | |||
| 631 | } | 631 | } |
| 632 | 632 | ||
| 633 | template <typename T, typename U, typename Compare> | 633 | template <typename T, typename U, typename Compare> |
| 634 | requires HasRBEntry<T> | 634 | requires HasRBEntry<T> |
| 635 | constexpr T* RB_FIND_EXISTING_KEY(RBHead<T>& head, const U& key, Compare cmp) { | 635 | constexpr T* RB_FIND_EXISTING_KEY(RBHead<T>& head, const U& key, Compare cmp) { |
| 636 | T* tmp = head.Root(); | 636 | T* tmp = head.Root(); |
| 637 | 637 | ||
| @@ -648,7 +648,7 @@ constexpr T* RB_FIND_EXISTING_KEY(RBHead<T>& head, const U& key, Compare cmp) { | |||
| 648 | } | 648 | } |
| 649 | 649 | ||
| 650 | template <typename T> | 650 | template <typename T> |
| 651 | requires HasRBEntry<T> | 651 | requires HasRBEntry<T> |
| 652 | constexpr T* RB_NEXT(T* elm) { | 652 | constexpr T* RB_NEXT(T* elm) { |
| 653 | if (RB_RIGHT(elm)) { | 653 | if (RB_RIGHT(elm)) { |
| 654 | elm = RB_RIGHT(elm); | 654 | elm = RB_RIGHT(elm); |
| @@ -669,7 +669,7 @@ constexpr T* RB_NEXT(T* elm) { | |||
| 669 | } | 669 | } |
| 670 | 670 | ||
| 671 | template <typename T> | 671 | template <typename T> |
| 672 | requires HasRBEntry<T> | 672 | requires HasRBEntry<T> |
| 673 | constexpr T* RB_PREV(T* elm) { | 673 | constexpr T* RB_PREV(T* elm) { |
| 674 | if (RB_LEFT(elm)) { | 674 | if (RB_LEFT(elm)) { |
| 675 | elm = RB_LEFT(elm); | 675 | elm = RB_LEFT(elm); |
| @@ -690,7 +690,7 @@ constexpr T* RB_PREV(T* elm) { | |||
| 690 | } | 690 | } |
| 691 | 691 | ||
| 692 | template <typename T> | 692 | template <typename T> |
| 693 | requires HasRBEntry<T> | 693 | requires HasRBEntry<T> |
| 694 | constexpr T* RB_MIN(RBHead<T>& head) { | 694 | constexpr T* RB_MIN(RBHead<T>& head) { |
| 695 | T* tmp = head.Root(); | 695 | T* tmp = head.Root(); |
| 696 | T* parent = nullptr; | 696 | T* parent = nullptr; |
| @@ -704,7 +704,7 @@ constexpr T* RB_MIN(RBHead<T>& head) { | |||
| 704 | } | 704 | } |
| 705 | 705 | ||
| 706 | template <typename T> | 706 | template <typename T> |
| 707 | requires HasRBEntry<T> | 707 | requires HasRBEntry<T> |
| 708 | constexpr T* RB_MAX(RBHead<T>& head) { | 708 | constexpr T* RB_MAX(RBHead<T>& head) { |
| 709 | T* tmp = head.Root(); | 709 | T* tmp = head.Root(); |
| 710 | T* parent = nullptr; | 710 | T* parent = nullptr; |