[ox/std] Use std::construct_at in Vector instead placement new

This commit is contained in:
Gary Talent 2022-04-10 03:03:07 -05:00
parent cf7f9b9088
commit 9d74eca436

View File

@ -387,7 +387,7 @@ constexpr Vector<T, SmallVectorSize>::Vector(std::size_t size) noexcept {
m_cap = m_size; m_cap = m_size;
this->allocate(&m_items, m_cap); this->allocate(&m_items, m_cap);
for (std::size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
m_items[i] = {}; std::construct_at(&m_items[i]);
} }
} }
@ -404,7 +404,7 @@ constexpr Vector<T, SmallVectorSize>::Vector(const Vector &other) {
m_cap = other.m_cap; m_cap = other.m_cap;
this->allocate(&m_items, other.m_cap); this->allocate(&m_items, other.m_cap);
for (std::size_t i = 0; i < m_size; ++i) { for (std::size_t i = 0; i < m_size; ++i) {
new (&m_items[i]) T(other.m_items[i]); std::construct_at(&m_items[i], other.m_items[i]);
} }
} }
@ -449,7 +449,7 @@ constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(cons
m_cap = other.m_cap; m_cap = other.m_cap;
this->allocate(&m_items, other.m_cap); this->allocate(&m_items, other.m_cap);
for (std::size_t i = 0; i < m_size; i++) { for (std::size_t i = 0; i < m_size; i++) {
new (&m_items[i]) T(other.m_items[i]); std::construct_at(&m_items[i], other.m_items[i]);
} }
} }
return *this; return *this;
@ -544,7 +544,7 @@ constexpr void Vector<T, SmallVectorSize>::resize(std::size_t size) {
} }
if (m_size < size) { if (m_size < size) {
for (std::size_t i = m_size; i < size; i++) { for (std::size_t i = m_size; i < size; i++) {
new (&m_items[i]) T(); std::construct_at(&m_items[i]);
} }
} else { } else {
for (std::size_t i = size; i < m_size; i++) { for (std::size_t i = size; i < m_size; i++) {
@ -572,11 +572,11 @@ constexpr void Vector<T, SmallVectorSize>::insert(std::size_t pos, const T &val)
} }
if (pos < m_size) { if (pos < m_size) {
for (auto i = m_size; i > pos; --i) { for (auto i = m_size; i > pos; --i) {
new(&m_items[i]) T(std::move(m_items[i - 1])); std::construct_at(&m_items[i], std::move(m_items[i - 1]));
} }
m_items[pos] = val; m_items[pos] = val;
} else { } else {
new(&m_items[pos]) T(val); std::construct_at(&m_items[pos], val);
} }
++m_size; ++m_size;
} }
@ -587,7 +587,7 @@ constexpr T &Vector<T, SmallVectorSize>::emplace_back(Args&&... args) {
if (m_size == m_cap) { if (m_size == m_cap) {
expandCap(m_cap ? m_cap * 2 : initialSize); expandCap(m_cap ? m_cap * 2 : initialSize);
} }
auto out = new (&m_items[m_size]) T{forward<Args>(args)...}; auto out = std::construct_at(&m_items[m_size], forward<Args>(args)...);
++m_size; ++m_size;
return *out; return *out;
} }
@ -597,7 +597,7 @@ constexpr void Vector<T, SmallVectorSize>::push_back(const T &item) {
if (m_size == m_cap) { if (m_size == m_cap) {
expandCap(m_cap ? m_cap * 2 : initialSize); expandCap(m_cap ? m_cap * 2 : initialSize);
} }
new (&m_items[m_size]) T(item); std::construct_at(&m_items[m_size], item);
++m_size; ++m_size;
} }
@ -645,7 +645,7 @@ constexpr void Vector<T, SmallVectorSize>::expandCap(std::size_t cap) {
if (oldItems) { // move over old items if (oldItems) { // move over old items
const auto itRange = ox::min(cap, m_size); const auto itRange = ox::min(cap, m_size);
for (std::size_t i = 0; i < itRange; ++i) { for (std::size_t i = 0; i < itRange; ++i) {
new (&m_items[i]) T(std::move(oldItems[i])); std::construct_at(&m_items[i], std::move(oldItems[i]));
oldItems[i].~T(); oldItems[i].~T();
} }
this->deallocate(oldItems, oldCap); this->deallocate(oldItems, oldCap);