[ox/std] Make most of Vector constexpr

This commit is contained in:
Gary Talent 2021-11-06 12:52:16 -05:00
parent 263ed4f7e7
commit 9a8bb557a6

View File

@ -217,13 +217,13 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
public:
constexpr Vector() noexcept = default;
explicit Vector(std::size_t size) noexcept;
explicit constexpr Vector(std::size_t size) noexcept;
Vector(std::initializer_list<T> list) noexcept;
constexpr Vector(std::initializer_list<T> list) noexcept;
Vector(const Vector &other);
constexpr Vector(const Vector &other);
Vector(Vector &&other) noexcept;
constexpr Vector(Vector &&other) noexcept;
~Vector();
@ -259,7 +259,7 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
return iterator<const T&, const T*, true>(m_items, MaxValue<size_type>, m_size);
}
bool operator==(const Vector &other) const;
constexpr bool operator==(const Vector &other) const;
constexpr Vector &operator=(const Vector &other);
@ -281,9 +281,9 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
constexpr std::size_t size() const noexcept;
[[nodiscard]]
bool empty() const noexcept;
constexpr bool empty() const noexcept;
void clear();
constexpr void clear();
constexpr void resize(std::size_t size);
@ -298,16 +298,16 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
}
[[nodiscard]]
bool contains(const T&) const;
constexpr bool contains(const T&) const;
void insert(std::size_t pos, const T &val);
constexpr void insert(std::size_t pos, const T &val);
template<typename... Args>
T &emplace_back(Args&&... args);
constexpr T &emplace_back(Args&&... args);
void push_back(const T &item);
constexpr void push_back(const T &item);
void pop_back();
constexpr void pop_back();
/**
* Removes an item from the Vector.
@ -315,14 +315,14 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
* @return Error if index is out of bounds
*/
template<typename RefType, typename RefPtr, bool reverse>
Error erase(const iterator<RefType, RefPtr, reverse> &pos);
constexpr Error erase(const iterator<RefType, RefPtr, reverse> &pos);
/**
* Removes an item from the Vector.
* @param pos position of item to remove
* @return Error if index is out of bounds
*/
Error erase(std::size_t pos);
constexpr Error erase(std::size_t pos);
/**
* Moves the last item in the Vector to position pos and decrements the
@ -330,10 +330,10 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
* @param pos position of item to remove
* @return Error if index is out of bounds
*/
Error unordered_erase(std::size_t pos);
constexpr Error unordered_erase(std::size_t pos);
private:
void expandCap(std::size_t cap);
constexpr void expandCap(std::size_t cap);
};
@ -341,12 +341,12 @@ template<typename T, std::size_t SmallVectorSize, typename RefType, bool reverse
using VectorIt = typename Vector<T, SmallVectorSize>::template iterator<RefType, reverse>;
template<typename T, std::size_t SmallVectorSize, typename RefType, bool reverse>
VectorIt<T, SmallVectorSize, RefType, reverse> operator+(std::size_t n, const VectorIt<T, SmallVectorSize, RefType, reverse> &a) {
constexpr VectorIt<T, SmallVectorSize, RefType, reverse> operator+(std::size_t n, const VectorIt<T, SmallVectorSize, RefType, reverse> &a) {
return a + n;
}
template<typename T, std::size_t SmallVectorSize>
Vector<T, SmallVectorSize>::Vector(std::size_t size) noexcept {
constexpr Vector<T, SmallVectorSize>::Vector(std::size_t size) noexcept {
m_size = size;
m_cap = m_size;
this->initItems(&m_items, m_cap);
@ -356,14 +356,14 @@ Vector<T, SmallVectorSize>::Vector(std::size_t size) noexcept {
}
template<typename T, std::size_t SmallVectorSize>
Vector<T, SmallVectorSize>::Vector(std::initializer_list<T> list) noexcept {
constexpr Vector<T, SmallVectorSize>::Vector(std::initializer_list<T> list) noexcept {
for (auto &item : list) {
emplace_back(item);
}
}
template<typename T, std::size_t SmallVectorSize>
Vector<T, SmallVectorSize>::Vector(const Vector &other) {
constexpr Vector<T, SmallVectorSize>::Vector(const Vector &other) {
m_size = other.m_size;
m_cap = other.m_cap;
this->initItems(&m_items, other.m_cap);
@ -373,7 +373,7 @@ Vector<T, SmallVectorSize>::Vector(const Vector &other) {
}
template<typename T, std::size_t SmallVectorSize>
Vector<T, SmallVectorSize>::Vector(Vector &&other) noexcept {
constexpr Vector<T, SmallVectorSize>::Vector(Vector &&other) noexcept {
m_size = other.m_size;
m_cap = other.m_cap;
m_items = other.m_items;
@ -391,7 +391,7 @@ Vector<T, SmallVectorSize>::~Vector() {
}
template<typename T, std::size_t SmallVectorSize>
bool Vector<T, SmallVectorSize>::operator==(const Vector &other) const {
constexpr bool Vector<T, SmallVectorSize>::operator==(const Vector &other) const {
if (m_size != other.m_size) {
return false;
}
@ -487,12 +487,12 @@ constexpr std::size_t Vector<T, SmallVectorSize>::size() const noexcept {
}
template<typename T, std::size_t SmallVectorSize>
bool Vector<T, SmallVectorSize>::empty() const noexcept {
constexpr bool Vector<T, SmallVectorSize>::empty() const noexcept {
return !m_size;
}
template<typename T, std::size_t SmallVectorSize>
void Vector<T, SmallVectorSize>::clear() {
constexpr void Vector<T, SmallVectorSize>::clear() {
if constexpr(is_class<T>()) {
for (std::size_t i = 0; i < m_size; ++i) {
m_items[i].~T();
@ -519,7 +519,7 @@ constexpr void Vector<T, SmallVectorSize>::resize(std::size_t size) {
}
template<typename T, std::size_t SmallVectorSize>
bool Vector<T, SmallVectorSize>::contains(const T &v) const {
constexpr bool Vector<T, SmallVectorSize>::contains(const T &v) const {
for (std::size_t i = 0; i < m_size; i++) {
if (m_items[i] == v) {
return true;
@ -529,7 +529,7 @@ bool Vector<T, SmallVectorSize>::contains(const T &v) const {
}
template<typename T, std::size_t SmallVectorSize>
void Vector<T, SmallVectorSize>::insert(std::size_t pos, const T &val) {
constexpr void Vector<T, SmallVectorSize>::insert(std::size_t pos, const T &val) {
// TODO: insert should ideally have its own expandCap
if (m_size == m_cap) {
expandCap(m_cap ? m_cap * 2 : 100);
@ -543,7 +543,7 @@ void Vector<T, SmallVectorSize>::insert(std::size_t pos, const T &val) {
template<typename T, std::size_t SmallVectorSize>
template<typename... Args>
T &Vector<T, SmallVectorSize>::emplace_back(Args&&... args) {
constexpr T &Vector<T, SmallVectorSize>::emplace_back(Args&&... args) {
if (m_size == m_cap) {
expandCap(m_cap ? m_cap * 2 : 100);
}
@ -553,7 +553,7 @@ T &Vector<T, SmallVectorSize>::emplace_back(Args&&... args) {
}
template<typename T, std::size_t SmallVectorSize>
void Vector<T, SmallVectorSize>::push_back(const T &item) {
constexpr void Vector<T, SmallVectorSize>::push_back(const T &item) {
if (m_size == m_cap) {
expandCap(m_cap ? m_cap * 2 : 100);
}
@ -562,19 +562,19 @@ void Vector<T, SmallVectorSize>::push_back(const T &item) {
}
template<typename T, std::size_t SmallVectorSize>
void Vector<T, SmallVectorSize>::pop_back() {
constexpr void Vector<T, SmallVectorSize>::pop_back() {
--m_size;
m_items[m_size].~T();
}
template<typename T, std::size_t SmallVectorSize>
template<typename RefType, typename RefPtr, bool reverse>
Error Vector<T, SmallVectorSize>::erase(const iterator<RefType, RefPtr, reverse> &pos) {
constexpr Error Vector<T, SmallVectorSize>::erase(const iterator<RefType, RefPtr, reverse> &pos) {
return erase(pos.offset());
}
template<typename T, std::size_t SmallVectorSize>
Error Vector<T, SmallVectorSize>::erase(std::size_t pos) {
constexpr Error Vector<T, SmallVectorSize>::erase(std::size_t pos) {
if (pos >= m_size) {
return OxError(1);
}
@ -586,7 +586,7 @@ Error Vector<T, SmallVectorSize>::erase(std::size_t pos) {
}
template<typename T, std::size_t SmallVectorSize>
Error Vector<T, SmallVectorSize>::unordered_erase(std::size_t pos) {
constexpr Error Vector<T, SmallVectorSize>::unordered_erase(std::size_t pos) {
if (pos >= m_size) {
return OxError(1);
}
@ -596,7 +596,7 @@ Error Vector<T, SmallVectorSize>::unordered_erase(std::size_t pos) {
}
template<typename T, std::size_t SmallVectorSize>
void Vector<T, SmallVectorSize>::expandCap(std::size_t cap) {
constexpr void Vector<T, SmallVectorSize>::expandCap(std::size_t cap) {
auto oldItems = m_items;
m_cap = cap;
this->initItems(&m_items, cap);