summaryrefslogtreecommitdiff
path: root/AK/IntrusiveList.h
blob: f946a5f527a8a7822aeddd6399fa3340fb90c1cd (plain)
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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Assertions.h>
#include <AK/BitCast.h>
#include <AK/Forward.h>
#include <AK/IntrusiveDetails.h>
#include <AK/Noncopyable.h>
#include <AK/StdLibExtras.h>

namespace AK::Detail {

template<typename T, typename Container = RawPtr<T>>
class IntrusiveListNode;

struct ExtractIntrusiveListTypes {
    template<typename V, typename Container, typename T>
    static V value(IntrusiveListNode<V, Container> T::*x);
    template<typename V, typename Container, typename T>
    static Container container(IntrusiveListNode<V, Container> T::*x);
};

template<typename T, typename Container = RawPtr<T>>
using SubstitutedIntrusiveListNode = IntrusiveListNode<T, typename Detail::SubstituteIntrusiveContainerType<T, Container>::Type>;

template<typename T, typename Container>
class IntrusiveListStorage {
private:
    friend class IntrusiveListNode<T, Container>;

    template<class T_, typename Container_, SubstitutedIntrusiveListNode<T_, Container_> T_::*member>
    friend class IntrusiveList;

    SubstitutedIntrusiveListNode<T, Container>* m_first { nullptr };
    SubstitutedIntrusiveListNode<T, Container>* m_last { nullptr };
};

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
class IntrusiveList {
    AK_MAKE_NONCOPYABLE(IntrusiveList);
    AK_MAKE_NONMOVABLE(IntrusiveList);

public:
    IntrusiveList() = default;
    ~IntrusiveList();

    void clear();
    [[nodiscard]] bool is_empty() const;
    [[nodiscard]] size_t size_slow() const;
    void append(T& n);
    void prepend(T& n);
    void insert_before(T&, T&);
    void remove(T& n);
    [[nodiscard]] bool contains(const T&) const;
    [[nodiscard]] Container first() const;
    [[nodiscard]] Container last() const;

    [[nodiscard]] Container take_first();
    [[nodiscard]] Container take_last();

    class Iterator {
    public:
        Iterator() = default;
        Iterator(T* value)
            : m_value(move(value))
        {
        }

        const T& operator*() const { return *m_value; }
        auto operator->() const { return m_value; }
        T& operator*() { return *m_value; }
        auto operator->() { return m_value; }
        bool operator==(Iterator const& other) const { return other.m_value == m_value; }
        bool operator!=(Iterator const& other) const { return !(*this == other); }
        Iterator& operator++()
        {
            m_value = IntrusiveList<T, Container, member>::next(m_value);
            return *this;
        }
        Iterator& erase();

    private:
        T* m_value { nullptr };
    };

    Iterator begin();
    Iterator end() { return Iterator {}; }

    class ReverseIterator {
    public:
        ReverseIterator() = default;
        ReverseIterator(T* value)
            : m_value(move(value))
        {
        }

        const T& operator*() const { return *m_value; }
        auto operator->() const { return m_value; }
        T& operator*() { return *m_value; }
        auto operator->() { return m_value; }
        bool operator==(ReverseIterator const& other) const { return other.m_value == m_value; }
        bool operator!=(ReverseIterator const& other) const { return !(*this == other); }
        ReverseIterator& operator++()
        {
            m_value = IntrusiveList<T, Container, member>::prev(m_value);
            return *this;
        }
        ReverseIterator& erase();

    private:
        T* m_value { nullptr };
    };

    ReverseIterator rbegin();
    ReverseIterator rend() { return ReverseIterator {}; }

    class ConstIterator {
    public:
        ConstIterator() = default;
        ConstIterator(const T* value)
            : m_value(value)
        {
        }

        const T& operator*() const { return *m_value; }
        auto operator->() const { return m_value; }
        bool operator==(ConstIterator const& other) const { return other.m_value == m_value; }
        bool operator!=(ConstIterator const& other) const { return !(*this == other); }
        ConstIterator& operator++()
        {
            m_value = IntrusiveList<T, Container, member>::next(m_value);
            return *this;
        }

    private:
        const T* m_value { nullptr };
    };

    ConstIterator begin() const;
    ConstIterator end() const { return ConstIterator {}; }

private:
    static T* next(T* current);
    static T* prev(T* current);
    static const T* next(const T* current);
    static const T* prev(const T* current);
    static T* node_to_value(SubstitutedIntrusiveListNode<T, Container>& node);
    IntrusiveListStorage<T, Container> m_storage;
};

template<typename T, typename Container>
class IntrusiveListNode {
public:
    ~IntrusiveListNode();
    void remove();
    bool is_in_list() const;

