2022-12-13 15:15:36 +02:00
|
|
|
#pragma once
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
#include <BAN/Errors.h>
|
2022-12-30 19:52:16 +02:00
|
|
|
#include <BAN/Math.h>
|
2022-12-13 14:43:58 +02:00
|
|
|
#include <BAN/Memory.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace BAN
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Vector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using size_type = size_t;
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Vector() = default;
|
2022-12-15 17:28:12 +02:00
|
|
|
Vector(const Vector<T>&);
|
2022-12-13 14:43:58 +02:00
|
|
|
~Vector();
|
|
|
|
|
2023-01-12 23:57:07 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> PushBack(const T&);
|
|
|
|
[[nodiscard]] ErrorOr<void> Insert(const T&, size_type);
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
void PopBack();
|
|
|
|
void Remove(size_type);
|
|
|
|
|
2022-12-20 11:38:29 +02:00
|
|
|
bool Has(const T&) const;
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
const T& operator[](size_type) const;
|
|
|
|
T& operator[](size_type);
|
|
|
|
|
2022-12-13 15:08:12 +02:00
|
|
|
const T& Back() const;
|
|
|
|
T& Back();
|
|
|
|
const T& Front() const;
|
|
|
|
T& Front();
|
|
|
|
|
2023-01-12 23:57:07 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> Resize(size_type);
|
|
|
|
[[nodiscard]] ErrorOr<void> Reserve(size_type);
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
bool Empty() const;
|
|
|
|
size_type Size() const;
|
|
|
|
size_type Capasity() const;
|
|
|
|
|
|
|
|
private:
|
2023-01-12 23:57:07 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> EnsureCapasity(size_type);
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_data = nullptr;
|
|
|
|
size_type m_capasity = 0;
|
|
|
|
size_type m_size = 0;
|
|
|
|
};
|
|
|
|
|
2022-12-15 17:28:12 +02:00
|
|
|
template<typename T>
|
|
|
|
Vector<T>::Vector(const Vector<T>& other)
|
|
|
|
{
|
|
|
|
MUST(EnsureCapasity(other.m_size));
|
|
|
|
for (size_type i = 0; i < other.m_size; i++)
|
|
|
|
m_data[i] = other[i];
|
|
|
|
m_size = other.m_size;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
template<typename T>
|
|
|
|
Vector<T>::~Vector()
|
|
|
|
{
|
|
|
|
for (size_type i = 0; i < m_size; i++)
|
|
|
|
m_data[i].~T();
|
|
|
|
BAN::deallocator(m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> Vector<T>::PushBack(const T& value)
|
|
|
|
{
|
|
|
|
TRY(EnsureCapasity(m_size + 1));
|
|
|
|
m_data[m_size] = value;
|
|
|
|
m_size++;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> Vector<T>::Insert(const T& value, size_type index)
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index <= m_size);
|
2022-12-13 14:43:58 +02:00
|
|
|
TRY(EnsureCapasity(m_size + 1));
|
|
|
|
memmove(m_data + index + 1, m_data + index, (m_size - index) * sizeof(T));
|
|
|
|
m_data[index] = value;
|
|
|
|
m_size++;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void Vector<T>::PopBack()
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 14:43:58 +02:00
|
|
|
m_data[m_size - 1].~T();
|
|
|
|
m_size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void Vector<T>::Remove(size_type index)
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index < m_size);
|
2022-12-13 14:43:58 +02:00
|
|
|
m_data[index].~T();
|
|
|
|
memmove(m_data + index, m_data + index + 1, (m_size - index - 1) * sizeof(T));
|
|
|
|
m_size--;
|
|
|
|
}
|
2022-12-20 11:38:29 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool Vector<T>::Has(const T& other) const
|
|
|
|
{
|
|
|
|
for (size_type i = 0; i < m_size; i++)
|
|
|
|
if (m_data[i] == other)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T& Vector<T>::operator[](size_type index) const
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index < m_size);
|
2022-12-13 14:43:58 +02:00
|
|
|
return m_data[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& Vector<T>::operator[](size_type index)
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index < m_size);
|
2022-12-13 14:43:58 +02:00
|
|
|
return m_data[index];
|
|
|
|
}
|
|
|
|
|
2022-12-13 15:08:12 +02:00
|
|
|
template<typename T>
|
|
|
|
const T& Vector<T>::Back() const
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 15:08:12 +02:00
|
|
|
return m_data[m_size - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& Vector<T>::Back()
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 15:08:12 +02:00
|
|
|
return m_data[m_size - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T& Vector<T>::Front() const
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 15:08:12 +02:00
|
|
|
return m_data[0];
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
T& Vector<T>::Front()
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2022-12-13 15:08:12 +02:00
|
|
|
return m_data[0];
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> Vector<T>::Resize(size_type size)
|
|
|
|
{
|
|
|
|
if (size < m_size)
|
|
|
|
{
|
|
|
|
for (size_type i = size; i < m_size; i++)
|
|
|
|
m_data[i].~T();
|
|
|
|
m_size = size;
|
|
|
|
}
|
|
|
|
else if (size > m_size)
|
|
|
|
{
|
|
|
|
TRY(EnsureCapasity(size));
|
|
|
|
for (size_type i = m_size; i < size; i++)
|
|
|
|
m_data[i] = T();
|
|
|
|
m_size = size;
|
|
|
|
}
|
|
|
|
m_size = size;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> Vector<T>::Reserve(size_type size)
|
|
|
|
{
|
|
|
|
TRY(EnsureCapasity(size));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool Vector<T>::Empty() const
|
|
|
|
{
|
|
|
|
return m_size == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename Vector<T>::size_type Vector<T>::Size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename Vector<T>::size_type Vector<T>::Capasity() const
|
|
|
|
{
|
|
|
|
return m_capasity;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> Vector<T>::EnsureCapasity(size_type size)
|
|
|
|
{
|
|
|
|
if (m_capasity >= size)
|
|
|
|
return {};
|
2023-01-12 17:00:29 +02:00
|
|
|
size_type new_cap = BAN::Math::max<size_type>(size, m_capasity * 3 / 2);
|
2022-12-13 14:43:58 +02:00
|
|
|
void* new_data = BAN::allocator(new_cap * sizeof(T));
|
|
|
|
if (new_data == nullptr)
|
|
|
|
return Error::FromString("Vector: Could not allocate memory");
|
2023-01-13 00:15:40 +02:00
|
|
|
if (m_data)
|
|
|
|
memcpy(new_data, m_data, m_size * sizeof(T));
|
2022-12-13 14:43:58 +02:00
|
|
|
BAN::deallocator(m_data);
|
|
|
|
m_data = (T*)new_data;
|
2023-01-13 00:56:38 +02:00
|
|
|
for (size_type i = m_capasity; i < new_cap; i++)
|
|
|
|
m_data[i] = T();
|
2022-12-13 14:43:58 +02:00
|
|
|
m_capasity = new_cap;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|