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

214 lines
4.1 KiB
C
Raw Normal View History

#pragma once
#include <BAN/Errors.h>
2022-12-30 19:52:16 +02:00
#include <BAN/Math.h>
2023-01-17 11:38:16 +02:00
#include <BAN/Memory.h>
2023-01-13 14:11:02 +02:00
#include <BAN/Move.h>
namespace BAN
{
template<typename T>
class Queue
{
public:
2023-01-13 14:11:02 +02:00
using size_type = size_t;
using value_type = T;
public:
Queue() = default;
2023-01-13 14:11:02 +02:00
Queue(Queue<T>&&);
Queue(const Queue<T>&);
~Queue();
2023-01-13 14:11:02 +02:00
Queue<T>& operator=(Queue<T>&&);
Queue<T>& operator=(const Queue<T>&);
[[nodiscard]] ErrorOr<void> Push(T&&);
[[nodiscard]] ErrorOr<void> Push(const T&);
2023-01-17 12:02:59 +02:00
template<typename... Args>
[[nodiscard]] ErrorOr<void> Emplace(Args...);
2023-01-13 14:11:02 +02:00
void Pop();
2023-01-13 14:11:02 +02:00
void Clear();
bool Empty() const;
size_type Size() const;
const T& Front() const;
T& Front();
private:
[[nodiscard]] ErrorOr<void> EnsureCapacity(size_type size);
2023-01-18 17:19:57 +02:00
const T* AddressOf(size_type, void* = nullptr) const;
T* AddressOf(size_type, void* = nullptr);
private:
2023-01-13 14:11:02 +02:00
uint8_t* m_data = nullptr;
size_type m_capacity = 0;
size_type m_size = 0;
};
2023-01-13 14:11:02 +02:00
template<typename T>
Queue<T>::Queue(Queue<T>&& other)
{
m_data = other.m_data;
m_capacity = other.m_capacity;
m_size = other.m_size;
other.m_data = nullptr;
other.m_capacity = 0;
other.m_size = 0;
}
template<typename T>
Queue<T>::Queue(const Queue<T>& other)
{
MUST(EnsureCapacity(other.Size()));
for (size_type i = 0; i < other.Size(); i++)
new (AddressOf(i)) T(*AddressOf(i, other.m_data));
2023-01-13 14:11:02 +02:00
m_size = other.m_size;
}
template<typename T>
Queue<T>::~Queue()
{
2023-01-13 14:11:02 +02:00
Clear();
}
template<typename T>
2023-01-13 14:11:02 +02:00
Queue<T>& Queue<T>::operator=(Queue<T>&& other)
{
Clear();
m_data = other.m_data;
m_capacity = other.m_capacity;
m_size = other.m_size;
other.m_data = nullptr;
other.m_capacity = 0;
other.m_size = 0;
return *this;
}
template<typename T>
Queue<T>& Queue<T>::operator=(const Queue<T>& other)
{
Clear();
MUST(EnsureCapacity(other.Size()));
for (size_type i = 0; i < other.Size(); i++)
new (AddressOf(i)) T(*AddressOf(i, other.m_data));
2023-01-13 14:11:02 +02:00
m_size = other.m_size;
return *this;
}
template<typename T>
ErrorOr<void> Queue<T>::Push(T&& value)
{
TRY(EnsureCapacity(m_size + 1));
new (AddressOf(m_size)) T(Move(value));
2023-01-13 14:11:02 +02:00
m_size++;
return {};
}
2023-01-13 14:11:02 +02:00
template<typename T>
ErrorOr<void> Queue<T>::Push(const T& value)
{
return Push(Move(T(value)));
}
2023-01-17 12:02:59 +02:00
template<typename T>
template<typename... Args>
ErrorOr<void> Queue<T>::Emplace(Args... args)
{
TRY(EnsureCapacity(m_size + 1));
new (AddressOf(m_size)) T(Forward<Args>(args)...);
m_size++;
return {};
}
template<typename T>
void Queue<T>::Pop()
{
2023-01-10 17:43:18 +02:00
ASSERT(m_size > 0);
2023-01-13 14:11:02 +02:00
for (size_type i = 0; i < m_size - 1; i++)
*AddressOf(i) = Move(*AddressOf(i + 1));
AddressOf(m_size - 1)->~T();
2023-01-13 14:11:02 +02:00
m_size--;
}
template<typename T>
void Queue<T>::Clear()
{
for (size_type i = 0; i < m_size; i++)
AddressOf(i)->~T();
2023-01-13 14:11:02 +02:00
BAN::deallocator(m_data);
m_data = nullptr;
m_capacity = 0;
m_size = 0;
}
template<typename T>
bool Queue<T>::Empty() const
{
return m_size == 0;
}
template<typename T>
typename Queue<T>::size_type Queue<T>::Size() const
{
return m_size;
}
template<typename T>
const T& Queue<T>::Front() const
{
2023-01-10 17:43:18 +02:00
ASSERT(m_size > 0);
return *AddressOf(0);
}
template<typename T>
T& Queue<T>::Front()
{
2023-01-10 17:43:18 +02:00
ASSERT(m_size > 0);
return *AddressOf(0);
}
template<typename T>
ErrorOr<void> Queue<T>::EnsureCapacity(size_type size)
{
if (m_capacity > size)
return {};
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 3 / 2);
2023-01-13 14:11:02 +02:00
uint8_t* new_data = (uint8_t*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr)
return Error::FromString("Queue: Could not allocate memory");
2023-01-13 14:11:02 +02:00
for (size_type i = 0; i < m_size; i++)
{
new (AddressOf(i, new_data)) T(Move(*AddressOf(i)));
AddressOf(i)->~T();
2023-01-13 14:11:02 +02:00
}
BAN::deallocator(m_data);
m_data = new_data;
m_capacity = new_cap;
2022-12-13 12:10:50 +02:00
return {};
}
2023-01-13 14:11:02 +02:00
template<typename T>
2023-01-18 17:19:57 +02:00
const T* Queue<T>::AddressOf(size_type index, void* base) const
{
if (base == nullptr)
base = m_data;
2023-01-18 17:19:57 +02:00
return (T*)base + index;
}
template<typename T>
2023-01-18 17:19:57 +02:00
T* Queue<T>::AddressOf(size_type index, void* base)
2023-01-13 14:11:02 +02:00
{
if (base == nullptr)
base = m_data;
2023-01-18 17:19:57 +02:00
return (T*)base + index;
2023-01-13 14:11:02 +02:00
}
}