    static constexpr bool IsRaw = IsPointer<Container>;

    // Note: For some reason, clang does not consider `member` as declared here, and as declared above (`SubstitutedIntrusiveListNode<T, Container> T::*`)
    //       to be of equal types. so for now, just make the members public on clang.
#ifndef __clang__
private:
    template<class T_, typename Container_, SubstitutedIntrusiveListNode<T_, Container_> T_::*member>
    friend class ::AK::Detail::IntrusiveList;
#endif

    IntrusiveListStorage<T, Container>* m_storage = nullptr;
    SubstitutedIntrusiveListNode<T, Container>* m_next = nullptr;
    SubstitutedIntrusiveListNode<T, Container>* m_prev = nullptr;
    [[no_unique_address]] SelfReferenceIfNeeded<Container, IsRaw> m_self;
};

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline typename IntrusiveList<T, Container, member>::Iterator& IntrusiveList<T, Container, member>::Iterator::erase()
{
    auto old = m_value;
    m_value = IntrusiveList<T, Container, member>::next(m_value);
    (old->*member).remove();
    return *this;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline IntrusiveList<T, Container, member>::~IntrusiveList()
{
    clear();
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline void IntrusiveList<T, Container, member>::clear()
{
    while (m_storage.m_first)
        m_storage.m_first->remove();
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline bool IntrusiveList<T, Container, member>::is_empty() const
{
    return m_storage.m_first == nullptr;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline size_t IntrusiveList<T, Container, member>::size_slow() const
{
    size_t size = 0;
    auto it_end = end();
    for (auto it = begin(); it != it_end; ++it) {
        ++size;
    }
    return size;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline void IntrusiveList<T, Container, member>::append(T& n)
{
    remove(n);

    auto& nnode = n.*member;
    nnode.m_storage = &m_storage;
    nnode.m_prev = m_storage.m_last;
    nnode.m_next = nullptr;
    if constexpr (!RemoveReference<decltype(nnode)>::IsRaw)
        nnode.m_self.reference = &n; // Note: Self-reference ensures that the object will keep a ref to itself when the Container is a smart pointer.

    if (m_storage.m_last)
        m_storage.m_last->m_next = &nnode;
    m_storage.m_last = &nnode;
    if (!m_storage.m_first)
        m_storage.m_first = &nnode;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline void IntrusiveList<T, Container, member>::prepend(T& n)
{
    remove(n);

    auto& nnode = n.*member;
    nnode.m_storage = &m_storage;
    nnode.m_prev = nullptr;
    nnode.m_next = m_storage.m_first;
    if constexpr (!RemoveReference<decltype(nnode)>::IsRaw)
        nnode.m_self.reference = &n;

    if (m_storage.m_first)
        m_storage.m_first->m_prev = &nnode;
    m_storage.m_first = &nnode;
    if (!m_storage.m_last)
        m_storage.m_last = &nnode;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline void IntrusiveList<T, Container, member>::insert_before(T& bn, T& n)
{
    remove(n);

    auto& new_node = n.*member;
    auto& before_node = bn.*member;
    new_node.m_storage = &m_storage;
    new_node.m_next = &before_node;
    new_node.m_prev = before_node.m_prev;
    if (before_node.m_prev)
        before_node.m_prev->m_next = &new_node;
    before_node.m_prev = &new_node;

    if (m_storage.m_first == &before_node) {
        m_storage.m_first = &new_node;
    }

    if constexpr (!RemoveReference<decltype(new_node)>::IsRaw)
        new_node.m_self.reference = &n;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline void IntrusiveList<T, Container, member>::remove(T& n)
{
    auto& nnode = n.*member;
    if (nnode.m_storage)
        nnode.remove();
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline bool IntrusiveList<T, Container, member>::contains(const T& n) const
{
    auto& nnode = n.*member;
    return nnode.m_storage == &m_storage;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline Container IntrusiveList<T, Container, member>::first() const
{
    return m_storage.m_first ? node_to_value(*m_storage.m_first) : nullptr;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline Container IntrusiveList<T, Container, member>::take_first()
{
    if (Container ptr = first()) {
        remove(*ptr);
        return ptr;
    }
    return nullptr;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline Container IntrusiveList<T, Container, member>::take_last()
{
    if (Container ptr = last()) {
        remove(*ptr);
        return ptr;
    }
    return nullptr;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline Container IntrusiveList<T, Container, member>::last() const
{
    return m_storage.m_last ? node_to_value(*m_storage.m_last) : nullptr;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline const T* IntrusiveList<T, Container, member>::next(const T* current)
{
    auto& nextnode = (current->*member).m_next;
    const T* nextstruct = nextnode ? node_to_value(*nextnode) : nullptr;
    return nextstruct;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline const T* IntrusiveList<T, Container, member>::prev(const T* current)
{
    auto& prevnode = (current->*member).m_prev;
    const T* prevstruct = prevnode ? node_to_value(*prevnode) : nullptr;
    return prevstruct;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline T* IntrusiveList<T, Container, member>::next(T* current)
{
    auto& nextnode = (current->*member).m_next;
    T* nextstruct = nextnode ? node_to_value(*nextnode) : nullptr;
    return nextstruct;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline T* IntrusiveList<T, Container, member>::prev(T* current)
{
    auto& prevnode = (current->*member).m_prev;
    T* prevstruct = prevnode ? node_to_value(*prevnode) : nullptr;
    return prevstruct;
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline typename IntrusiveList<T, Container, member>::Iterator IntrusiveList<T, Container, member>::begin()
{
    return m_storage.m_first ? Iterator(node_to_value(*m_storage.m_first)) : Iterator();
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline typename IntrusiveList<T, Container, member>::ReverseIterator IntrusiveList<T, Container, member>::rbegin()
{
    return m_storage.m_last ? ReverseIterator(node_to_value(*m_storage.m_last)) : ReverseIterator();
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline typename IntrusiveList<T, Container, member>::ConstIterator IntrusiveList<T, Container, member>::begin() const
{
    return m_storage.m_first ? ConstIterator(node_to_value(*m_storage.m_first)) : ConstIterator();
}

template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
inline T* IntrusiveList<T, Container, member>::node_to_value(SubstitutedIntrusiveListNode<T, Container>& node)
{
    // Note: Since this might seem odd, here's an explanation on what this function actually does:
    //       `node` is a reference that resides in some part of the actual value (of type T), the
    //       placement (i.e. offset) of which is described by the pointer-to-data-member parameter
    //       named `member`.
    //       This function effectively takes in the address of the data member, and returns the address
    //       of the value (of type T) holding that member.
    return bit_cast<T*>(bit_cast<unsigned char*>(&node) - bit_cast<unsigned char*>(member));
}

template<typename T, typename Container>
inline IntrusiveListNode<T, Container>::~IntrusiveListNode()
{
    VERIFY(!is_in_list());
}

template<typename T, typename Container>
inline void IntrusiveListNode<T, Container>::remove()
{
    VERIFY(m_storage);
    if (m_storage->m_first == this)
        m_storage->m_first = m_next;
    if (m_storage->m_last == this)
        m_storage->m_last = m_prev;
    if (m_prev)
        m_prev->m_next = m_next;
    if (m_next)
        m_next->m_prev = m_prev;
    m_prev = nullptr;
    m_next = nullptr;
    m_storage = nullptr;
    if constexpr (!IsRaw)
        m_self.reference = nullptr;
}

template<typename T, typename Container>
inline bool IntrusiveListNode<T, Container>::is_in_list() const
{
    return m_storage != nullptr;
}

// Specialise IntrusiveList for NonnullRefPtr
// By default, intrusive lists cannot contain null entries anyway, so switch to RefPtr
// and just make the user-facing functions deref the pointers.

template<class T, SubstitutedIntrusiveListNode<T, NonnullRefPtr<T>> T::*member>
class IntrusiveList<T, NonnullRefPtr<T>, member> : public IntrusiveList<T, RefPtr<T>, member> {
public:
    [[nodiscard]] NonnullRefPtr<T> first() const { return *IntrusiveList<T, RefPtr<T>, member>::first(); }
    [[nodiscard]] NonnullRefPtr<T> last() const { return *IntrusiveList<T, RefPtr<T>, member>::last(); }

    [[nodiscard]] NonnullRefPtr<T> take_first() { return *IntrusiveList<T, RefPtr<T>, member>::take_first(); }
    [[nodiscard]] NonnullRefPtr<T> take_last() { return *IntrusiveList<T, RefPtr<T>, member>::take_last(); }
};

}

namespace AK {

template<typename T, typename Container = RawPtr<T>>
using IntrusiveListNode = Detail::SubstitutedIntrusiveListNode<T, Container>;

template<auto member>
using IntrusiveList = Detail::IntrusiveList<
    decltype(Detail::ExtractIntrusiveListTypes::value(member)),
    decltype(Detail::ExtractIntrusiveListTypes::container(member)),
    member>;

}

using AK::IntrusiveList;
using AK::IntrusiveListNode;