diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 6fb2ada4..9e76e556 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -38,6 +38,17 @@ struct SmallVector { } } + constexpr void moveConstructItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { + if (cap <= Size) { + const auto dstItems = bit_cast(m_data); + const auto srcItems = bit_cast(src.m_data); + for (auto i = 0u; i < count; ++i) { + new (&dstItems[i]) T(move(srcItems[i])); + } + *items = bit_cast(m_data); + } + } + constexpr void moveItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { if (cap <= Size) { const auto dstItems = bit_cast(m_data); @@ -68,6 +79,10 @@ struct SmallVector { *items = bit_cast(new AllocAlias[cap]); } + [[maybe_unused]] + constexpr void moveConstructItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept { + } + [[maybe_unused]] constexpr void moveItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept { } @@ -284,7 +299,7 @@ class Vector: detail::SmallVector { void insert(std::size_t pos, const T &val); template - void emplace_back(Args&&... args); + T &emplace_back(Args&&... args); void push_back(const T &item); @@ -349,7 +364,7 @@ Vector::Vector(const Vector &other) { m_cap = other.m_cap; this->initItems(&m_items, other.m_cap); for (std::size_t i = 0; i < m_size; ++i) { - m_items[i] = other.m_items[i]; + new (&m_items[i]) T(other.m_items[i]); } } @@ -358,7 +373,7 @@ Vector::Vector(Vector &&other) noexcept { m_size = other.m_size; m_cap = other.m_cap; m_items = other.m_items; - this->moveItemsFrom(&m_items, other, m_size, m_cap); + this->moveConstructItemsFrom(&m_items, other, m_size, m_cap); other.m_size = 0; other.m_cap = 0; other.m_items = nullptr; @@ -489,7 +504,7 @@ constexpr void Vector::resize(std::size_t size) { } if (m_size < size) { for (std::size_t i = m_size; i < size; i++) { - m_items[i] = {}; + new (&m_items[i]) T(); } } else { for (std::size_t i = size; i < m_size; i++) { @@ -524,12 +539,13 @@ void Vector::insert(std::size_t pos, const T &val) { template template -void Vector::emplace_back(Args&&... args) { +T &Vector::emplace_back(Args&&... args) { if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); } - new (&m_items[m_size]) T{forward(args)...}; + auto out = new (&m_items[m_size]) T{forward(args)...}; ++m_size; + return *out; } template