From a651d45a2f5cecb7fafd46d1d12fed3637ff681f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 22 May 2024 23:13:52 -0500 Subject: [PATCH] [ox/std] Fix Vector insert functions --- deps/ox/src/ox/std/vector.hpp | 48 +++++++++++++---------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 5787db69..a3f763de 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -271,7 +271,7 @@ class Vector: detail::VectorAllocator { constexpr bool contains(MaybeView_t const&) const noexcept; constexpr iterator insert( - std::size_t pos, std::size_t cnt, T val) noexcept(useNoexcept); + std::size_t pos, std::size_t cnt, T const&val) noexcept(useNoexcept); constexpr iterator insert(std::size_t pos, T val) noexcept(useNoexcept); @@ -531,29 +531,23 @@ constexpr bool Vector::contains(MaybeView_t co template constexpr typename Vector::template iterator Vector::insert( - std::size_t pos, std::size_t cnt, T val) noexcept(useNoexcept) { + std::size_t pos, std::size_t cnt, T const&val) noexcept(useNoexcept) { if (m_size + cnt > m_cap) { reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt); - if (pos < m_size) { - m_items[pos] = std::move(val); - } else { - for (auto i = 0u; i < cnt; ++i) { - std::construct_at(&m_items[pos + i], m_items[pos]); - } + } + if (pos < m_size) { + for (auto i = m_size + cnt - 1; i > pos; --i) { + std::construct_at(&m_items[i], std::move(m_items[i - cnt])); + } + for (auto i = pos; i < pos + cnt; ++i) { + m_items[i] = val; } } else { - if (pos < m_size) { - for (auto i = m_size + cnt - 1; i > pos; --i) { - std::construct_at(&m_items[i], std::move(m_items[i - cnt])); - } - m_items[pos] = std::move(val); - } else { - for (auto i = 0u; i < cnt; ++i) { - std::construct_at(&m_items[pos + i], m_items[pos]); - } + for (auto i = 0u; i < cnt; ++i) { + std::construct_at(&m_items[pos + i], m_items[pos]); } } - ++m_size; + m_size += cnt; return begin() + pos; } @@ -562,20 +556,14 @@ constexpr typename Vector::template iterator::insert(std::size_t pos, T val) noexcept(useNoexcept) { if (m_size == m_cap) { reserveInsert(m_cap ? m_cap * 2 : initialCap, pos); - if (pos < m_size) { - m_items[pos] = std::move(val); - } else { - std::construct_at(&m_items[pos], m_items[pos]); + } + if (pos < m_size) { + for (auto i = m_size; i > pos; --i) { + std::construct_at(&m_items[i], std::move(m_items[i - 1])); } + m_items[pos] = std::move(val); } else { - if (pos < m_size) { - for (auto i = m_size; i > pos; --i) { - std::construct_at(&m_items[i], std::move(m_items[i - 1])); - } - m_items[pos] = std::move(val); - } else { - std::construct_at(&m_items[pos], m_items[pos]); - } + std::construct_at(&m_items[pos], std::move(val)); } ++m_size; return begin() + pos;