Common: Make SmallVector work with non-standard-layout types.

This commit is contained in:
Jordan Woyak 2025-01-25 19:59:18 -06:00
parent 56b7b0a804
commit 9777e8e76b

View File

@ -3,8 +3,8 @@
#pragma once #pragma once
#include <array> #include <array>
#include <cassert>
#include <cstddef> #include <cstddef>
#include <type_traits>
#include <utility> #include <utility>
namespace Common namespace Common
@ -14,39 +14,99 @@ namespace Common
template <typename T, size_t MaxSize> template <typename T, size_t MaxSize>
class SmallVector final class SmallVector final
{ {
static_assert(std::is_standard_layout_v<T> == true, "Type must be a standard layout type");
public: public:
using value_type = T;
SmallVector() = default; SmallVector() = default;
explicit SmallVector(size_t size) : m_size(size) {} explicit SmallVector(size_t new_size) { resize(new_size); }
void push_back(const T& x) { m_array[m_size++] = x; } ~SmallVector() { clear(); }
void push_back(T&& x) { m_array[m_size++] = std::move(x); }
template <typename... Args> SmallVector(const SmallVector& other)
T& emplace_back(Args&&... args)
{ {
return m_array[m_size++] = T{std::forward<Args>(args)...}; for (auto& value : other)
emplace_back(value);
} }
T& operator[](size_t i) { return m_array[i]; } SmallVector& operator=(const SmallVector& rhs)
const T& operator[](size_t i) const { return m_array[i]; } {
clear();
for (auto& value : rhs)
emplace_back(value);
return *this;
}
auto data() { return m_array.data(); } SmallVector(SmallVector&& other)
auto begin() { return m_array.begin(); } {
auto end() { return m_array.begin() + m_size; } for (auto& value : other)
emplace_back(std::move(value));
other.clear();
}
auto data() const { return m_array.data(); } SmallVector& operator=(SmallVector&& rhs)
auto begin() const { return m_array.begin(); } {
auto end() const { return m_array.begin() + m_size; } clear();
for (auto& value : rhs)
emplace_back(std::move(value));
rhs.clear();
return *this;
}
void push_back(const value_type& x) { emplace_back(x); }
void push_back(value_type&& x) { emplace_back(std::move(x)); }
template <typename... Args>
value_type& emplace_back(Args&&... args)
{
assert(m_size < MaxSize);
return *::new (&m_array[m_size++ * sizeof(value_type)]) value_type{std::forward<Args>(args)...};
}
void pop_back()
{
assert(m_size > 0);
std::destroy_at(data() + --m_size);
}
value_type& operator[](size_t i)
{
assert(i < m_size);
return data()[i];
}
const value_type& operator[](size_t i) const
{
assert(i < m_size);
return data()[i];
}
auto data() { return std::launder(reinterpret_cast<value_type*>(m_array.data())); }
auto begin() { return data(); }
auto end() { return data() + m_size; }
auto data() const { return std::launder(reinterpret_cast<const value_type*>(m_array.data())); }
auto begin() const { return data(); }
auto end() const { return data() + m_size; }
size_t capacity() const { return MaxSize; }
size_t size() const { return m_size; } size_t size() const { return m_size; }
bool empty() const { return m_size == 0; } bool empty() const { return m_size == 0; }
void clear() { m_size = 0; } void resize(size_t new_size)
{
assert(new_size <= MaxSize);
while (size() < new_size)
emplace_back();
while (size() > new_size)
pop_back();
}
void clear() { resize(0); }
private: private:
std::array<T, MaxSize> m_array{}; alignas(value_type) std::array<std::byte, MaxSize * sizeof(value_type)> m_array;
size_t m_size = 0; size_t m_size = 0;
}; };