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