banan-os/BAN/include/BAN/LinkedList.h

399 lines
8.9 KiB
C
Raw Normal View History

2023-02-01 01:22:41 +02:00
#pragma once
#include <BAN/Errors.h>
#include <BAN/Move.h>
#include <BAN/New.h>
2023-02-01 01:22:41 +02:00
namespace BAN
{
2023-02-06 19:01:52 +02:00
template<typename T, bool CONST>
2023-02-01 01:22:41 +02:00
class LinkedListIterator;
2023-02-01 01:22:41 +02:00
template<typename T>
class LinkedList
{
public:
using size_type = size_t;
using value_type = T;
2023-02-06 19:01:52 +02:00
using iterator = LinkedListIterator<T, false>;
using const_iterator = LinkedListIterator<T, true>;
2023-02-01 01:22:41 +02:00
public:
LinkedList() = default;
2023-02-06 19:01:52 +02:00
LinkedList(const LinkedList<T>& other) { *this = other; }
LinkedList(LinkedList<T>&& other) { *this = move(other); }
~LinkedList() { clear(); }
LinkedList<T>& operator=(const LinkedList<T>&);
LinkedList<T>& operator=(LinkedList<T>&&);
2023-02-01 01:22:41 +02:00
ErrorOr<void> push_back(const T&);
ErrorOr<void> push_back(T&&);
ErrorOr<void> insert(iterator, const T&);
ErrorOr<void> insert(iterator, T&&);
2023-02-01 01:22:41 +02:00
template<typename... Args>
ErrorOr<void> emplace_back(Args&&...);
2023-02-01 01:22:41 +02:00
template<typename... Args>
ErrorOr<void> emplace(iterator, Args&&...);
2023-02-01 01:22:41 +02:00
void pop_back();
iterator remove(iterator);
void clear();
2023-02-01 01:22:41 +02:00
2023-02-06 19:01:52 +02:00
iterator begin() { return iterator(m_data, empty()); }
const_iterator begin() const { return const_iterator(m_data, empty()); }
2023-02-01 01:22:41 +02:00
iterator end() { return iterator(m_last, true); }
const_iterator end() const { return const_iterator(m_last, true); }
const T& back() const;
T& back();
const T& front() const;
T& front();
2023-02-01 01:22:41 +02:00
bool contains(const T&) const;
size_type size() const;
bool empty() const;
2023-02-01 01:22:41 +02:00
private:
struct Node
{
T value;
Node* next;
Node* prev;
};
ErrorOr<Node*> allocate_node() const;
2023-02-01 01:22:41 +02:00
Node* m_data = nullptr;
Node* m_last = nullptr;
size_type m_size = 0;
2023-02-06 19:01:52 +02:00
friend class LinkedListIterator<T, true>;
friend class LinkedListIterator<T, false>;
2023-02-01 01:22:41 +02:00
};
2023-02-06 19:01:52 +02:00
template<typename T, bool CONST>
2023-02-01 01:22:41 +02:00
class LinkedListIterator
{
public:
using value_type = T;
using data_type = maybe_const_t<CONST, typename LinkedList<T>::Node>;
2023-02-01 01:22:41 +02:00
public:
LinkedListIterator() = default;
2023-02-06 19:01:52 +02:00
template<bool C>
LinkedListIterator(const LinkedListIterator<T, C>&, enable_if_t<C == CONST || !C>* = 0);
2023-02-01 01:22:41 +02:00
2023-02-06 19:01:52 +02:00
LinkedListIterator<T, CONST>& operator++();
LinkedListIterator<T, CONST>& operator--();
LinkedListIterator<T, CONST> operator++(int);
LinkedListIterator<T, CONST> operator--(int);
2023-02-01 01:22:41 +02:00
2023-02-06 19:01:52 +02:00
template<bool ENABLE = !CONST>
enable_if_t<ENABLE, T&> operator*();
const T& operator*() const;
template<bool ENABLE = !CONST>
enable_if_t<ENABLE, T*> operator->();
const T* operator->() const;
bool operator==(const LinkedListIterator<T, CONST>&) const;
bool operator!=(const LinkedListIterator<T, CONST>&) const;
operator bool() const;
2023-02-01 01:22:41 +02:00
private:
LinkedListIterator(data_type*, bool);
2023-02-06 19:01:52 +02:00
2023-02-01 01:22:41 +02:00
private:
data_type* m_current = nullptr;
2023-02-01 01:22:41 +02:00
bool m_past_end = false;
friend class LinkedList<T>;
2023-02-06 19:01:52 +02:00
friend class LinkedListIterator<T, !CONST>;
2023-02-01 01:22:41 +02:00
};
template<typename T>
2023-02-06 19:01:52 +02:00
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& other)
2023-02-01 01:22:41 +02:00
{
clear();
2023-02-06 19:01:52 +02:00
for (const T& elem : other)
MUST(push_back(elem));
return *this;
}
template<typename T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList<T>&& other)
{
clear();
m_data = other.m_data;
m_last = other.m_last;
m_size = other.m_size;
other.m_data = nullptr;
other.m_last = nullptr;
other.m_size = 0;
return *this;
2023-02-01 01:22:41 +02:00
}
template<typename T>
ErrorOr<void> LinkedList<T>::push_back(const T& value)
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
return push_back(move(T(value)));
2023-02-01 01:22:41 +02:00
}
template<typename T>
ErrorOr<void> LinkedList<T>::push_back(T&& value)
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
return insert(end(), move(value));
2023-02-01 01:22:41 +02:00
}
template<typename T>
ErrorOr<void> LinkedList<T>::insert(iterator iter, const T& value)
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
return insert(iter, move(T(value)));
2023-02-01 01:22:41 +02:00
}
template<typename T>
ErrorOr<void> LinkedList<T>::insert(iterator iter, T&& value)
2023-02-01 01:22:41 +02:00
{
Node* next = iter.m_past_end ? nullptr : iter.m_current;
Node* prev = next ? next->prev : m_last;
Node* new_node = TRY(allocate_node());
new (&new_node->value) T(move(value));
2023-02-01 01:22:41 +02:00
new_node->next = next;
new_node->prev = prev;
(prev ? prev->next : m_data) = new_node;
(next ? next->prev : m_last) = new_node;
m_size++;
return {};
}
template<typename T>
template<typename... Args>
ErrorOr<void> LinkedList<T>::emplace_back(Args&&... args)
2023-02-01 01:22:41 +02:00
{
return emplace(end(), forward<Args>(args)...);
2023-02-01 01:22:41 +02:00
}
template<typename T>
template<typename... Args>
ErrorOr<void> LinkedList<T>::emplace(iterator iter, Args&&... args)
2023-02-01 01:22:41 +02:00
{
Node* next = iter.m_past_end ? nullptr : iter.m_current;
Node* prev = next ? next->prev : m_last;
Node* new_node = TRY(allocate_node());
new (&new_node->value) T(forward<Args>(args)...);
2023-02-01 01:22:41 +02:00
new_node->next = next;
new_node->prev = prev;
(prev ? prev->next : m_data) = new_node;
(next ? next->prev : m_last) = new_node;
m_size++;
return {};
}
template<typename T>
void LinkedList<T>::pop_back()
2023-02-01 01:22:41 +02:00
{
2023-09-20 20:22:02 +03:00
remove(iterator(m_last, false));
2023-02-01 01:22:41 +02:00
}
template<typename T>
LinkedList<T>::iterator LinkedList<T>::remove(iterator iter)
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
ASSERT(!empty() && iter);
2023-02-01 01:22:41 +02:00
Node* node = iter.m_current;
Node* prev = node->prev;
Node* next = node->next;
node->value.~T();
BAN::deallocator(node);
(prev ? prev->next : m_data) = next;
(next ? next->prev : m_last) = prev;
m_size--;
return next ? iterator(next, false) : iterator(m_last, true);
2023-02-01 01:22:41 +02:00
}
template<typename T>
void LinkedList<T>::clear()
2023-02-01 01:22:41 +02:00
{
Node* ptr = m_data;
while (ptr)
{
Node* next = ptr->next;
ptr->value.~T();
BAN::deallocator(ptr);
ptr = next;
}
m_data = nullptr;
m_last = nullptr;
m_size = 0;
}
template<typename T>
const T& LinkedList<T>::back() const
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
ASSERT(!empty());
return *const_iterator(m_last, false);
2023-02-01 01:22:41 +02:00
}
template<typename T>
T& LinkedList<T>::back()
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
ASSERT(!empty());
return *iterator(m_last, false);
2023-02-01 01:22:41 +02:00
}
template<typename T>
const T& LinkedList<T>::front() const
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
ASSERT(!empty());
return *const_iterator(m_data, false);
2023-02-01 01:22:41 +02:00
}
template<typename T>
T& LinkedList<T>::front()
2023-02-01 01:22:41 +02:00
{
2023-02-06 19:01:52 +02:00
ASSERT(!empty());
return *iterator(m_data, false);
2023-02-01 01:22:41 +02:00
}
template<typename T>
bool LinkedList<T>::contains(const T& value) const
{
if (empty()) return false;
for (Node* node = m_data;; node = node->next)
{
if (node->value == value)
return true;
if (node == m_last)
return false;
}
}
2023-02-01 01:22:41 +02:00
template<typename T>
typename LinkedList<T>::size_type LinkedList<T>::size() const
2023-02-01 01:22:41 +02:00
{
return m_size;
}
template<typename T>
bool LinkedList<T>::empty() const
2023-02-01 01:22:41 +02:00
{
return m_size == 0;
}
template<typename T>
ErrorOr<typename LinkedList<T>::Node*> LinkedList<T>::allocate_node() const
{
Node* node = (Node*)BAN::allocator(sizeof(Node));
if (node == nullptr)
return Error::from_errno(ENOMEM);
2023-02-01 01:22:41 +02:00
return node;
}
2023-02-06 19:01:52 +02:00
template<typename T, bool CONST>
template<bool C>
LinkedListIterator<T, CONST>::LinkedListIterator(const LinkedListIterator<T, C>& other, enable_if_t<C == CONST || !C>*)
: m_current(other.m_current)
, m_past_end(other.m_past_end)
{
}
template<typename T, bool CONST>
LinkedListIterator<T, CONST>::LinkedListIterator(data_type* node, bool past_end)
: m_current(node)
, m_past_end(past_end)
2023-02-06 19:01:52 +02:00
{
}
template<typename T, bool CONST>
LinkedListIterator<T, CONST>& LinkedListIterator<T, CONST>::operator++()
{
ASSERT(m_current);
ASSERT(m_current->next || !m_past_end);
if (m_current->next)
m_current = m_current->next;
else
m_past_end = true;
return *this;
}
template<typename T, bool CONST>
LinkedListIterator<T, CONST>& LinkedListIterator<T, CONST>::operator--()
{
ASSERT(m_current);
ASSERT(m_current->prev || m_past_end);
if (m_past_end)
m_past_end = false;
else
m_current = m_current->prev;
return *this;
}
template<typename T, bool CONST>
LinkedListIterator<T, CONST> LinkedListIterator<T, CONST>::operator++(int)
{
auto temp = *this;
++(*this);
return temp;
}
template<typename T, bool CONST>
LinkedListIterator<T, CONST> LinkedListIterator<T, CONST>::operator--(int)
{
auto temp = *this;
--(*this);
return temp;
}
template<typename T, bool CONST>
template<bool ENABLE>
enable_if_t<ENABLE, T&> LinkedListIterator<T, CONST>::operator*()
{
ASSERT(m_current);
return m_current->value;
}
template<typename T, bool CONST>
const T& LinkedListIterator<T, CONST>::operator*() const
{
ASSERT(m_current);
return m_current->value;
}
template<typename T, bool CONST>
template<bool ENABLE>
enable_if_t<ENABLE, T*> LinkedListIterator<T, CONST>::operator->()
{
ASSERT(m_current);
return &m_current->value;
}
template<typename T, bool CONST>
const T* LinkedListIterator<T, CONST>::operator->() const
{
ASSERT(m_current);
return &m_current->value;
}
2023-02-06 19:01:52 +02:00
template<typename T, bool CONST>
bool LinkedListIterator<T, CONST>::operator==(const LinkedListIterator<T, CONST>& other) const
{
if (m_current != other.m_current)
return false;
return m_past_end == other.m_past_end;
}
template<typename T, bool CONST>
bool LinkedListIterator<T, CONST>::operator!=(const LinkedListIterator<T, CONST>& other) const
{
return !(*this == other);
}
template<typename T, bool CONST>
LinkedListIterator<T, CONST>::operator bool() const
{
return m_current;
}
2023-02-01 01:22:41 +02:00
}