[ox/std] Add Vector::emplace
This commit is contained in:
parent
5fa614ab83
commit
4cb6992178
34
deps/ox/src/ox/std/vector.hpp
vendored
34
deps/ox/src/ox/std/vector.hpp
vendored
@ -342,9 +342,12 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr bool contains(const T&) const;
|
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>
|
template<typename... Args>
|
||||||
constexpr T &emplace_back(Args&&... 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>
|
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
|
// TODO: insert should ideally have its own reserve
|
||||||
if (m_size + cnt > m_cap) {
|
if (m_size + cnt > m_cap) {
|
||||||
reserve(m_cap ? m_size + cnt : initialSize);
|
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;
|
++m_size;
|
||||||
|
return begin() + pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, std::size_t SmallVectorSize, typename Allocator>
|
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
|
// TODO: insert should ideally have its own reserve
|
||||||
if (m_size == m_cap) {
|
if (m_size == m_cap) {
|
||||||
reserve(m_cap ? m_cap * 2 : initialSize);
|
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);
|
std::construct_at(&m_items[pos], val);
|
||||||
}
|
}
|
||||||
++m_size;
|
++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>
|
template<typename T, std::size_t SmallVectorSize, typename Allocator>
|
||||||
|
Loading…
Reference in New Issue
Block a user