diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/fud_hash_map.hpp | 16 | ||||
-rw-r--r-- | include/fud_hash_map_impl.hpp | 8 | ||||
-rw-r--r-- | include/fud_vector.hpp | 53 |
3 files changed, 55 insertions, 22 deletions
diff --git a/include/fud_hash_map.hpp b/include/fud_hash_map.hpp index 9220876..ba9e0c2 100644 --- a/include/fud_hash_map.hpp +++ b/include/fud_hash_map.hpp @@ -200,7 +200,7 @@ class HashMap { if (hashIndexOption.isNone()) { return insert(key, value); } - auto hashIndex{hashIndexOption.take()}; + auto hashIndex{hashIndexOption.value()}; m_data[hashIndex].value() = value; return FudStatus::Success; } @@ -211,7 +211,7 @@ class HashMap { if (hashIndexOption.isNone()) { return insert(key, std::move(value)); } - auto hashIndex{hashIndexOption.take()}; + auto hashIndex{hashIndexOption.value()}; m_data[hashIndex].value() = std::move(value); return FudStatus::Success; } @@ -222,7 +222,7 @@ class HashMap { if (hashIndexOption.isNone()) { return insert(std::move(key), value); } - auto hashIndex{hashIndexOption.take()}; + auto hashIndex{hashIndexOption.value()}; m_data[hashIndex].value() = value; return FudStatus::Success; } @@ -233,7 +233,7 @@ class HashMap { if (hashIndexOption.isNone()) { return insert(std::move(key), std::move(value)); } - auto hashIndex{hashIndexOption.take()}; + auto hashIndex{hashIndexOption.value()}; m_data[hashIndex].value() = std::move(value); return FudStatus::Success; } @@ -290,7 +290,7 @@ class HashMap { return value; } - KeyValuePair extractPair(const Key& key) + Option<KeyValuePair> extractPair(const Key& key) { auto hashIndexOption = lookup(key); if (hashIndexOption.isNone()) { @@ -298,13 +298,13 @@ class HashMap { } auto hashIndex{hashIndexOption.value()}; - KeyValuePair kvPair{key, m_data[hashIndex].takeValue()}; + KeyValuePair kvPair{std::move(m_data[hashIndex].takeKey()), std::move(m_data[hashIndex].takeValue())}; m_data[hashIndex].tombstone(); return kvPair; } - KeyValuePair extractPair(Key&& key) + Option<KeyValuePair> extractPair(Key&& key) { auto hashIndexOption = lookup(key); if (hashIndexOption.isNone()) { @@ -312,7 +312,7 @@ class HashMap { } auto hashIndex{hashIndexOption.value()}; - KeyValuePair kvPair{std::move(key), m_data[hashIndex].takeValue()}; + KeyValuePair kvPair{std::move(key), std::move(m_data[hashIndex].takeValue())}; m_data[hashIndex].tombstone(); return kvPair; diff --git a/include/fud_hash_map_impl.hpp b/include/fud_hash_map_impl.hpp index 6e224f9..23f5653 100644 --- a/include/fud_hash_map_impl.hpp +++ b/include/fud_hash_map_impl.hpp @@ -151,7 +151,13 @@ struct MapEntry { return *std::bit_cast<Value*>(m_valueData.data()); } - [[nodiscard]] Value&& takeValue() & + [[nodiscard]] constexpr Key&& takeKey() & + { + fudAssert(hasValue()); + return std::move(*std::bit_cast<Key*>(m_keyData.data())); + } + + [[nodiscard]] constexpr Value&& takeValue() & { fudAssert(hasValue()); return std::move(*std::bit_cast<Value*>(m_valueData.data())); diff --git a/include/fud_vector.hpp b/include/fud_vector.hpp index 1ba2abf..b2827b4 100644 --- a/include/fud_vector.hpp +++ b/include/fud_vector.hpp @@ -181,9 +181,6 @@ class Vector { Builder&& builder, Allocator* allocator = &globalFudAllocator) { - // using BuilderResult = decltype(std::forward<Builder>(builder)(T{})); - // static_assert(std::is_same_v<BuilderResult, FudStatus>); - auto status = Vector::initializeWithCapacity(output, count, allocator); if (status != FudStatus::Success) { return status; @@ -201,7 +198,7 @@ class Vector { return FudStatus::Success; } - static Result<Vector<T>, FudStatus> from(const Vector<T>& rhs, Option<Allocator*> allocatorOption = NullOpt) + static Result<Vector<T>, FudStatus> copy(const Vector<T>& rhs, Option<Allocator*> allocatorOption = NullOpt) { Allocator* allocator = nullptr; if (allocatorOption.hasValue()) { @@ -223,7 +220,7 @@ class Vector { return spanResult.takeError(); } Vector<T> output{}; - auto status = Vector::initializeFromSpan(output, rhs.m_length, allocator); + auto status = Vector::initializeFromSpan(output, spanResult.takeOkay(), allocator); if (status != FudStatus::Success) { return status; } @@ -231,7 +228,7 @@ class Vector { } template <size_t Size> - static Result<Vector<T>, FudStatus> from(Span<const T, Size>& rhs, Allocator* allocator) + static Result<Vector<T>, FudStatus> from(Span<const T, Size> rhs, Allocator* allocator) { Vector<T> output{}; auto status = initializeFromSpan(output, rhs, allocator); @@ -242,7 +239,7 @@ class Vector { } template <size_t Size> - static FudStatus initializeFromSpan(Vector<T>& output, Span<const T, Size>& rhs, Allocator* allocator) + static FudStatus initializeFromSpan(Vector<T>& output, Span<const T, Size> rhs, Allocator* allocator) { auto status = Vector::initializeWithCapacity(output, rhs.size(), allocator); if (status != FudStatus::Success) { @@ -259,7 +256,38 @@ class Vector { return Vector<T>{std::move(rhs)}; } - FudStatus copy(const Vector<T>& rhs); + FudStatus clone(const Vector<T>& rhs) + { + fudAssert(&rhs != this); + auto cleanupStatus = cleanup(); + if (cleanupStatus != FudStatus::Success) { + return cleanupStatus; + } + + auto spanResult = rhs.span(); + if (spanResult.isError()) { + return spanResult.takeError(); + } + + return initializeFromSpan(*this, spanResult.takeOkay(), rhs.m_allocator); + } + + FudStatus copy(const Vector<T>& rhs) + { + fudAssert(&rhs != this); + auto* allocator = m_allocator; + auto cleanupStatus = cleanup(); + if (cleanupStatus != FudStatus::Success) { + return cleanupStatus; + } + + auto spanResult = rhs.span(); + if (spanResult.isError()) { + return spanResult.takeError(); + } + + return initializeFromSpan(*this, spanResult.takeOkay(), allocator); + } FudStatus take(Vector<T>&& rhs); @@ -280,20 +308,19 @@ class Vector { Result<Span<const T>, FudStatus> span() const { - using RetType = Result<Span<const T>, FudStatus>; if (m_data == nullptr) { - return RetType::error(FudStatus::ObjectInvalid); + return Error{FudStatus::ObjectInvalid}; } - return RetType::okay(Span{m_data, m_length}); + return Okay{Span{m_data, m_length}}; } Result<Span<T>, FudStatus> span() { using RetType = Result<Span<T>, FudStatus>; if (m_data == nullptr) { - return RetType::error(FudStatus::ObjectInvalid); + return Error{FudStatus::ObjectInvalid}; } - return RetType::okay(Span{m_data, m_length}); + return Okay{Span{m_data, m_length}}; } Result<Span<const T>, FudStatus> span(size_t count) const |