[ox/std] Make Vector::erase return iterator item after removed item

This commit is contained in:
Gary Talent 2021-11-29 03:33:13 -06:00
parent 1f24912ddd
commit a94051644b
3 changed files with 22 additions and 14 deletions

View File

@ -80,7 +80,7 @@ struct Exception: public std::exception {
uint16_t line = 0;
ErrorCode errCode = 0;
explicit inline Exception(const char *file, uint32_t line, ErrorCode errCode, const char *msg = nullptr) noexcept {
explicit inline Exception(const char *file, uint32_t line, ErrorCode errCode, const char *msg = "") noexcept {
this->file = file;
this->line = line;
this->msg = msg;
@ -88,7 +88,11 @@ struct Exception: public std::exception {
}
inline explicit Exception(const Error &err) {
if (err.msg) {
this->msg = err.msg;
} else {
this->msg = "";
}
this->file = err.file;
this->line = err.line;
this->errCode = err.errCode;
@ -121,7 +125,7 @@ struct [[nodiscard]] Result {
constexpr Result(const type &value, const Error &error = OxError(0)) noexcept: value(const_cast<type&>(value)), error(error) {
}
constexpr Result(type &&value, const Error &error = OxError(0)) noexcept: value(move(value)), error(error) {
constexpr Result(type &&value, const Error &error = OxError(0)) noexcept: value(std::move(value)), error(error) {
}
explicit constexpr operator const type&() const noexcept {
@ -148,7 +152,7 @@ struct [[nodiscard]] Result {
}
constexpr Error moveTo(type *val) noexcept {
*val = move(value);
*val = std::move(value);
return error;
}
@ -170,6 +174,8 @@ constexpr Error toError(const Result<T> &ve) noexcept {
}
constexpr void oxIgnoreError(const ox::Error&) noexcept {}
template<typename T>
constexpr void oxIgnoreError(const ox::Result<T>&) noexcept {}
#if __cplusplus >= 202002L
#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error
#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] throw ox::Exception(_ox_error)

View File

@ -182,7 +182,7 @@ void HashMap<K, T>::erase(const K &k) {
h = hash(hashStr, 8) % m_pairs.size();
}
}
m_keys.erase(ox::find(m_keys.cbegin(), m_keys.cend(), k));
m_keys.erase(ox::find(m_keys.begin(), m_keys.end(), k));
}
template<typename K, typename T>

View File

@ -8,6 +8,8 @@
#pragma once
#include <iostream>
#include "bit.hpp"
#include "initializerlist.hpp"
#include "iterator.hpp"
@ -116,6 +118,8 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
size_type m_max = 0;
public:
constexpr iterator() noexcept = default;
constexpr iterator(PtrType t, size_type offset, size_type max) noexcept {
m_t = t;
m_offset = offset;
@ -316,15 +320,14 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
* @param pos iterator at the point to remove
* @return Error if index is out of bounds
*/
template<typename RefType, typename RefPtr, bool reverse>
constexpr Error erase(const iterator<RefType, RefPtr, reverse> &pos);
constexpr Result<iterator<>> erase(const iterator<> &pos);
/**
* Removes an item from the Vector.
* @param pos position of item to remove
* @return Error if index is out of bounds
*/
constexpr Error erase(std::size_t pos);
constexpr Result<iterator<>> erase(std::size_t pos);
/**
* Moves the last item in the Vector to position pos and decrements the
@ -570,21 +573,20 @@ constexpr void Vector<T, SmallVectorSize>::pop_back() {
}
template<typename T, std::size_t SmallVectorSize>
template<typename RefType, typename RefPtr, bool reverse>
constexpr Error Vector<T, SmallVectorSize>::erase(const iterator<RefType, RefPtr, reverse> &pos) {
constexpr Result<typename Vector<T, SmallVectorSize>::template iterator<>> Vector<T, SmallVectorSize>::erase(const iterator<> &pos) {
return erase(pos.offset());
}
template<typename T, std::size_t SmallVectorSize>
constexpr Error Vector<T, SmallVectorSize>::erase(std::size_t pos) {
constexpr Result<typename Vector<T, SmallVectorSize>::template iterator<>> Vector<T, SmallVectorSize>::erase(std::size_t pos) {
if (pos >= m_size) {
return OxError(1);
return OxError(1, "Vector::erase failed: pos is greater than Vector size");
}
--m_size;
for (auto i = pos; i < m_size; ++i) {
m_items[i] = move(m_items[i + 1]);
m_items[i] = std::move(m_items[i + 1]);
}
return OxError(0);
return begin() + pos;
}
template<typename T, std::size_t SmallVectorSize>