[ox/std] Make many Vector functions conditionally noexcept

This commit is contained in:
Gary Talent 2023-06-18 01:39:35 -05:00
parent 7a0312ba91
commit 7437487340

View File

@ -235,7 +235,7 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
};
private:
static constexpr auto initialSize = SmallVectorSize > 0 ? SmallVectorSize : 50;
static constexpr auto initialCap = SmallVectorSize > 0 ? SmallVectorSize : 50;
std::size_t m_size = 0;
std::size_t m_cap = 0;
T *m_items = nullptr;
@ -247,7 +247,7 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
constexpr Vector(std::initializer_list<T> list) noexcept;
constexpr Vector(const Vector &other);
constexpr Vector(const Vector &other) noexcept(ox::is_integral_v<T>);
constexpr Vector(Vector &&other) noexcept;
@ -311,9 +311,9 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
return iterator<const T&, const T*, true>(m_items, MaxValue<size_type>, m_size);
}
constexpr bool operator==(const Vector &other) const;
constexpr bool operator==(const Vector &other) const noexcept(ox::is_integral_v<T>);
constexpr Vector &operator=(const Vector &other);
constexpr Vector &operator=(const Vector &other) noexcept(ox::is_integral_v<T>);
constexpr Vector &operator=(Vector &&other) noexcept;
@ -339,9 +339,9 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
[[nodiscard]]
constexpr bool empty() const noexcept;
constexpr void clear();
constexpr void clear() noexcept(ox::is_integral_v<T>);
constexpr void resize(std::size_t size);
constexpr void resize(std::size_t size) noexcept(ox::is_integral_v<T>);
[[nodiscard]]
constexpr T *data() noexcept {
@ -354,35 +354,36 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
}
[[nodiscard]]
constexpr bool contains(const T&) const;
constexpr bool contains(const T&) const noexcept(ox::is_integral_v<T>);
constexpr iterator<> 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) noexcept(ox::is_integral_v<T>);
constexpr iterator<> insert(std::size_t pos, const T &val);
constexpr iterator<> insert(std::size_t pos, const T &val) noexcept(ox::is_integral_v<T>);
template<typename... Args>
constexpr iterator<> emplace(std::size_t pos, Args&&... args);
constexpr iterator<> emplace(std::size_t pos, Args&&... args) noexcept(ox::is_integral_v<T>);
template<typename... Args>
constexpr T &emplace_back(Args&&... args);
constexpr T &emplace_back(Args&&... args) noexcept(ox::is_integral_v<T>);
constexpr void push_back(const T &item);
constexpr void push_back(const T &item) noexcept(ox::is_integral_v<T>);
constexpr void pop_back();
constexpr void pop_back() noexcept(ox::is_integral_v<T>);
/**
* Removes an item from the Vector.
* @param pos iterator at the point to remove
* @return Error if index is out of bounds
*/
constexpr Result<iterator<>> erase(const iterator<> &pos);
constexpr Result<iterator<>> erase(const iterator<> &pos) noexcept(ox::is_integral_v<T>);
/**
* Removes an item from the Vector.
* @param pos position of item to remove
* @return Error if index is out of bounds
*/
constexpr Result<iterator<>> erase(std::size_t pos);
constexpr Result<iterator<>> erase(std::size_t pos) noexcept(ox::is_integral_v<T>);
/**
* Moves the last item in the Vector to position pos and decrements the
@ -390,12 +391,13 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
* @param pos position of item to remove
* @return Error if index is out of bounds
*/
constexpr Error unordered_erase(std::size_t pos);
constexpr Error unordered_erase(std::size_t pos) noexcept(ox::is_integral_v<T>);
constexpr void reserve(std::size_t cap);
constexpr void reserve(std::size_t cap) noexcept(ox::is_integral_v<T>);
private:
constexpr void reserveInsert(std::size_t cap, std::size_t pos, std::size_t offset = 1);
constexpr void reserveInsert(
std::size_t cap, std::size_t pos, std::size_t offset = 1) noexcept(ox::is_integral_v<T>);
};
@ -427,7 +429,7 @@ constexpr Vector<T, SmallVectorSize, Allocator>::Vector(std::initializer_list<T>
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr Vector<T, SmallVectorSize, Allocator>::Vector(const Vector &other) {
constexpr Vector<T, SmallVectorSize, Allocator>::Vector(const Vector &other) noexcept(ox::is_integral_v<T>) {
m_size = other.m_size;
m_cap = other.m_cap;
this->allocate(&m_items, other.m_cap);
@ -455,7 +457,8 @@ constexpr Vector<T, SmallVectorSize, Allocator>::~Vector() {
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr bool Vector<T, SmallVectorSize, Allocator>::operator==(const Vector &other) const {
constexpr bool Vector<T, SmallVectorSize, Allocator>::operator==(
const Vector &other) const noexcept(ox::is_integral_v<T>) {
if (m_size != other.m_size) {
return false;
}
@ -469,7 +472,7 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::operator==(const Vector &o
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr Vector<T, SmallVectorSize, Allocator> &Vector<T, SmallVectorSize, Allocator>::operator=(
const Vector &other) {
const Vector &other) noexcept(ox::is_integral_v<T>) {
if (this != &other) {
clear();
this->deallocate(m_items, m_cap);
@ -554,7 +557,7 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::empty() const noexcept {
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::clear() {
constexpr void Vector<T, SmallVectorSize, Allocator>::clear() noexcept(ox::is_integral_v<T>) {
if constexpr(is_class<T>()) {
for (std::size_t i = 0; i < m_size; ++i) {
m_items[i].~T();
@ -564,7 +567,7 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::clear() {
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::resize(std::size_t size) {
constexpr void Vector<T, SmallVectorSize, Allocator>::resize(std::size_t size) noexcept(ox::is_integral_v<T>) {
if (m_cap < size) {
reserve(size * 2);
}
@ -581,7 +584,7 @@ constexpr void Vector<T, SmallVectorSize, Allocator>::resize(std::size_t size) {
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(const T &v) const {
constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(const T &v) const noexcept(ox::is_integral_v<T>) {
for (std::size_t i = 0; i < m_size; i++) {
if (m_items[i] == v) {
return true;
@ -592,9 +595,10 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(const T &v) const
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, std::size_t cnt, const T &val) {
Vector<T, SmallVectorSize, Allocator>::insert(
std::size_t pos, std::size_t cnt, const T &val) noexcept(ox::is_integral_v<T>) {
if (m_size + cnt > m_cap) {
reserveInsert(m_cap ? m_size + cnt : initialSize, pos, cnt);
reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt);
if (pos < m_size) {
m_items[pos] = val;
} else {
@ -620,9 +624,9 @@ Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, std::size_t cnt,
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<>
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) {
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) noexcept(ox::is_integral_v<T>) {
if (m_size == m_cap) {
reserveInsert(m_cap ? m_cap * 2 : initialSize, pos);
reserveInsert(m_cap ? m_cap * 2 : initialCap, pos);
if (pos < m_size) {
m_items[pos] = val;
} else {
@ -645,9 +649,9 @@ Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, const T &val) {
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) {
Vector<T, SmallVectorSize, Allocator>::emplace(std::size_t pos, Args&&... args) noexcept(ox::is_integral_v<T>) {
if (m_size == m_cap) {
reserveInsert(m_cap ? m_cap * 2 : initialSize);
reserveInsert(m_cap ? m_cap * 2 : initialCap);
if (pos < m_size) {
m_items[pos].~T();
}
@ -667,9 +671,10 @@ Vector<T, SmallVectorSize, Allocator>::emplace(std::size_t pos, Args&&... args)
template<typename T, std::size_t SmallVectorSize, typename Allocator>
template<typename... Args>
constexpr T &Vector<T, SmallVectorSize, Allocator>::emplace_back(Args&&... args) {
constexpr T &Vector<T, SmallVectorSize, Allocator>::emplace_back(Args&&... args)
noexcept(ox::is_integral_v<T>) {
if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialSize);
reserve(m_cap ? m_cap * 2 : initialCap);
}
auto out = std::construct_at(&m_items[m_size], ox::forward<Args>(args)...);
++m_size;
@ -677,29 +682,29 @@ constexpr T &Vector<T, SmallVectorSize, Allocator>::emplace_back(Args&&... args)
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::push_back(const T &item) {
constexpr void Vector<T, SmallVectorSize, Allocator>::push_back(const T &item) noexcept(ox::is_integral_v<T>) {
if (m_size == m_cap) {
reserve(m_cap ? m_cap * 2 : initialSize);
reserve(m_cap ? m_cap * 2 : initialCap);
}
std::construct_at(&m_items[m_size], item);
++m_size;
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::pop_back() {
constexpr void Vector<T, SmallVectorSize, Allocator>::pop_back() noexcept(ox::is_integral_v<T>) {
--m_size;
m_items[m_size].~T();
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr Result<typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false>>
Vector<T, SmallVectorSize, Allocator>::erase(const iterator<> &pos) {
Vector<T, SmallVectorSize, Allocator>::erase(const iterator<> &pos) noexcept(ox::is_integral_v<T>) {
return erase(pos.offset());
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr Result<typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false>>
Vector<T, SmallVectorSize, Allocator>::erase(std::size_t pos) {
Vector<T, SmallVectorSize, Allocator>::erase(std::size_t pos) noexcept(ox::is_integral_v<T>) {
if (pos >= m_size) {
return OxError(1, "Vector::erase failed: pos is greater than Vector size");
}
@ -712,7 +717,8 @@ Vector<T, SmallVectorSize, Allocator>::erase(std::size_t pos) {
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr Error Vector<T, SmallVectorSize, Allocator>::unordered_erase(std::size_t pos) {
constexpr Error Vector<T, SmallVectorSize, Allocator>::unordered_erase(std::size_t pos)
noexcept(ox::is_integral_v<T>) {
if (pos >= m_size) {
return OxError(1);
}
@ -723,7 +729,7 @@ constexpr Error Vector<T, SmallVectorSize, Allocator>::unordered_erase(std::size
}
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::reserve(std::size_t cap) {
constexpr void Vector<T, SmallVectorSize, Allocator>::reserve(std::size_t cap) noexcept(ox::is_integral_v<T>) {
if (cap <= m_cap) {
return;
}
@ -745,7 +751,7 @@ template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr void Vector<T, SmallVectorSize, Allocator>::reserveInsert(
std::size_t cap,
std::size_t pos,
std::size_t offset) {
std::size_t offset) noexcept(ox::is_integral_v<T>) {
if (cap <= m_cap) {
return;
}