summaryrefslogtreecommitdiff
path: root/AK/IntrusiveList.h
blob: fb183a77f9d9831f22c8fee944b9e69c3818190a (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <AK/Assertions.h>

namespace AK {

class IntrusiveListNode;
class IntrusiveListStorage {
private:
    friend class IntrusiveListNode;
    template<class T, IntrusiveListNode T::*member>
    friend class IntrusiveList;
    IntrusiveListNode* m_first { nullptr };
    IntrusiveListNode* m_last { nullptr };
};

template<class T, IntrusiveListNode T::*member>
class IntrusiveList {
public:
    IntrusiveList();
    ~IntrusiveList();

    void clear();
    [[nodiscard]] bool is_empty() const;
    void append(T& n);
    void prepend(T& n);
    void remove(T& n);
    [[nodiscard]] bool contains(const T&) const;
    [[nodiscard]] T* first() const;
    [[nodiscard]] T* last() const;

    [[nodiscard]] T* take_first();
    [[nodiscard]] T* take_last();

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

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

    private:
        T* m_value { nullptr };
    };

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

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

        const T& operator*() const { return *m_value; }
        const T* operator->() const { return m_value; }
        bool operator==(const ConstIterator& other) const { return other.m_value == m_value; }
        bool operator!=(const ConstIterator& other) const { return !(*this == other); }
        ConstIterator& operator++()
        {
            m_value = IntrusiveList<T, member>::next(const_cast<T*>(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* node_to_value(IntrusiveListNode& node);
    IntrusiveListStorage m_storage;
};

class IntrusiveListNode {
public:
    ~IntrusiveListNode();
    void remove();
    bool is_in_list() const;

private:
    template<class T, IntrusiveListNode T::*member>
    friend class IntrusiveList;
    IntrusiveListStorage* m_storage = nullptr;
    IntrusiveListNode* m_next = nullptr;
    IntrusiveListNode* m_prev = nullptr;
};

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

template<class T, IntrusiveListNode T::*member>
inline IntrusiveList<T, member>::IntrusiveList()

{
}

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

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

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

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

    nnode.m_storage = &m_storage;
    nnode.m_prev = m_storage.m_last;
    nnode.m_next = nullptr;

    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, IntrusiveListNode T::*member>
inline void IntrusiveList<T, member>::prepend(T& n)
{
    auto& nnode = n.*member;
    if (nnode.m_storage)
        nnode.remove();

    nnode.m_storage = &m_storage;
    nnode.m_prev = nullptr;
    nnode.m_next = m_storage.m_first;

    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, IntrusiveListNode T::*member>
inline void IntrusiveList<T, member>::remove(T& n)
{
    auto& nnode = n.*member;
    if (nnode.m_storage)
        nnode.remove();
}

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

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

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

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

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

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

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

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

template<class T, IntrusiveListNode T::*member>
inline T* IntrusiveList<T, member>::node_to_value(IntrusiveListNode& node)
{
    return (T*)((char*)&node - ((char*)&(((T*)nullptr)->*member) - (char*)nullptr));
}

inline IntrusiveListNode::~IntrusiveListNode()
{
    if (m_storage)
        remove();
}

inline void IntrusiveListNode::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;
}

inline bool IntrusiveListNode::is_in_list() const
{
    return m_storage != nullptr;
}

}

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