From c46ec57431cb68e868723df7d78cfc44cfab9d60 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 17 Apr 2021 12:44:27 -0500 Subject: [PATCH] [ox/std] Make Vector erase methods return Errors --- deps/ox/src/ox/std/vector.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 6b65c75e..27182188 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -84,15 +84,17 @@ class Vector { /** * Removes an item from the Vector. * @param pos position of item to remove + * @return Error if index is out of bounds */ - void erase(std::size_t pos); + Error erase(std::size_t pos); /** * Moves the last item in the Vector to position pos and decrements the * size by 1. * @param pos position of item to remove + * @return Error if index is out of bounds */ - void unordered_erase(std::size_t pos); + Error unordered_erase(std::size_t pos); private: void expandCap(std::size_t cap); @@ -301,17 +303,25 @@ void Vector::pop_back() { } template -void Vector::erase(std::size_t pos) { - m_size--; - for (auto i = pos; i < m_size; i++) { +Error Vector::erase(std::size_t pos) { + if (pos >= m_size) { + return OxError(1); + } + --m_size; + for (auto i = pos; i < m_size; ++i) { m_items[i] = ox::move(m_items[i + 1]); } + return OxError(0); } template -void Vector::unordered_erase(std::size_t pos) { +Error Vector::unordered_erase(std::size_t pos) { + if (pos >= m_size) { + return OxError(1); + } m_size--; m_items[pos] = ox::move(m_items[m_size]); + return OxError(0); } template