[ox/std] Rename Vector alloc and dealloc methods

This commit is contained in:
Gary Talent 2021-11-08 02:25:51 -06:00
parent 46fe5c3b8f
commit 22f08f83c5

View File

@ -32,7 +32,7 @@ struct SmallVector {
SmallVector(SmallVector&) noexcept = default;
SmallVector(SmallVector&&) noexcept = default;
protected:
constexpr void initItems(T **items, std::size_t cap) noexcept {
constexpr void allocate(T **items, std::size_t cap) noexcept {
if (cap <= Size) {
*items = reinterpret_cast<T*>(m_data);
} else {
@ -62,9 +62,9 @@ struct SmallVector {
}
}
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);
constexpr void deallocate(T *items, std::size_t cap) noexcept {
if (items && static_cast<void*>(items) != static_cast<void*>(m_data)) {
m_allocator.deallocate(items, cap);
}
}
@ -79,7 +79,7 @@ struct SmallVector<T, 0> {
SmallVector(SmallVector&) noexcept = default;
SmallVector(SmallVector&&) noexcept = default;
protected:
constexpr void initItems(T **items, std::size_t cap) noexcept {
constexpr void allocate(T **items, std::size_t cap) noexcept {
*items = m_allocator.allocate(cap);
}
@ -91,8 +91,10 @@ struct SmallVector<T, 0> {
constexpr void moveItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept {
}
constexpr void clearItems(T *items, std::size_t n) noexcept {
m_allocator.deallocate(items, n);
constexpr void deallocate(T *items, std::size_t cap) noexcept {
if (items) {
m_allocator.deallocate(items, cap);
}
}
};
@ -349,7 +351,7 @@ template<typename T, std::size_t SmallVectorSize>
constexpr Vector<T, SmallVectorSize>::Vector(std::size_t size) noexcept {
m_size = size;
m_cap = m_size;
this->initItems(&m_items, m_cap);
this->allocate(&m_items, m_cap);
for (std::size_t i = 0; i < size; ++i) {
m_items[i] = {};
}
@ -366,7 +368,7 @@ template<typename T, std::size_t SmallVectorSize>
constexpr Vector<T, SmallVectorSize>::Vector(const Vector &other) {
m_size = other.m_size;
m_cap = other.m_cap;
this->initItems(&m_items, other.m_cap);
this->allocate(&m_items, other.m_cap);
for (std::size_t i = 0; i < m_size; ++i) {
new (&m_items[i]) T(other.m_items[i]);
}
@ -386,7 +388,7 @@ constexpr Vector<T, SmallVectorSize>::Vector(Vector &&other) noexcept {
template<typename T, std::size_t SmallVectorSize>
Vector<T, SmallVectorSize>::~Vector() {
clear();
this->clearItems(m_items, m_cap);
this->deallocate(m_items, m_cap);
m_items = nullptr;
}
@ -407,11 +409,11 @@ template<typename T, std::size_t SmallVectorSize>
constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(const Vector &other) {
if (this != &other) {
clear();
this->clearItems(m_items, m_cap);
this->deallocate(m_items, m_cap);
m_items = nullptr;
m_size = other.m_size;
m_cap = other.m_cap;
this->initItems(&m_items, other.m_cap);
this->allocate(&m_items, other.m_cap);
for (std::size_t i = 0; i < m_size; i++) {
m_items[i] = other.m_items[i];
}
@ -423,7 +425,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(m_items, m_cap);
this->deallocate(m_items, m_cap);
m_size = other.m_size;
m_cap = other.m_cap;
m_items = other.m_items;
@ -599,7 +601,7 @@ template<typename T, std::size_t SmallVectorSize>
constexpr void Vector<T, SmallVectorSize>::expandCap(std::size_t cap) {
auto oldItems = m_items;
m_cap = cap;
this->initItems(&m_items, cap);
this->allocate(&m_items, cap);
if (oldItems) { // move over old items
const auto itRange = cap > m_size ? m_size : cap;
for (std::size_t i = 0; i < itRange; i++) {
@ -608,7 +610,7 @@ constexpr 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(m_items, m_cap);
this->deallocate(oldItems, m_cap);
}
}