[ox/std] Make Vector use std::allocator
This commit is contained in:
parent
380dd1cd76
commit
263ed4f7e7
26
deps/ox/src/ox/std/vector.hpp
vendored
26
deps/ox/src/ox/std/vector.hpp
vendored
@ -12,6 +12,7 @@
|
||||
#include "initializerlist.hpp"
|
||||
#include "iterator.hpp"
|
||||
#include "math.hpp"
|
||||
#include "memory.hpp"
|
||||
#include "new.hpp"
|
||||
#include "types.hpp"
|
||||
#include "utility.hpp"
|
||||
@ -23,6 +24,7 @@ namespace detail {
|
||||
template<typename T, std::size_t Size = 100>
|
||||
struct SmallVector {
|
||||
private:
|
||||
std::allocator<T> m_allocator;
|
||||
AllocAlias<T> m_data[Size] = {};
|
||||
|
||||
public:
|
||||
@ -34,7 +36,7 @@ struct SmallVector {
|
||||
if (cap <= Size) {
|
||||
*items = reinterpret_cast<T*>(m_data);
|
||||
} else {
|
||||
*items = reinterpret_cast<T*>(new AllocAlias<T>[cap]);
|
||||
*items = m_allocator.allocate(cap);
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,9 +62,9 @@ struct SmallVector {
|
||||
}
|
||||
}
|
||||
|
||||
constexpr void clearItems(AllocAlias<T> *items) noexcept {
|
||||
if (items != m_data) {
|
||||
delete[] items;
|
||||
constexpr void clearItems(T *items, std::size_t n) noexcept {
|
||||
if (static_cast<void*>(items) != static_cast<void*>(m_data)) {
|
||||
m_allocator.deallocate(items, n);
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,13 +72,15 @@ struct SmallVector {
|
||||
|
||||
template<typename T>
|
||||
struct SmallVector<T, 0> {
|
||||
private:
|
||||
std::allocator<T> m_allocator;
|
||||
public:
|
||||
SmallVector() noexcept = default;
|
||||
SmallVector(SmallVector&) noexcept = default;
|
||||
SmallVector(SmallVector&&) noexcept = default;
|
||||
protected:
|
||||
constexpr void initItems(T **items, std::size_t cap) noexcept {
|
||||
*items = reinterpret_cast<T*>(new AllocAlias<T>[cap]);
|
||||
*items = m_allocator.allocate(cap);
|
||||
}
|
||||
|
||||
[[maybe_unused]]
|
||||
@ -87,8 +91,8 @@ struct SmallVector<T, 0> {
|
||||
constexpr void moveItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept {
|
||||
}
|
||||
|
||||
constexpr void clearItems(AllocAlias<T> *items) noexcept {
|
||||
delete[] items;
|
||||
constexpr void clearItems(T *items, std::size_t n) noexcept {
|
||||
m_allocator.deallocate(items, n);
|
||||
}
|
||||
|
||||
};
|
||||
@ -382,7 +386,7 @@ Vector<T, SmallVectorSize>::Vector(Vector &&other) noexcept {
|
||||
template<typename T, std::size_t SmallVectorSize>
|
||||
Vector<T, SmallVectorSize>::~Vector() {
|
||||
clear();
|
||||
this->clearItems(reinterpret_cast<AllocAlias<T>*>(m_items));
|
||||
this->clearItems(m_items, m_cap);
|
||||
m_items = nullptr;
|
||||
}
|
||||
|
||||
@ -403,7 +407,7 @@ template<typename T, std::size_t SmallVectorSize>
|
||||
constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(const Vector &other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
this->clearItems(reinterpret_cast<AllocAlias<T>*>(m_items));
|
||||
this->clearItems(m_items, m_cap);
|
||||
m_items = nullptr;
|
||||
m_size = other.m_size;
|
||||
m_cap = other.m_cap;
|
||||
@ -419,7 +423,7 @@ template<typename T, std::size_t SmallVectorSize>
|
||||
constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(Vector &&other) noexcept {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
this->clearItems(reinterpret_cast<AllocAlias<T>*>(m_items));
|
||||
this->clearItems(m_items, m_cap);
|
||||
m_size = other.m_size;
|
||||
m_cap = other.m_cap;
|
||||
m_items = other.m_items;
|
||||
@ -604,7 +608,7 @@ void Vector<T, SmallVectorSize>::expandCap(std::size_t cap) {
|
||||
for (std::size_t i = itRange; i < m_cap; i++) {
|
||||
new (&m_items[i]) T;
|
||||
}
|
||||
this->clearItems(reinterpret_cast<AllocAlias<T>*>(oldItems));
|
||||
this->clearItems(m_items, m_cap);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user