2022-12-13 10:39:57 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <BAN/Errors.h>
|
2022-12-30 19:52:16 +02:00
|
|
|
#include <BAN/Math.h>
|
2022-12-13 13:40:54 +02:00
|
|
|
#include <BAN/Memory.h>
|
2022-12-13 10:39:57 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace BAN
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Queue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using size_type = uint32_t;
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Queue() = default;
|
|
|
|
~Queue();
|
|
|
|
|
2023-01-12 23:57:07 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> Push(const T& value);
|
2022-12-13 10:39:57 +02:00
|
|
|
void Pop();
|
|
|
|
|
|
|
|
bool Empty() const;
|
|
|
|
size_type Size() const;
|
|
|
|
|
|
|
|
const T& Front() const;
|
|
|
|
T& Front();
|
|
|
|
|
|
|
|
private:
|
2023-01-13 00:15:40 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> EnsureCapacity(size_type size);
|
2022-12-13 10:39:57 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_data = nullptr;
|
|
|
|
size_type m_capacity = 0;
|
|
|
|
size_type m_size = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Queue<T>::~Queue()
|
|
|
|
{
|
2022-12-13 13:40:54 +02:00
|
|
|
for (size_type i = 0; i < m_size; i++)
|
|
|
|
m_data[i].~T();
|
2023-01-13 01:09:40 +02:00
|
|
|
delete[] m_data;
|
2022-12-13 10:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> Queue<T>::Push(const T& value)
|
|
|
|
{
|
2023-01-13 00:15:40 +02:00
|
|
|
TRY(EnsureCapacity(m_size + 1));
|
2022-12-13 10:39:57 +02:00
|
|
|
m_data[m_size++] = value;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void Queue<T>::Pop()
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 10:39:57 +02:00
|
|
|
m_data->~T();
|
|
|
|
memmove(m_data, m_data + 1, sizeof(T) * (--m_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-12-13 10:39:57 +02:00
|
|
|
return *m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& Queue<T>::Front()
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 10:39:57 +02:00
|
|
|
return *m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-01-13 00:15:40 +02:00
|
|
|
ErrorOr<void> Queue<T>::EnsureCapacity(size_type size)
|
2022-12-13 10:39:57 +02:00
|
|
|
{
|
|
|
|
if (m_capacity > size)
|
|
|
|
return {};
|
2023-01-12 17:00:29 +02:00
|
|
|
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 3 / 2);
|
2023-01-13 01:09:40 +02:00
|
|
|
T* new_data = new T[new_cap];
|
2022-12-13 10:39:57 +02:00
|
|
|
if (new_data == nullptr)
|
2022-12-13 13:40:54 +02:00
|
|
|
return Error::FromString("Queue: Could not allocate memory");
|
2023-01-13 00:15:40 +02:00
|
|
|
if (m_data)
|
|
|
|
memcpy(new_data, m_data, m_size * sizeof(T));
|
2023-01-13 01:09:40 +02:00
|
|
|
delete[] m_data;
|
|
|
|
m_data = new_data;
|
2022-12-13 10:39:57 +02:00
|
|
|
m_capacity = new_cap;
|
2022-12-13 12:10:50 +02:00
|
|
|
return {};
|
2022-12-13 10:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|