summaryrefslogtreecommitdiffstats
path: root/src/common/intrusive_list.h
blob: d330dc1c2213f037e726f8de4f1c2a8c8cbd17fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/common_funcs.h"
#include "common/parent_of_member.h"

namespace Common {

// Forward declare implementation class for Node.
namespace impl {

class IntrusiveListImpl;

}

class IntrusiveListNode {
    YUZU_NON_COPYABLE(IntrusiveListNode);

private:
    friend class impl::IntrusiveListImpl;

    IntrusiveListNode* m_prev;
    IntrusiveListNode* m_next;

public:
    constexpr IntrusiveListNode() : m_prev(this), m_next(this) {}

    constexpr bool IsLinked() const {
        return m_next != this;
    }

private:
    constexpr void LinkPrev(IntrusiveListNode* node) {
        // We can't link an already linked node.
        ASSERT(!node->IsLinked());
        this->SplicePrev(node, node);
    }

    constexpr void SplicePrev(IntrusiveListNode* first, IntrusiveListNode* last) {
        // Splice a range into the list.
        auto last_prev = last->m_prev;
        first->m_prev = m_prev;
        last_prev->m_next = this;
        m_prev->m_next = first;
        m_prev = last_prev;
    }

    constexpr void LinkNext(IntrusiveListNode* node) {
        // We can't link an already linked node.
        ASSERT(!node->IsLinked());
        return this->SpliceNext(node, node);
    }

    constexpr void SpliceNext(IntrusiveListNode* first, IntrusiveListNode* last) {
        // Splice a range into the list.
        auto last_prev = last->m_prev;
        first->m_prev = this;
        last_prev->m_next = m_next;
        m_next->m_prev = last_prev;
        m_next = first;
    }

    constexpr void Unlink() {
        this->Unlink(m_next);
    }

    constexpr void Unlink(IntrusiveListNode* last) {
        // Unlink a node from a next node.
        auto last_prev = last->m_prev;
        m_prev->m_next = last;
        last->m_prev = m_prev;
        last_prev->m_next = this;
        m_prev = last_prev;
    }

    constexpr IntrusiveListNode* GetPrev() {
        return m_prev;
    }

    constexpr const IntrusiveListNode* GetPrev() const {
        return m_prev;
    }

    constexpr IntrusiveListNode* GetNext() {
        return m_next;
    }

    constexpr const IntrusiveListNode* GetNext() const {
        return m_next;
    }
};
// DEPRECATED: static_assert(std::is_literal_type<IntrusiveListNode>::value);

namespace impl {

class IntrusiveListImpl {
    YUZU_NON_COPYABLE(IntrusiveListImpl);

private:
    IntrusiveListNode m_root_node;

public:
    template <bool Const>
    class Iterator;

    using value_type = IntrusiveListNode;
    using size_type = size_t;
    using difference_type = ptrdiff_t;
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using reference = value_type&;
    using const_reference = const value_type&;
    using iterator = Iterator<false>;
    using const_iterator = Iterator<true>;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    template <bool Const>
    class Iterator {
    public:
        using iterator_category = std::bidirectional_iterator_tag;
        using value_type = typename IntrusiveListImpl::value_type;
        using difference_type = typename IntrusiveListImpl::difference_type;
        using pointer =
            std::conditional_t<Const, IntrusiveListImpl::const_pointer, IntrusiveListImpl::pointer>;
        using reference = std::conditional_t<Const, IntrusiveListImpl::const_reference,
                                             IntrusiveListImpl::reference>;

    private:
        pointer m_node;

    public:
        constexpr explicit Iterator(pointer n) : m_node(n) {}

        constexpr bool operator==(const Iterator& rhs) const {
            return m_node == rhs.m_node;
        }

        constexpr pointer operator->() const {
            return m_node;
        }

        constexpr reference operator*() const {
            return *m_node;
        }

        constexpr Iterator& operator++() {
            m_node = m_node->m_next;
            return *this;
        }

        constexpr Iterator& operator--() {
            m_node = m_node->m_prev;
            return *this;
        }

        constexpr Iterator operator++(int) {
            const Iterator it{*this};
            ++(*this);
            return it;
        }

        constexpr Iterator operator--(int) {
            const Iterator it{*this};
            --(*this);
            return it;
        }

