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>
|
2023-01-17 11:38:16 +02:00
|
|
|
#include <BAN/Memory.h>
|
2023-01-13 13:50:47 +02:00
|
|
|
#include <BAN/Move.h>
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
namespace BAN
|
|
|
|
{
|
|
|
|
|
2023-01-13 17:48:19 +02:00
|
|
|
template<typename T>
|
|
|
|
class Vector;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class VectorIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VectorIterator() = default;
|
|
|
|
VectorIterator(const VectorIterator& other) : m_data(other.m_data) { }
|
|
|
|
VectorIterator& operator=(const VectorIterator& other) { m_data = other.m_data; return *this; }
|
|
|
|
VectorIterator& operator++() { m_data++; return *this; }
|
|
|
|
T& operator*() { return *m_data; }
|
|
|
|
const T& operator*() const { return *m_data; }
|
|
|
|
T* operator->() { return m_data; }
|
|
|
|
const T* operator->() const { return m_data; }
|
|
|
|
bool operator==(const VectorIterator<T>& other) const { return !(*this != other); }
|
|
|
|
bool operator!=(const VectorIterator<T>& other) const { return m_data != other.m_data; }
|
|
|
|
private:
|
|
|
|
VectorIterator(T* data) : m_data(data) { }
|
|
|
|
private:
|
|
|
|
T* m_data = nullptr;
|
|
|
|
friend class Vector<T>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class VectorConstIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VectorConstIterator() = default;
|
|
|
|
VectorConstIterator(const VectorConstIterator& other) : m_data(other.m_data) { }
|
|
|
|
VectorConstIterator& operator=(const VectorConstIterator& other) { m_data = other.m_data; return *this; }
|
|
|
|
VectorConstIterator& operator++() { m_data++; return *this; }
|
|
|
|
const T& operator*() const { return *m_data; }
|
|
|
|
const T* operator->() const { return m_data; }
|
|
|
|
bool operator==(const VectorConstIterator<T>& other) const { return !(*this != other); }
|
|
|
|
bool operator!=(const VectorConstIterator<T>& other) const { return m_data != other.m_data; }
|
|
|
|
private:
|
|
|
|
VectorConstIterator(T* data) : m_data(data) { }
|
|
|
|
private:
|
|
|
|
const T* m_data = nullptr;
|
|
|
|
friend class Vector<T>;
|
|
|
|
};
|
|
|
|
|
2023-01-13 13:50:47 +02:00
|
|
|
// T must be move assignable, move constructable (and copy constructable for some functions)
|
2022-12-13 14:43:58 +02:00
|
|
|
template<typename T>
|
|
|
|
class Vector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using size_type = size_t;
|
|
|
|
using value_type = T;
|
2023-01-13 17:48:19 +02:00
|
|
|
using iterator = VectorIterator<T>;
|
|
|
|
using const_iterator = VectorConstIterator<T>;
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
Vector() = default;
|
2023-01-13 13:50:47 +02:00
|
|
|
Vector(Vector<T>&&);
|
2022-12-15 17:28:12 +02:00
|
|
|
Vector(const Vector<T>&);
|
2022-12-13 14:43:58 +02:00
|
|
|
~Vector();
|
|
|
|
|
2023-01-13 13:50:47 +02:00
|
|
|
Vector<T>& operator=(Vector<T>&&);
|
|
|
|
Vector<T>& operator=(const Vector<T>&);
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> push_back(T&&);
|
|
|
|
[[nodiscard]] ErrorOr<void> push_back(const T&);
|
2023-01-17 11:59:36 +02:00
|
|
|
template<typename... Args>
|
2023-02-01 21:05:44 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> emplace_back(Args...);
|
2023-01-17 11:59:36 +02:00
|
|
|
template<typename... Args>
|
2023-02-01 21:05:44 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> emplace(size_type, Args...);
|
|
|
|
[[nodiscard]] ErrorOr<void> insert(size_type, T&&);
|
|
|
|
[[nodiscard]] ErrorOr<void> insert(size_type, const T&);
|
2022-12-13 14:43:58 +02:00
|
|
|
|
2023-01-13 17:48:19 +02:00
|
|
|
iterator begin();
|
|
|
|
iterator end();
|
|
|
|
const_iterator begin() const;
|
|
|
|
const_iterator end() const;
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void pop_back();
|
|
|
|
void remove(size_type);
|
|
|
|
void clear();
|
2022-12-13 14:43:58 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
bool has(const T&) const;
|
2022-12-20 11:38:29 +02:00
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
const T& operator[](size_type) const;
|
|
|
|
T& operator[](size_type);
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
const T& back() const;
|
|
|
|
T& back();
|
|
|
|
const T& front() const;
|
|
|
|
T& front();
|
2022-12-13 15:08:12 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> resize(size_type);
|
|
|
|
[[nodiscard]] ErrorOr<void> reserve(size_type);
|
2022-12-13 14:43:58 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
bool empty() const;
|
|
|
|
size_type size() const;
|
|
|
|
size_type capacity() const;
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
private:
|
2023-02-01 21:05:44 +02:00
|
|
|
[[nodiscard]] ErrorOr<void> ensure_capacity(size_type);
|
|
|
|
const T* address_of(size_type, void* = nullptr) const;
|
|
|
|
T* address_of(size_type, void* = nullptr);
|
2022-12-13 14:43:58 +02:00
|
|
|
|
|
|
|
private:
|
2023-01-13 13:50:47 +02:00
|
|
|
uint8_t* m_data = nullptr;
|
|
|
|
size_type m_capacity = 0;
|
2022-12-13 14:43:58 +02:00
|
|
|
size_type m_size = 0;
|
|
|
|
};
|
|
|
|
|
2023-01-13 13:50:47 +02:00
|
|
|
template<typename T>
|
|
|
|
Vector<T>::Vector(Vector<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;
|
|
|
|
}
|
|
|
|
|
2022-12-15 17:28:12 +02:00
|
|
|
template<typename T>
|
|
|
|
Vector<T>::Vector(const Vector<T>& other)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
MUST(ensure_capacity(other.m_size));
|
2022-12-15 17:28:12 +02:00
|
|
|
for (size_type i = 0; i < other.m_size; i++)
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(i)) T(other[i]);
|
2022-12-15 17:28:12 +02:00
|
|
|
m_size = other.m_size;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
template<typename T>
|
|
|
|
Vector<T>::~Vector()
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
clear();
|
2022-12-13 14:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-01-13 13:50:47 +02:00
|
|
|
Vector<T>& Vector<T>::operator=(Vector<T>&& other)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
clear();
|
2023-01-13 13:50:47 +02:00
|
|
|
|
|
|
|
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;
|
2023-01-18 17:16:19 +02:00
|
|
|
|
|
|
|
return *this;
|
2023-01-13 13:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Vector<T>& Vector<T>::operator=(const Vector<T>& other)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
clear();
|
|
|
|
MUST(ensure_capacity(other.size()));
|
|
|
|
for (size_type i = 0; i < other.size(); i++)
|
|
|
|
new (address_of(i)) T(other[i]);
|
2023-01-13 13:50:47 +02:00
|
|
|
m_size = other.m_size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::push_back(T&& value)
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
TRY(ensure_capacity(m_size + 1));
|
|
|
|
new (address_of(m_size)) T(move(value));
|
2022-12-13 14:43:58 +02:00
|
|
|
m_size++;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::push_back(const T& value)
|
2023-01-13 13:50:47 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
return push_back(move(T(value)));
|
2023-01-13 13:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-01-17 11:59:36 +02:00
|
|
|
template<typename... Args>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::emplace_back(Args... args)
|
2023-01-17 11:59:36 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
TRY(ensure_capacity(m_size + 1));
|
|
|
|
new (address_of(m_size)) T(forward<Args>(args)...);
|
2023-01-17 11:59:36 +02:00
|
|
|
m_size++;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
template<typename... Args>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::emplace(size_type index, Args... args)
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index <= m_size);
|
2023-02-01 21:05:44 +02:00
|
|
|
TRY(ensure_capacity(m_size + 1));
|
2023-01-13 13:50:47 +02:00
|
|
|
if (index < m_size)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(m_size)) T(move(*address_of(m_size - 1)));
|
2023-01-13 13:50:47 +02:00
|
|
|
for (size_type i = m_size - 1; i > index; i--)
|
2023-02-01 21:05:44 +02:00
|
|
|
*address_of(i) = move(*address_of(i - 1));
|
|
|
|
*address_of(index) = move(T(forward<Args>(args)...));
|
2023-01-17 11:59:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(m_size)) T(forward<Args>(args)...);
|
2023-01-17 11:59:36 +02:00
|
|
|
}
|
|
|
|
m_size++;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::insert(size_type index, T&& value)
|
2023-01-17 11:59:36 +02:00
|
|
|
{
|
|
|
|
ASSERT(index <= m_size);
|
2023-02-01 21:05:44 +02:00
|
|
|
TRY(ensure_capacity(m_size + 1));
|
2023-01-17 11:59:36 +02:00
|
|
|
if (index < m_size)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(m_size)) T(move(*address_of(m_size - 1)));
|
2023-01-17 11:59:36 +02:00
|
|
|
for (size_type i = m_size - 1; i > index; i--)
|
2023-02-01 21:05:44 +02:00
|
|
|
*address_of(i) = move(*address_of(i - 1));
|
|
|
|
*address_of(index) = move(value);
|
2023-01-17 11:59:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(m_size)) T(move(value));
|
2023-01-13 13:50:47 +02:00
|
|
|
}
|
2022-12-13 14:43:58 +02:00
|
|
|
m_size++;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:50:47 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::insert(size_type index, const T& value)
|
2023-01-13 13:50:47 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
return insert(move(T(value)), index);
|
2023-01-13 13:50:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 17:48:19 +02:00
|
|
|
template<typename T>
|
|
|
|
typename Vector<T>::iterator Vector<T>::begin()
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
return VectorIterator<T>(address_of(0));
|
2023-01-13 17:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename Vector<T>::iterator Vector<T>::end()
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
return VectorIterator<T>(address_of(m_size));
|
2023-01-13 17:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename Vector<T>::const_iterator Vector<T>::begin() const
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
return VectorConstIterator<T>(address_of(0));
|
2023-01-13 17:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
typename Vector<T>::const_iterator Vector<T>::end() const
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
return VectorConstIterator<T>(address_of(m_size));
|
2023-01-13 17:48:19 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
void Vector<T>::pop_back()
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2023-02-01 21:05:44 +02:00
|
|
|
address_of(m_size - 1)->~T();
|
2022-12-13 14:43:58 +02:00
|
|
|
m_size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
void Vector<T>::remove(size_type index)
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index < m_size);
|
2023-01-13 13:50:47 +02:00
|
|
|
for (size_type i = index; i < m_size - 1; i++)
|
2023-02-01 21:05:44 +02:00
|
|
|
*address_of(i) = move(*address_of(i + 1));
|
|
|
|
address_of(m_size - 1)->~T();
|
2022-12-13 14:43:58 +02:00
|
|
|
m_size--;
|
|
|
|
}
|
2022-12-20 11:38:29 +02:00
|
|
|
|
2023-01-13 13:50:47 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
void Vector<T>::clear()
|
2023-01-13 13:50:47 +02:00
|
|
|
{
|
|
|
|
for (size_type i = 0; i < m_size; i++)
|
2023-02-01 21:05:44 +02:00
|
|
|
address_of(i)->~T();
|
2023-01-13 13:50:47 +02:00
|
|
|
BAN::deallocator(m_data);
|
|
|
|
m_data = nullptr;
|
|
|
|
m_capacity = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:38:29 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
bool Vector<T>::has(const T& other) const
|
2022-12-20 11:38:29 +02:00
|
|
|
{
|
|
|
|
for (size_type i = 0; i < m_size; i++)
|
2023-02-01 21:05:44 +02:00
|
|
|
if (*address_of(i) == other)
|
2022-12-20 11:38:29 +02:00
|
|
|
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);
|
2023-02-01 21:05:44 +02:00
|
|
|
return *address_of(index);
|
2022-12-13 14:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& Vector<T>::operator[](size_type index)
|
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(index < m_size);
|
2023-02-01 21:05:44 +02:00
|
|
|
return *address_of(index);
|
2022-12-13 14:43:58 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 15:08:12 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
const T& Vector<T>::back() const
|
2022-12-13 15:08:12 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2023-02-01 21:05:44 +02:00
|
|
|
return *address_of(m_size - 1);
|
2022-12-13 15:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
T& Vector<T>::back()
|
2022-12-13 15:08:12 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2023-02-01 21:05:44 +02:00
|
|
|
return *address_of(m_size - 1);
|
2022-12-13 15:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
const T& Vector<T>::front() const
|
2022-12-13 15:08:12 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2023-02-01 21:05:44 +02:00
|
|
|
return *address_of(0);
|
2022-12-13 15:08:12 +02:00
|
|
|
}
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
T& Vector<T>::front()
|
2022-12-13 15:08:12 +02:00
|
|
|
{
|
2023-01-10 17:43:18 +02:00
|
|
|
ASSERT(m_size > 0);
|
2023-02-01 21:05:44 +02:00
|
|
|
return *address_of(0);
|
2022-12-13 15:08:12 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::resize(size_type size)
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
TRY(ensure_capacity(size));
|
2022-12-13 14:43:58 +02:00
|
|
|
if (size < m_size)
|
|
|
|
for (size_type i = size; i < m_size; i++)
|
2023-02-01 21:05:44 +02:00
|
|
|
address_of(i)->~T();
|
2023-01-13 13:50:47 +02:00
|
|
|
if (size > m_size)
|
2022-12-13 14:43:58 +02:00
|
|
|
for (size_type i = m_size; i < size; i++)
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(i)) T();
|
2022-12-13 14:43:58 +02:00
|
|
|
m_size = size;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::reserve(size_type size)
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
TRY(ensure_capacity(size));
|
2022-12-13 14:43:58 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
bool Vector<T>::empty() const
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
|
|
|
return m_size == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
typename Vector<T>::size_type Vector<T>::size() const
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
typename Vector<T>::size_type Vector<T>::capacity() const
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-01-13 13:50:47 +02:00
|
|
|
return m_capacity;
|
2022-12-13 14:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
ErrorOr<void> Vector<T>::ensure_capacity(size_type size)
|
2022-12-13 14:43:58 +02:00
|
|
|
{
|
2023-01-13 13:50:47 +02:00
|
|
|
if (m_capacity >= size)
|
2022-12-13 14:43:58 +02:00
|
|
|
return {};
|
2023-01-13 13:50:47 +02:00
|
|
|
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 3 / 2);
|
|
|
|
uint8_t* new_data = (uint8_t*)BAN::allocator(new_cap * sizeof(T));
|
2022-12-13 14:43:58 +02:00
|
|
|
if (new_data == nullptr)
|
2023-02-01 21:05:44 +02:00
|
|
|
return Error::from_string("Vector: Could not allocate memory");
|
2023-01-13 13:50:47 +02:00
|
|
|
for (size_type i = 0; i < m_size; i++)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
new (address_of(i, new_data)) T(move(*address_of(i)));
|
|
|
|
address_of(i)->~T();
|
2023-01-13 13:50:47 +02:00
|
|
|
}
|
|
|
|
BAN::deallocator(m_data);
|
2023-01-13 01:09:40 +02:00
|
|
|
m_data = new_data;
|
2023-01-13 13:50:47 +02:00
|
|
|
m_capacity = new_cap;
|
2022-12-13 14:43:58 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:50:47 +02:00
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
const T* Vector<T>::address_of(size_type index, void* base) const
|
2023-01-16 12:08:22 +02:00
|
|
|
{
|
|
|
|
if (base == nullptr)
|
|
|
|
base = m_data;
|
2023-01-18 17:16:19 +02:00
|
|
|
return (T*)base + index;
|
2023-01-16 12:08:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-01 21:05:44 +02:00
|
|
|
T* Vector<T>::address_of(size_type index, void* base)
|
2023-01-13 13:50:47 +02:00
|
|
|
{
|
|
|
|
if (base == nullptr)
|
|
|
|
base = m_data;
|
2023-01-18 17:16:19 +02:00
|
|
|
return (T*)base + index;
|
2023-01-13 13:50:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:58 +02:00
|
|
|
}
|