BAN: Rewrite Queue with move semantics

This commit is contained in:
Bananymous 2023-01-13 14:11:02 +02:00
parent 0f4e95acc5
commit 57cbd728db
1 changed files with 104 additions and 21 deletions

View File

@ -2,10 +2,7 @@
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Math.h> #include <BAN/Math.h>
#include <BAN/Memory.h> #include <BAN/Move.h>
#include <stdint.h>
#include <string.h>
namespace BAN namespace BAN
{ {
@ -14,15 +11,23 @@ namespace BAN
class Queue class Queue
{ {
public: public:
using size_type = uint32_t; using size_type = size_t;
using value_type = T; using value_type = T;
public: public:
Queue() = default; Queue() = default;
Queue(Queue<T>&&);
Queue(const Queue<T>&);
~Queue(); ~Queue();
[[nodiscard]] ErrorOr<void> Push(const T& value); Queue<T>& operator=(Queue<T>&&);
Queue<T>& operator=(const Queue<T>&);
[[nodiscard]] ErrorOr<void> Push(T&&);
[[nodiscard]] ErrorOr<void> Push(const T&);
void Pop(); void Pop();
void Clear();
bool Empty() const; bool Empty() const;
size_type Size() const; size_type Size() const;
@ -32,35 +37,102 @@ namespace BAN
private: private:
[[nodiscard]] ErrorOr<void> EnsureCapacity(size_type size); [[nodiscard]] ErrorOr<void> EnsureCapacity(size_type size);
T* Address(size_type, uint8_t* = nullptr) const;
private: private:
T* m_data = nullptr; uint8_t* m_data = nullptr;
size_type m_capacity = 0; size_type m_capacity = 0;
size_type m_size = 0; size_type m_size = 0;
}; };
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 (Address(i)) T(*Address(i, other.m_data));
m_size = other.m_size;
}
template<typename T> template<typename T>
Queue<T>::~Queue() Queue<T>::~Queue()
{ {
for (size_type i = 0; i < m_size; i++) Clear();
m_data[i].~T(); }
delete[] m_data;
template<typename T>
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 (Address(i)) T(*Address(i, other.m_data));
m_size = other.m_size;
return *this;
}
template<typename T>
ErrorOr<void> Queue<T>::Push(T&& value)
{
TRY(EnsureCapacity(m_size + 1));
new (Address(m_size)) T(Move(value));
m_size++;
return {};
} }
template<typename T> template<typename T>
ErrorOr<void> Queue<T>::Push(const T& value) ErrorOr<void> Queue<T>::Push(const T& value)
{ {
TRY(EnsureCapacity(m_size + 1)); return Push(Move(T(value)));
m_data[m_size++] = value;
return {};
} }
template<typename T> template<typename T>
void Queue<T>::Pop() void Queue<T>::Pop()
{ {
ASSERT(m_size > 0); ASSERT(m_size > 0);
m_data->~T(); for (size_type i = 0; i < m_size - 1; i++)
memmove(m_data, m_data + 1, sizeof(T) * (--m_size)); *Address(i) = Move(*Address(i + 1));
Address(m_size - 1)->~T();
m_size--;
}
template<typename T>
void Queue<T>::Clear()
{
for (size_type i = 0; i < m_size; i++)
Address(i)->~T();
BAN::deallocator(m_data);
m_data = nullptr;
m_capacity = 0;
m_size = 0;
} }
template<typename T> template<typename T>
@ -79,14 +151,14 @@ namespace BAN
const T& Queue<T>::Front() const const T& Queue<T>::Front() const
{ {
ASSERT(m_size > 0); ASSERT(m_size > 0);
return *m_data; return *Address(0);
} }
template<typename T> template<typename T>
T& Queue<T>::Front() T& Queue<T>::Front()
{ {
ASSERT(m_size > 0); ASSERT(m_size > 0);
return *m_data; return *Address(0);
} }
template<typename T> template<typename T>
@ -95,15 +167,26 @@ namespace BAN
if (m_capacity > size) if (m_capacity > size)
return {}; return {};
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 3 / 2); size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 3 / 2);
T* new_data = new T[new_cap]; uint8_t* new_data = (uint8_t*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr) if (new_data == nullptr)
return Error::FromString("Queue: Could not allocate memory"); return Error::FromString("Queue: Could not allocate memory");
if (m_data) for (size_type i = 0; i < m_size; i++)
memcpy(new_data, m_data, m_size * sizeof(T)); {
delete[] m_data; new (Address(i, new_data)) T(Move(*Address(i)));
Address(i)->~T();
}
BAN::deallocator(m_data);
m_data = new_data; m_data = new_data;
m_capacity = new_cap; m_capacity = new_cap;
return {}; return {};
} }
template<typename T>
T* Queue<T>::Address(size_type index, uint8_t* base) const
{
if (base == nullptr)
base = m_data;
return (T*)(base + index * sizeof(T));
}
} }