        constexpr operator Iterator<true>() const {
            return Iterator<true>(m_node);
        }

        constexpr Iterator<false> GetNonConstIterator() const {
            return Iterator<false>(const_cast<IntrusiveListImpl::pointer>(m_node));
        }
    };

public:
    constexpr IntrusiveListImpl() : m_root_node() {}

    // Iterator accessors.
    constexpr iterator begin() {
        return iterator(m_root_node.GetNext());
    }

    constexpr const_iterator begin() const {
        return const_iterator(m_root_node.GetNext());
    }

    constexpr iterator end() {
        return iterator(std::addressof(m_root_node));
    }

    constexpr const_iterator end() const {
        return const_iterator(std::addressof(m_root_node));
    }

    constexpr iterator iterator_to(reference v) {
        // Only allow iterator_to for values in lists.
        ASSERT(v.IsLinked());
        return iterator(std::addressof(v));
    }

    constexpr const_iterator iterator_to(const_reference v) const {
        // Only allow iterator_to for values in lists.
        ASSERT(v.IsLinked());
        return const_iterator(std::addressof(v));
    }

    // Content management.
    constexpr bool empty() const {
        return !m_root_node.IsLinked();
    }

    constexpr size_type size() const {
        return static_cast<size_type>(std::distance(this->begin(), this->end()));
    }

    constexpr reference back() {
        return *m_root_node.GetPrev();
    }

    constexpr const_reference back() const {
        return *m_root_node.GetPrev();
    }

    constexpr reference front() {
        return *m_root_node.GetNext();
    }

    constexpr const_reference front() const {
        return *m_root_node.GetNext();
    }

    constexpr void push_back(reference node) {
        m_root_node.LinkPrev(std::addressof(node));
    }

    constexpr void push_front(reference node) {
        m_root_node.LinkNext(std::addressof(node));
    }

    constexpr void pop_back() {
        m_root_node.GetPrev()->Unlink();
    }

    constexpr void pop_front() {
        m_root_node.GetNext()->Unlink();
    }

    constexpr iterator insert(const_iterator pos, reference node) {
        pos.GetNonConstIterator()->LinkPrev(std::addressof(node));
        return iterator(std::addressof(node));
    }

    constexpr void splice(const_iterator pos, IntrusiveListImpl& o) {
        splice_impl(pos, o.begin(), o.end());
    }

    constexpr void splice(const_iterator pos, IntrusiveListImpl& o, const_iterator first) {
        const_iterator last(first);
        std::advance(last, 1);
        splice_impl(pos, first, last);
    }

    constexpr void splice(const_iterator pos, IntrusiveListImpl& o, const_iterator first,
                          const_iterator last) {
        splice_impl(pos, first, last);
    }

    constexpr iterator erase(const_iterator pos) {
        if (pos == this->end()) {
            return this->end();
        }
        iterator it(pos.GetNonConstIterator());
        (it++)->Unlink();
        return it;
    }

    constexpr void clear() {
        while (!this->empty()) {
            this->pop_front();
        }
    }

private:
    constexpr void splice_impl(const_iterator _pos, const_iterator _first, const_iterator _last) {
        if (_first == _last) {
            return;
        }
        iterator pos(_pos.GetNonConstIterator());
        iterator first(_first.GetNonConstIterator());
        iterator last(_last.GetNonConstIterator());
        first->Unlink(std::addressof(*last));
        pos->SplicePrev(std::addressof(*first), std::addressof(*first));
    }
};

} // namespace impl

template <class T, class Traits>
class IntrusiveList {
    YUZU_NON_COPYABLE(IntrusiveList);

private:
    impl::IntrusiveListImpl m_impl;

public:
    template <bool Const>
    class Iterator;

    using value_type = T;
    using size_type = size_t;
    using difference_type = ptrdiff_t;
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using reference = value_type&;
    using const_reference = const value_type&;
    using iterator = Iterator<false>;
    using const_iterator = Iterator<true>;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    template <bool Const>
    class Iterator {
    public:
        friend class Common::IntrusiveList<T, Traits>;

        using ImplIterator =
            std::conditional_t<Const, Common::impl::IntrusiveListImpl::const_iterator,
                               Common::impl::IntrusiveListImpl::iterator>;

