BAN: Cleanup LinkedList code

This commit is contained in:
Bananymous 2023-02-06 19:01:52 +02:00
parent 0eb1fb8bae
commit 08dfb0e1db
1 changed files with 169 additions and 96 deletions

View File

@ -7,26 +7,26 @@
namespace BAN namespace BAN
{ {
template<typename T> template<typename T, bool CONST>
class LinkedListIterator; class LinkedListIterator;
template<typename T>
class LinkedListConstIterator;
template<typename T> template<typename T>
class LinkedList class LinkedList
{ {
BAN_NON_COPYABLE(LinkedList<T>);
BAN_NON_MOVABLE(LinkedList<T>);
public: public:
using size_type = size_t; using size_type = size_t;
using value_type = T; using value_type = T;
using iterator = LinkedListIterator<T>; using iterator = LinkedListIterator<T, false>;
using const_iterator = LinkedListConstIterator<T>; using const_iterator = LinkedListIterator<T, true>;
public: public:
LinkedList() = default; LinkedList() = default;
~LinkedList(); 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>&&);
[[nodiscard]] ErrorOr<void> push_back(const T&); [[nodiscard]] ErrorOr<void> push_back(const T&);
[[nodiscard]] ErrorOr<void> push_back(T&&); [[nodiscard]] ErrorOr<void> push_back(T&&);
@ -41,8 +41,8 @@ namespace BAN
void remove(const_iterator); void remove(const_iterator);
void clear(); void clear();
iterator begin() { return iterator(m_data, false); } iterator begin() { return iterator(m_data, empty()); }
const_iterator begin() const { return const_iterator(m_data, false); } const_iterator begin() const { return const_iterator(m_data, empty()); }
iterator end() { return iterator(m_last, true); } iterator end() { return iterator(m_last, true); }
const_iterator end() const { return const_iterator(m_last, true); } const_iterator end() const { return const_iterator(m_last, true); }
@ -68,11 +68,11 @@ namespace BAN
Node* m_last = nullptr; Node* m_last = nullptr;
size_type m_size = 0; size_type m_size = 0;
friend class LinkedListIterator<T>; friend class LinkedListIterator<T, true>;
friend class LinkedListConstIterator<T>; friend class LinkedListIterator<T, false>;
}; };
template<typename T> template<typename T, bool CONST>
class LinkedListIterator class LinkedListIterator
{ {
public: public:
@ -80,111 +80,77 @@ namespace BAN
public: public:
LinkedListIterator() = default; LinkedListIterator() = default;
LinkedListIterator(const LinkedListIterator<T>& other) : m_current(other.m_current), m_past_end(other.m_past_end) {} template<bool C>
LinkedListIterator<T>& operator++() LinkedListIterator(const LinkedListIterator<T, C>&, enable_if_t<C == CONST || !C>* = 0);
{
ASSERT(m_current); LinkedListIterator<T, CONST>& operator++();
ASSERT(m_current->next || !m_past_end); LinkedListIterator<T, CONST>& operator--();
if (m_current->next) LinkedListIterator<T, CONST> operator++(int);
m_current = m_current->next; LinkedListIterator<T, CONST> operator--(int);
else
m_past_end = true; template<bool ENABLE = !CONST>
return *this; enable_if_t<ENABLE, T&> operator*();
} const T& operator*() const;
LinkedListIterator<T>& operator--()
{ template<bool ENABLE = !CONST>
ASSERT(m_current); enable_if_t<ENABLE, T*> operator->();
ASSERT(m_current->prev || m_past_end); const T* operator->() const;
if (m_past_end)
m_past_end = false; bool operator==(const LinkedListIterator<T, CONST>&) const;
else bool operator!=(const LinkedListIterator<T, CONST>&) const;
m_current = m_current->prev; operator bool() const;
return *this;
}
LinkedListIterator<T> operator++(int) { auto temp = *this; ++(*this); return temp; }
LinkedListIterator<T> operator--(int) { auto temp = *this; --(*this); return temp; }
T& operator*() { ASSERT(m_current); return m_current->value; }
const T& operator*() const { ASSERT(m_current); return m_current->value; }
bool operator==(const LinkedListIterator<T>& other) const { return m_current && m_current == other.m_current && m_past_end == other.m_past_end; }
bool operator!=(const LinkedListIterator<T>& other) const { return !(*this == other); }
operator bool() const { return m_current; }
private: private:
LinkedListIterator(typename LinkedList<T>::Node* node, bool past_end) : m_current(node), m_past_end(past_end) { } LinkedListIterator(typename LinkedList<T>::Node*, bool);
private: private:
typename LinkedList<T>::Node* m_current = nullptr; typename LinkedList<T>::Node* m_current = nullptr;
bool m_past_end = false; bool m_past_end = false;
friend class LinkedList<T>; friend class LinkedList<T>;
friend class LinkedListConstIterator<T>; friend class LinkedListIterator<T, !CONST>;
}; };
template<typename T>
class LinkedListConstIterator
{
public:
using value_type = T;
public:
LinkedListConstIterator() = default;
LinkedListConstIterator(const LinkedListIterator<T>& other) : m_current(other.m_current), m_past_end(other.m_past_end) {}
LinkedListConstIterator(const LinkedListConstIterator<T>& other) : m_current(other.m_current), m_past_end(other.m_past_end) {}
LinkedListConstIterator<T>& 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;
}
LinkedListConstIterator<T>& 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;
}
LinkedListConstIterator<T> operator++(int) { auto temp = *this; ++(*this); return temp; }
LinkedListConstIterator<T> operator--(int) { auto temp = *this; --(*this); return temp; }
const T& operator*() const { ASSERT(m_current); return m_current->value; }
bool operator==(const LinkedListConstIterator<T>& other) const { return m_current && m_current == other.m_current && m_past_end == other.m_past_end; }
bool operator!=(const LinkedListConstIterator<T>& other) const { return !(*this == other); }
operator bool() const { return m_current; }
private:
LinkedListConstIterator(typename LinkedList<T>::Node* node, bool past_end) : m_current(node), m_past_end(past_end) {}
private:
typename LinkedList<T>::Node* m_current = nullptr;
bool m_past_end = false;
friend class LinkedList<T>;
};
template<typename T> template<typename T>
LinkedList<T>::~LinkedList() LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& other)
{ {
clear(); clear();
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;
} }
template<typename T> template<typename T>
ErrorOr<void> LinkedList<T>::push_back(const T& value) ErrorOr<void> LinkedList<T>::push_back(const T& value)
{ {
return push_back(Move(T(value))); return push_back(move(T(value)));
} }
template<typename T> template<typename T>
ErrorOr<void> LinkedList<T>::push_back(T&& value) ErrorOr<void> LinkedList<T>::push_back(T&& value)
{ {
return insert(end(), Move(value)); return insert(end(), move(value));
} }
template<typename T> template<typename T>
ErrorOr<void> LinkedList<T>::insert(const_iterator iter, const T& value) ErrorOr<void> LinkedList<T>::insert(const_iterator iter, const T& value)
{ {
return insert(iter, Move(T(value))); return insert(iter, move(T(value)));
} }
template<typename T> template<typename T>
@ -234,7 +200,7 @@ namespace BAN
template<typename T> template<typename T>
void LinkedList<T>::remove(const_iterator iter) void LinkedList<T>::remove(const_iterator iter)
{ {
ASSERT(m_size > 0); ASSERT(!empty() && iter);
Node* node = iter.m_current; Node* node = iter.m_current;
Node* prev = node->prev; Node* prev = node->prev;
Node* next = node->next; Node* next = node->next;
@ -264,28 +230,28 @@ namespace BAN
template<typename T> template<typename T>
const T& LinkedList<T>::back() const const T& LinkedList<T>::back() const
{ {
ASSERT(m_size > 0); ASSERT(!empty());
return *const_iterator(m_last); return *const_iterator(m_last);
} }
template<typename T> template<typename T>
T& LinkedList<T>::back() T& LinkedList<T>::back()
{ {
ASSERT(m_size > 0); ASSERT(!empty());
return *iterator(m_last); return *iterator(m_last);
} }
template<typename T> template<typename T>
const T& LinkedList<T>::front() const const T& LinkedList<T>::front() const
{ {
ASSERT(m_size > 0); ASSERT(!empty());
return *const_iterator(m_data); return *const_iterator(m_data);
} }
template<typename T> template<typename T>
T& LinkedList<T>::front() T& LinkedList<T>::front()
{ {
ASSERT(m_size > 0); ASSERT(!empty());
return *iterator(m_data); return *iterator(m_data);
} }
@ -310,4 +276,111 @@ namespace BAN
return node; return node;
} }
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(typename LinkedList<T>::Node* node, bool past_end)
: m_current(node),
m_past_end(past_end)
{
}
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;
}
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;
}
} }