2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-13 11:50:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
#include <AK/Assertions.h>
|
2022-11-01 09:17:26 +00:00
|
|
|
#include <AK/Error.h>
|
2020-12-24 18:23:12 +00:00
|
|
|
#include <AK/Find.h>
|
2019-06-29 17:14:03 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-13 11:50:44 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-27 13:57:49 +00:00
|
|
|
template<typename ListType, typename ElementType>
|
|
|
|
class DoublyLinkedListIterator {
|
|
|
|
public:
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator!=(DoublyLinkedListIterator const& other) const { return m_node != other.m_node; }
|
|
|
|
bool operator==(DoublyLinkedListIterator const& other) const { return m_node == other.m_node; }
|
2019-06-27 13:57:49 +00:00
|
|
|
DoublyLinkedListIterator& operator++()
|
|
|
|
{
|
|
|
|
m_node = m_node->next;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ElementType& operator*() { return m_node->value; }
|
|
|
|
ElementType* operator->() { return &m_node->value; }
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] bool is_end() const { return !m_node; }
|
2019-06-27 13:57:49 +00:00
|
|
|
static DoublyLinkedListIterator universal_end() { return DoublyLinkedListIterator(nullptr); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend ListType;
|
|
|
|
explicit DoublyLinkedListIterator(typename ListType::Node* node)
|
|
|
|
: m_node(node)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
typename ListType::Node* m_node;
|
|
|
|
};
|
|
|
|
|
2018-10-13 11:50:44 +00:00
|
|
|
template<typename T>
|
|
|
|
class DoublyLinkedList {
|
|
|
|
private:
|
|
|
|
struct Node {
|
2020-12-24 18:23:12 +00:00
|
|
|
template<typename U>
|
|
|
|
explicit Node(U&& v)
|
|
|
|
: value(forward<U>(v))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-01-10 18:27:41 +00:00
|
|
|
static_assert(
|
|
|
|
requires { T(v); }, "Conversion operator is missing.");
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2018-10-13 11:50:44 +00:00
|
|
|
T value;
|
|
|
|
Node* next { nullptr };
|
|
|
|
Node* prev { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
DoublyLinkedList() = default;
|
2018-10-13 11:50:44 +00:00
|
|
|
~DoublyLinkedList() { clear(); }
|
|
|
|
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] bool is_empty() const { return !m_head; }
|
2018-10-13 11:50:44 +00:00
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
for (auto* node = m_head; node;) {
|
2018-10-13 11:50:44 +00:00
|
|
|
auto* next = node->next;
|
|
|
|
delete node;
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
m_head = nullptr;
|
|
|
|
m_tail = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] T& first()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_head);
|
2020-12-24 18:23:12 +00:00
|
|
|
return m_head->value;
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] const T& first() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_head);
|
2020-12-24 18:23:12 +00:00
|
|
|
return m_head->value;
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] T& last()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_head);
|
2020-12-24 18:23:12 +00:00
|
|
|
return m_tail->value;
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] const T& last() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_head);
|
2020-12-24 18:23:12 +00:00
|
|
|
return m_tail->value;
|
2018-10-13 11:50:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 18:23:12 +00:00
|
|
|
template<typename U>
|
2022-11-01 09:17:26 +00:00
|
|
|
ErrorOr<void> try_append(U&& value)
|
2019-12-02 17:36:10 +00:00
|
|
|
{
|
2021-01-10 18:27:41 +00:00
|
|
|
static_assert(
|
|
|
|
requires { T(value); }, "Conversion operator is missing.");
|
2022-11-01 09:17:26 +00:00
|
|
|
auto* node = new (nothrow) Node(forward<U>(value));
|
|
|
|
if (!node)
|
|
|
|
return Error::from_errno(ENOMEM);
|
2020-12-24 18:23:12 +00:00
|
|
|
if (!m_head) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_tail);
|
2020-12-24 18:23:12 +00:00
|
|
|
m_head = node;
|
|
|
|
m_tail = node;
|
2022-11-01 09:17:26 +00:00
|
|
|
return {};
|
2020-12-24 18:23:12 +00:00
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_tail);
|
|
|
|
VERIFY(!node->next);
|
2020-12-24 18:23:12 +00:00
|
|
|
m_tail->next = node;
|
|
|
|
node->prev = m_tail;
|
|
|
|
m_tail = node;
|
2022-11-01 09:17:26 +00:00
|
|
|
return {};
|
2019-12-02 17:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 18:23:12 +00:00
|
|
|
template<typename U>
|
2022-11-01 09:17:26 +00:00
|
|
|
ErrorOr<void> try_prepend(U&& value)
|
2019-12-02 17:36:10 +00:00
|
|
|
{
|
2021-04-10 13:59:06 +00:00
|
|
|
static_assert(IsSame<T, U>);
|
2022-11-01 09:17:26 +00:00
|
|
|
auto* node = new (nothrow) Node(forward<U>(value));
|
|
|
|
if (!node)
|
|
|
|
return Error::from_errno(ENOMEM);
|
2020-12-24 18:23:12 +00:00
|
|
|
if (!m_head) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_tail);
|
2020-12-24 18:23:12 +00:00
|
|
|
m_head = node;
|
|
|
|
m_tail = node;
|
2022-11-01 09:17:26 +00:00
|
|
|
return {};
|
2020-12-24 18:23:12 +00:00
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_tail);
|
|
|
|
VERIFY(!node->prev);
|
2020-12-24 18:23:12 +00:00
|
|
|
m_head->prev = node;
|
|
|
|
node->next = m_head;
|
|
|
|
m_head = node;
|
2022-11-01 09:17:26 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef KERNEL
|
|
|
|
template<typename U>
|
|
|
|
void append(U&& value)
|
|
|
|
{
|
|
|
|
MUST(try_append(forward<U>(value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
void prepend(U&& value)
|
|
|
|
{
|
|
|
|
MUST(try_prepend(forward<U>(value)));
|
2019-12-02 17:36:10 +00:00
|
|
|
}
|
2022-11-01 09:17:26 +00:00
|
|
|
#endif
|
2019-12-02 17:36:10 +00:00
|
|
|
|
2021-04-11 08:22:35 +00:00
|
|
|
[[nodiscard]] bool contains_slow(const T& value) const
|
2018-10-13 11:50:44 +00:00
|
|
|
{
|
2020-12-24 18:23:12 +00:00
|
|
|
return find(value) != end();
|
2018-10-13 11:50:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:57:49 +00:00
|
|
|
using Iterator = DoublyLinkedListIterator<DoublyLinkedList, T>;
|
|
|
|
friend Iterator;
|
2018-10-13 11:50:44 +00:00
|
|
|
Iterator begin() { return Iterator(m_head); }
|
2019-01-19 21:53:05 +00:00
|
|
|
Iterator end() { return Iterator::universal_end(); }
|
2018-10-13 11:50:44 +00:00
|
|
|
|
2019-06-27 13:57:49 +00:00
|
|
|
using ConstIterator = DoublyLinkedListIterator<const DoublyLinkedList, const T>;
|
|
|
|
friend ConstIterator;
|
2018-10-13 11:50:44 +00:00
|
|
|
ConstIterator begin() const { return ConstIterator(m_head); }
|
2019-01-19 21:53:05 +00:00
|
|
|
ConstIterator end() const { return ConstIterator::universal_end(); }
|
2018-10-13 11:50:44 +00:00
|
|
|
|
|
|
|
ConstIterator find(const T& value) const
|
|
|
|
{
|
2020-12-24 18:23:12 +00:00
|
|
|
return AK::find(begin(), end(), value);
|
2018-10-13 11:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator find(const T& value)
|
|
|
|
{
|
2020-12-24 18:23:12 +00:00
|
|
|
return AK::find(begin(), end(), value);
|
2018-10-13 11:50:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 17:36:10 +00:00
|
|
|
void remove(Iterator it)
|
2018-10-13 11:50:44 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it.m_node);
|
2018-10-13 11:50:44 +00:00
|
|
|
auto* node = it.m_node;
|
2018-10-14 20:08:36 +00:00
|
|
|
if (node->prev) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(node != m_head);
|
2018-10-13 11:50:44 +00:00
|
|
|
node->prev->next = node->next;
|
2018-10-14 20:08:36 +00:00
|
|
|
} else {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(node == m_head);
|
2018-10-13 11:50:44 +00:00
|
|
|
m_head = node->next;
|
2018-10-14 20:08:36 +00:00
|
|
|
}
|
|
|
|
if (node->next) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(node != m_tail);
|
2018-10-14 20:08:36 +00:00
|
|
|
node->next->prev = node->prev;
|
|
|
|
} else {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(node == m_tail);
|
2018-10-13 11:50:44 +00:00
|
|
|
m_tail = node->prev;
|
2018-10-14 20:08:36 +00:00
|
|
|
}
|
2018-10-13 11:50:44 +00:00
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Node* m_head { nullptr };
|
|
|
|
Node* m_tail { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::DoublyLinkedList;
|