        using iterator_category = std::bidirectional_iterator_tag;
        using value_type = typename IntrusiveList::value_type;
        using difference_type = typename IntrusiveList::difference_type;
        using pointer =
            std::conditional_t<Const, IntrusiveList::const_pointer, IntrusiveList::pointer>;
        using reference =
            std::conditional_t<Const, IntrusiveList::const_reference, IntrusiveList::reference>;

    private:
        ImplIterator m_iterator;

    private:
        constexpr explicit Iterator(ImplIterator it) : m_iterator(it) {}

        constexpr ImplIterator GetImplIterator() const {
            return m_iterator;
        }

    public:
        constexpr bool operator==(const Iterator& rhs) const {
            return m_iterator == rhs.m_iterator;
        }

        constexpr pointer operator->() const {
            return std::addressof(Traits::GetParent(*m_iterator));
        }

        constexpr reference operator*() const {
            return Traits::GetParent(*m_iterator);
        }

        constexpr Iterator& operator++() {
            ++m_iterator;
            return *this;
        }

        constexpr Iterator& operator--() {
            --m_iterator;
            return *this;
        }

        constexpr Iterator operator++(int) {
            const Iterator it{*this};
            ++m_iterator;
            return it;
        }

        constexpr Iterator operator--(int) {
            const Iterator it{*this};
            --m_iterator;
            return it;
        }

        constexpr operator Iterator<true>() const {
            return Iterator<true>(m_iterator);
        }
    };

private:
    static constexpr IntrusiveListNode& GetNode(reference ref) {
        return Traits::GetNode(ref);
    }

    static constexpr IntrusiveListNode const& GetNode(const_reference ref) {
        return Traits::GetNode(ref);
    }

    static constexpr reference GetParent(IntrusiveListNode& node) {
        return Traits::GetParent(node);
    }

    static constexpr const_reference GetParent(IntrusiveListNode const& node) {
        return Traits::GetParent(node);
    }

public:
    constexpr IntrusiveList() : m_impl() {}

    // Iterator accessors.
    constexpr iterator begin() {
        return iterator(m_impl.begin());
    }

    constexpr const_iterator begin() const {
        return const_iterator(m_impl.begin());
    }

    constexpr iterator end() {
        return iterator(m_impl.end());
    }

    constexpr const_iterator end() const {
        return const_iterator(m_impl.end());
    }

    constexpr const_iterator cbegin() const {
        return this->begin();
    }

    constexpr const_iterator cend() const {
        return this->end();
    }

    constexpr reverse_iterator rbegin() {
        return reverse_iterator(this->end());
    }

    constexpr const_reverse_iterator rbegin() const {
        return const_reverse_iterator(this->end());
    }

    constexpr reverse_iterator rend() {
        return reverse_iterator(this->begin());
    }

    constexpr const_reverse_iterator rend() const {
        return const_reverse_iterator(this->begin());
    }

    constexpr const_reverse_iterator crbegin() const {
        return this->rbegin();
    }

    constexpr const_reverse_iterator crend() const {
        return this->rend();
    }

    constexpr iterator iterator_to(reference v) {
        return iterator(m_impl.iterator_to(GetNode(v)));
    }

    constexpr const_iterator iterator_to(const_reference v) const {
        return const_iterator(m_impl.iterator_to(GetNode(v)));
    }

    // Content management.
    constexpr bool empty() const {
        return m_impl.empty();
    }

    constexpr size_type size() const {
        return m_impl.size();
    }

    constexpr reference back() {
        return GetParent(m_impl.back());
    }

    constexpr const_reference back() const {
        return GetParent(m_impl.back());
    }

    constexpr reference front() {
        return GetParent(m_impl.front());
    }

    constexpr const_reference front() const {
        return GetParent(m_impl.front());
    }

    constexpr void push_back(reference ref) {
        m_impl.push_back(GetNode(ref));
    }

    constexpr void push_front(reference ref) {
        m_impl.push_front(GetNode(ref));
    }

    constexpr void pop_back() {
        m_impl.pop_back();
    }

    constexpr void pop_front() {
        m_impl.pop_front();
    }

    constexpr iterator insert(const_iterator pos, reference ref) {
        return iterator(m_impl.insert(pos.GetImplIterator(), GetNode(ref)));
    }

