2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-19 11:04:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-04-16 12:00:20 +00:00
|
|
|
#include <AK/BitCast.h>
|
2021-04-16 12:03:24 +00:00
|
|
|
#include <AK/Forward.h>
|
2021-09-07 22:57:49 +00:00
|
|
|
#include <AK/IntrusiveDetails.h>
|
2021-07-17 09:24:02 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-04-16 12:03:24 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-02-14 21:29:06 +00:00
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
namespace AK::Detail {
|
2019-07-19 11:04:42 +00:00
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
template<typename T, typename Container = RawPtr<T>>
|
2019-07-19 11:04:42 +00:00
|
|
|
class IntrusiveListNode;
|
2021-09-09 12:00:59 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
2021-04-20 08:31:27 +00:00
|
|
|
|
|
|
|
template<typename T, typename Container = RawPtr<T>>
|
2021-09-09 12:00:59 +00:00
|
|
|
using SubstitutedIntrusiveListNode = IntrusiveListNode<T, typename Detail::SubstituteIntrusiveContainerType<T, Container>::Type>;
|
2021-04-20 08:31:27 +00:00
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
template<typename T, typename Container>
|
2019-07-19 11:04:42 +00:00
|
|
|
class IntrusiveListStorage {
|
|
|
|
private:
|
2021-09-09 12:00:59 +00:00
|
|
|
friend class IntrusiveListNode<T, Container>;
|
2021-04-20 08:31:27 +00:00
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T_, typename Container_, SubstitutedIntrusiveListNode<T_, Container_> T_::*member>
|
2019-07-19 11:04:42 +00:00
|
|
|
friend class IntrusiveList;
|
2021-04-20 08:31:27 +00:00
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
SubstitutedIntrusiveListNode<T, Container>* m_first { nullptr };
|
|
|
|
SubstitutedIntrusiveListNode<T, Container>* m_last { nullptr };
|
2019-07-19 11:04:42 +00:00
|
|
|
};
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2019-07-19 11:04:42 +00:00
|
|
|
class IntrusiveList {
|
2021-07-17 09:24:02 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(IntrusiveList);
|
|
|
|
AK_MAKE_NONMOVABLE(IntrusiveList);
|
|
|
|
|
2019-07-19 11:04:42 +00:00
|
|
|
public:
|
2021-05-21 23:08:04 +00:00
|
|
|
IntrusiveList() = default;
|
2019-07-19 11:04:42 +00:00
|
|
|
~IntrusiveList();
|
|
|
|
|
|
|
|
void clear();
|
2021-04-11 08:25:58 +00:00
|
|
|
[[nodiscard]] bool is_empty() const;
|
2021-06-06 21:13:10 +00:00
|
|
|
[[nodiscard]] size_t size_slow() const;
|
2019-07-19 11:04:42 +00:00
|
|
|
void append(T& n);
|
|
|
|
void prepend(T& n);
|
2021-06-15 09:33:59 +00:00
|
|
|
void insert_before(T&, T&);
|
2019-07-19 11:04:42 +00:00
|
|
|
void remove(T& n);
|
2022-08-19 18:53:40 +00:00
|
|
|
[[nodiscard]] bool contains(T const&) const;
|
2021-04-16 12:03:24 +00:00
|
|
|
[[nodiscard]] Container first() const;
|
|
|
|
[[nodiscard]] Container last() const;
|
2019-07-19 11:04:42 +00:00
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
[[nodiscard]] Container take_first();
|
|
|
|
[[nodiscard]] Container take_last();
|
2019-12-22 11:23:30 +00:00
|
|
|
|
2019-07-19 11:04:42 +00:00
|
|
|
class Iterator {
|
|
|
|
public:
|
2021-03-11 13:12:33 +00:00
|
|
|
Iterator() = default;
|
|
|
|
Iterator(T* value)
|
2021-04-16 12:03:24 +00:00
|
|
|
: m_value(move(value))
|
2021-03-11 13:12:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
T const& operator*() const { return *m_value; }
|
2021-04-16 12:03:24 +00:00
|
|
|
auto operator->() const { return m_value; }
|
|
|
|
T& operator*() { return *m_value; }
|
|
|
|
auto operator->() { return m_value; }
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(Iterator const& other) const { return other.m_value == m_value; }
|
2021-03-11 13:12:33 +00:00
|
|
|
Iterator& operator++()
|
|
|
|
{
|
2021-04-16 12:03:24 +00:00
|
|
|
m_value = IntrusiveList<T, Container, member>::next(m_value);
|
2021-03-11 13:12:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2019-07-19 11:04:42 +00:00
|
|
|
Iterator& erase();
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_value { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin();
|
2021-03-11 13:12:33 +00:00
|
|
|
Iterator end() { return Iterator {}; }
|
|
|
|
|
2021-06-03 10:14:56 +00:00
|
|
|
class ReverseIterator {
|
|
|
|
public:
|
|
|
|
ReverseIterator() = default;
|
|
|
|
ReverseIterator(T* value)
|
|
|
|
: m_value(move(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
T const& operator*() const { return *m_value; }
|
2021-06-03 10:14:56 +00:00
|
|
|
auto operator->() const { return m_value; }
|
|
|
|
T& operator*() { return *m_value; }
|
|
|
|
auto operator->() { return m_value; }
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(ReverseIterator const& other) const { return other.m_value == m_value; }
|
2021-06-03 10:14:56 +00:00
|
|
|
ReverseIterator& operator++()
|
|
|
|
{
|
|
|
|
m_value = IntrusiveList<T, Container, member>::prev(m_value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_value { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
ReverseIterator rbegin();
|
|
|
|
ReverseIterator rend() { return ReverseIterator {}; }
|
|
|
|
|
2021-03-11 13:12:33 +00:00
|
|
|
class ConstIterator {
|
|
|
|
public:
|
|
|
|
ConstIterator() = default;
|
2022-08-19 18:53:40 +00:00
|
|
|
ConstIterator(T const* value)
|
2021-03-11 13:12:33 +00:00
|
|
|
: m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
T const& operator*() const { return *m_value; }
|
2021-04-16 12:03:24 +00:00
|
|
|
auto operator->() const { return m_value; }
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(ConstIterator const& other) const { return other.m_value == m_value; }
|
2021-03-11 13:12:33 +00:00
|
|
|
ConstIterator& operator++()
|
|
|
|
{
|
2021-04-16 12:03:24 +00:00
|
|
|
m_value = IntrusiveList<T, Container, member>::next(m_value);
|
2021-03-11 13:12:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-08-19 18:53:40 +00:00
|
|
|
T const* m_value { nullptr };
|
2021-03-11 13:12:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ConstIterator begin() const;
|
|
|
|
ConstIterator end() const { return ConstIterator {}; }
|
2019-07-19 11:04:42 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static T* next(T* current);
|
2021-06-03 10:14:56 +00:00
|
|
|
static T* prev(T* current);
|
2022-08-19 18:53:40 +00:00
|
|
|
static T const* next(T const* current);
|
|
|
|
static T const* prev(T const* current);
|
2021-09-09 12:00:59 +00:00
|
|
|
static T* node_to_value(SubstitutedIntrusiveListNode<T, Container>& node);
|
2021-04-16 12:03:24 +00:00
|
|
|
IntrusiveListStorage<T, Container> m_storage;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename Container>
|
2019-07-19 11:04:42 +00:00
|
|
|
class IntrusiveListNode {
|
|
|
|
public:
|
|
|
|
~IntrusiveListNode();
|
|
|
|
void remove();
|
|
|
|
bool is_in_list() const;
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
static constexpr bool IsRaw = IsPointer<Container>;
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
// Note: For some reason, clang does not consider `member` as declared here, and as declared above (`SubstitutedIntrusiveListNode<T, Container> T::*`)
|
2021-04-20 08:31:27 +00:00
|
|
|
// to be of equal types. so for now, just make the members public on clang.
|
2022-10-04 19:04:13 +00:00
|
|
|
#if !defined(AK_COMPILER_CLANG)
|
2019-07-19 11:04:42 +00:00
|
|
|
private:
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T_, typename Container_, SubstitutedIntrusiveListNode<T_, Container_> T_::*member>
|
|
|
|
friend class ::AK::Detail::IntrusiveList;
|
2021-04-20 08:31:27 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
IntrusiveListStorage<T, Container>* m_storage = nullptr;
|
2021-09-09 12:00:59 +00:00
|
|
|
SubstitutedIntrusiveListNode<T, Container>* m_next = nullptr;
|
|
|
|
SubstitutedIntrusiveListNode<T, Container>* m_prev = nullptr;
|
2021-08-19 00:07:05 +00:00
|
|
|
[[no_unique_address]] SelfReferenceIfNeeded<Container, IsRaw> m_self;
|
2019-07-19 11:04:42 +00:00
|
|
|
};
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline typename IntrusiveList<T, Container, member>::Iterator& IntrusiveList<T, Container, member>::Iterator::erase()
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2021-04-16 12:03:24 +00:00
|
|
|
auto old = m_value;
|
|
|
|
m_value = IntrusiveList<T, Container, member>::next(m_value);
|
2019-07-19 11:04:42 +00:00
|
|
|
(old->*member).remove();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline IntrusiveList<T, Container, member>::~IntrusiveList()
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline void IntrusiveList<T, Container, member>::clear()
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
while (m_storage.m_first)
|
|
|
|
m_storage.m_first->remove();
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline bool IntrusiveList<T, Container, member>::is_empty() const
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
return m_storage.m_first == nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-06-06 21:13:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline void IntrusiveList<T, Container, member>::append(T& n)
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2021-04-16 12:03:24 +00:00
|
|
|
remove(n);
|
2019-07-19 11:04:42 +00:00
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
auto& nnode = n.*member;
|
2019-07-19 11:04:42 +00:00
|
|
|
nnode.m_storage = &m_storage;
|
|
|
|
nnode.m_prev = m_storage.m_last;
|
|
|
|
nnode.m_next = nullptr;
|
2021-04-16 12:03:24 +00:00
|
|
|
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.
|
2019-07-19 11:04:42 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline void IntrusiveList<T, Container, member>::prepend(T& n)
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2021-06-15 09:31:58 +00:00
|
|
|
remove(n);
|
2019-07-19 11:04:42 +00:00
|
|
|
|
2021-06-15 09:31:58 +00:00
|
|
|
auto& nnode = n.*member;
|
2019-07-19 11:04:42 +00:00
|
|
|
nnode.m_storage = &m_storage;
|
|
|
|
nnode.m_prev = nullptr;
|
|
|
|
nnode.m_next = m_storage.m_first;
|
2021-04-16 12:03:24 +00:00
|
|
|
if constexpr (!RemoveReference<decltype(nnode)>::IsRaw)
|
|
|
|
nnode.m_self.reference = &n;
|
2019-07-19 11:04:42 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-06-15 09:33:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline void IntrusiveList<T, Container, member>::remove(T& n)
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
auto& nnode = n.*member;
|
|
|
|
if (nnode.m_storage)
|
|
|
|
nnode.remove();
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2022-08-19 18:53:40 +00:00
|
|
|
inline bool IntrusiveList<T, Container, member>::contains(T const& n) const
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
auto& nnode = n.*member;
|
|
|
|
return nnode.m_storage == &m_storage;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline Container IntrusiveList<T, Container, member>::first() const
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
return m_storage.m_first ? node_to_value(*m_storage.m_first) : nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline Container IntrusiveList<T, Container, member>::take_first()
|
2019-12-22 11:23:30 +00:00
|
|
|
{
|
2021-04-20 06:27:26 +00:00
|
|
|
if (Container ptr = first()) {
|
2019-12-22 11:23:30 +00:00
|
|
|
remove(*ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline Container IntrusiveList<T, Container, member>::take_last()
|
2020-11-24 15:37:55 +00:00
|
|
|
{
|
2021-04-20 06:27:26 +00:00
|
|
|
if (Container ptr = last()) {
|
2020-11-24 15:37:55 +00:00
|
|
|
remove(*ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline Container IntrusiveList<T, Container, member>::last() const
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
return m_storage.m_last ? node_to_value(*m_storage.m_last) : nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2022-08-19 18:53:40 +00:00
|
|
|
inline T const* IntrusiveList<T, Container, member>::next(T const* current)
|
2021-04-16 12:03:24 +00:00
|
|
|
{
|
|
|
|
auto& nextnode = (current->*member).m_next;
|
2022-08-19 18:53:40 +00:00
|
|
|
T const* nextstruct = nextnode ? node_to_value(*nextnode) : nullptr;
|
2021-04-16 12:03:24 +00:00
|
|
|
return nextstruct;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2022-08-19 18:53:40 +00:00
|
|
|
inline T const* IntrusiveList<T, Container, member>::prev(T const* current)
|
2021-06-03 10:14:56 +00:00
|
|
|
{
|
|
|
|
auto& prevnode = (current->*member).m_prev;
|
2022-08-19 18:53:40 +00:00
|
|
|
T const* prevstruct = prevnode ? node_to_value(*prevnode) : nullptr;
|
2021-06-03 10:14:56 +00:00
|
|
|
return prevstruct;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline T* IntrusiveList<T, Container, member>::next(T* current)
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
auto& nextnode = (current->*member).m_next;
|
|
|
|
T* nextstruct = nextnode ? node_to_value(*nextnode) : nullptr;
|
|
|
|
return nextstruct;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-06-03 10:14:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline typename IntrusiveList<T, Container, member>::Iterator IntrusiveList<T, Container, member>::begin()
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
return m_storage.m_first ? Iterator(node_to_value(*m_storage.m_first)) : Iterator();
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-06-03 10:14:56 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
inline typename IntrusiveList<T, Container, member>::ConstIterator IntrusiveList<T, Container, member>::begin() const
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2021-03-11 13:12:33 +00:00
|
|
|
return m_storage.m_first ? ConstIterator(node_to_value(*m_storage.m_first)) : ConstIterator();
|
2019-07-19 11:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, typename Container, SubstitutedIntrusiveListNode<T, Container> T::*member>
|
|
|
|
inline T* IntrusiveList<T, Container, member>::node_to_value(SubstitutedIntrusiveListNode<T, Container>& node)
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2023-04-30 15:10:07 +00:00
|
|
|
// Note: A data member pointer is a 32-bit offset in the Windows ABI (both x86 and x86_64),
|
|
|
|
// whereas it is an appropriately sized ptrdiff_t in the Itanium ABI, the following ensures
|
|
|
|
// that we always use the correct type for the subtraction.
|
|
|
|
using EquivalentNumericTypeForDataMemberPointer = Conditional<sizeof(member) == sizeof(ptrdiff_t), ptrdiff_t, u32>;
|
|
|
|
static_assert(sizeof(EquivalentNumericTypeForDataMemberPointer) == sizeof(member),
|
|
|
|
"The equivalent numeric type for the data member pointer must have the same size as the data member pointer itself.");
|
|
|
|
|
2021-04-16 12:00:20 +00:00
|
|
|
// 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.
|
2023-04-30 15:10:07 +00:00
|
|
|
return bit_cast<T*>(bit_cast<unsigned char*>(&node) - bit_cast<EquivalentNumericTypeForDataMemberPointer>(member));
|
2019-07-19 11:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
template<typename T, typename Container>
|
|
|
|
inline IntrusiveListNode<T, Container>::~IntrusiveListNode()
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2021-05-19 21:42:28 +00:00
|
|
|
VERIFY(!is_in_list());
|
2019-07-19 11:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
template<typename T, typename Container>
|
|
|
|
inline void IntrusiveListNode<T, Container>::remove()
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_storage);
|
2019-07-19 11:04:42 +00:00
|
|
|
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;
|
2021-04-16 12:03:24 +00:00
|
|
|
if constexpr (!IsRaw)
|
|
|
|
m_self.reference = nullptr;
|
2019-07-19 11:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
template<typename T, typename Container>
|
|
|
|
inline bool IntrusiveListNode<T, Container>::is_in_list() const
|
2019-07-19 11:04:42 +00:00
|
|
|
{
|
|
|
|
return m_storage != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:31:27 +00:00
|
|
|
// Specialise IntrusiveList for NonnullRefPtr
|
2021-04-16 12:03:24 +00:00
|
|
|
// By default, intrusive lists cannot contain null entries anyway, so switch to RefPtr
|
|
|
|
// and just make the user-facing functions deref the pointers.
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
template<class T, SubstitutedIntrusiveListNode<T, NonnullRefPtr<T>> T::*member>
|
2021-04-16 12:03:24 +00:00
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
2019-07-19 11:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
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>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-07-19 11:04:42 +00:00
|
|
|
using AK::IntrusiveList;
|
|
|
|
using AK::IntrusiveListNode;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|