[ox/std] Add Vector::emplace

This commit is contained in:
Gary Talent 2023-01-14 19:42:17 -06:00
parent 5fa614ab83
commit 4cb6992178

View File

@ -342,9 +342,12 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
[[nodiscard]]
constexpr bool contains(const T&) const;
constexpr void insert(std::size_t pos, std::size_t cnt, const T &val);
constexpr iterator<> insert(std::size_t pos, std::size_t cnt, const T &val);
constexpr void insert(std::size_t pos, const T &val);
constexpr iterator<> insert(std::size_t pos, const T &val);
template<typename... Args>
constexpr iterator<> emplace(std::size_t pos, Args&&... args);
template<typename... Args>
constexpr T &emplace_back(Args&&... args);
@ -571,7 +574,8 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(const T &v) const
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, std::size_t cnt, const T &val) {
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, std::size_t cnt, const T &val) {
// TODO: insert should ideally have its own reserve
if (m_size + cnt > m_cap) {
reserve(m_cap ? m_size + cnt : initialSize);
@ -587,10 +591,12 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, st
}
}
++m_size;
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) {
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) {
// TODO: insert should ideally have its own reserve
if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialSize);
@ -604,6 +610,26 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, co
std::construct_at(&m_items[pos], val);
}
++m_size;
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
template<typename... Args>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::emplace(std::size_t pos, Args&&... args) {
// TODO: insert should ideally have its own reserve
if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialSize);
}
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].~T();
}
std::construct_at(&m_items[pos], ox::forward<Args>(args)...);
++m_size;
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>