    constexpr void splice(const_iterator pos, IntrusiveList& o) {
        m_impl.splice(pos.GetImplIterator(), o.m_impl);
    }

    constexpr void splice(const_iterator pos, IntrusiveList& o, const_iterator first) {
        m_impl.splice(pos.GetImplIterator(), o.m_impl, first.GetImplIterator());
    }

    constexpr void splice(const_iterator pos, IntrusiveList& o, const_iterator first,
                          const_iterator last) {
        m_impl.splice(pos.GetImplIterator(), o.m_impl, first.GetImplIterator(),
                      last.GetImplIterator());
    }

    constexpr iterator erase(const_iterator pos) {
        return iterator(m_impl.erase(pos.GetImplIterator()));
    }

    constexpr void clear() {
        m_impl.clear();
    }
};

template <auto T, class Derived = Common::impl::GetParentType<T>>
class IntrusiveListMemberTraits;

template <class Parent, IntrusiveListNode Parent::*Member, class Derived>
class IntrusiveListMemberTraits<Member, Derived> {
public:
    using ListType = IntrusiveList<Derived, IntrusiveListMemberTraits>;

private:
    friend class IntrusiveList<Derived, IntrusiveListMemberTraits>;

    static constexpr IntrusiveListNode& GetNode(Derived& parent) {
        return parent.*Member;
    }

    static constexpr IntrusiveListNode const& GetNode(Derived const& parent) {
        return parent.*Member;
    }

    static Derived& GetParent(IntrusiveListNode& node) {
        return Common::GetParentReference<Member, Derived>(std::addressof(node));
    }

    static Derived const& GetParent(IntrusiveListNode const& node) {
        return Common::GetParentReference<Member, Derived>(std::addressof(node));
    }
};

template <auto T, class Derived = Common::impl::GetParentType<T>>
class IntrusiveListMemberTraitsByNonConstexprOffsetOf;

template <class Parent, IntrusiveListNode Parent::*Member, class Derived>
class IntrusiveListMemberTraitsByNonConstexprOffsetOf<Member, Derived> {
public:
    using ListType = IntrusiveList<Derived, IntrusiveListMemberTraitsByNonConstexprOffsetOf>;

private:
    friend class IntrusiveList<Derived, IntrusiveListMemberTraitsByNonConstexprOffsetOf>;

    static constexpr IntrusiveListNode& GetNode(Derived& parent) {
        return parent.*Member;
    }

    static constexpr IntrusiveListNode const& GetNode(Derived const& parent) {
        return parent.*Member;
    }

    static Derived& GetParent(IntrusiveListNode& node) {
        return *reinterpret_cast<Derived*>(reinterpret_cast<char*>(std::addressof(node)) -
                                           GetOffset());
    }

    static Derived const& GetParent(IntrusiveListNode const& node) {
        return *reinterpret_cast<const Derived*>(
            reinterpret_cast<const char*>(std::addressof(node)) - GetOffset());
    }

    static uintptr_t GetOffset() {
        return reinterpret_cast<uintptr_t>(std::addressof(reinterpret_cast<Derived*>(0)->*Member));
    }
};

template <class Derived>
class IntrusiveListBaseNode : public IntrusiveListNode {};

template <class Derived>
class IntrusiveListBaseTraits {
public:
    using ListType = IntrusiveList<Derived, IntrusiveListBaseTraits>;

private:
    friend class IntrusiveList<Derived, IntrusiveListBaseTraits>;

    static constexpr IntrusiveListNode& GetNode(Derived& parent) {
        return static_cast<IntrusiveListNode&>(
            static_cast<IntrusiveListBaseNode<Derived>&>(parent));
    }

    static constexpr IntrusiveListNode const& GetNode(Derived const& parent) {
        return static_cast<const IntrusiveListNode&>(
            static_cast<const IntrusiveListBaseNode<Derived>&>(parent));
    }

    static constexpr Derived& GetParent(IntrusiveListNode& node) {
        return static_cast<Derived&>(static_cast<IntrusiveListBaseNode<Derived>&>(node));
    }

    static constexpr Derived const& GetParent(IntrusiveListNode const& node) {
        return static_cast<const Derived&>(
            static_cast<const IntrusiveListBaseNode<Derived>&>(node));
    }
};

} // namespace Common