From 090af1b8097fecf6b09f3048811a44e11ece3242 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 3 Apr 2025 06:07:34 -0500 Subject: Test hash map extract pair, fix bugs. --- include/fud_hash_map.hpp | 16 ++++++------- include/fud_hash_map_impl.hpp | 8 ++++++- include/fud_vector.hpp | 53 ++++++++++++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 22 deletions(-) (limited to 'include') 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 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 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(m_valueData.data()); } - [[nodiscard]] Value&& takeValue() & + [[nodiscard]] constexpr Key&& takeKey() & + { + fudAssert(hasValue()); + return std::move(*std::bit_cast(m_keyData.data())); + } + + [[nodiscard]] constexpr Value&& takeValue() & { fudAssert(hasValue()); return std::move(*std::bit_cast(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)(T{})); - // static_assert(std::is_same_v); - auto status = Vector::initializeWithCapacity(output, count, allocator); if (status != FudStatus::Success) { return status; @@ -201,7 +198,7 @@ class Vector { return FudStatus::Success; } - static Result, FudStatus> from(const Vector& rhs, Option allocatorOption = NullOpt) + static Result, FudStatus> copy(const Vector& rhs, Option allocatorOption = NullOpt) { Allocator* allocator = nullptr; if (allocatorOption.hasValue()) { @@ -223,7 +220,7 @@ class Vector { return spanResult.takeError(); } Vector 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 - static Result, FudStatus> from(Span& rhs, Allocator* allocator) + static Result, FudStatus> from(Span rhs, Allocator* allocator) { Vector output{}; auto status = initializeFromSpan(output, rhs, allocator); @@ -242,7 +239,7 @@ class Vector { } template - static FudStatus initializeFromSpan(Vector& output, Span& rhs, Allocator* allocator) + static FudStatus initializeFromSpan(Vector& output, Span rhs, Allocator* allocator) { auto status = Vector::initializeWithCapacity(output, rhs.size(), allocator); if (status != FudStatus::Success) { @@ -259,7 +256,38 @@ class Vector { return Vector{std::move(rhs)}; } - FudStatus copy(const Vector& rhs); + FudStatus clone(const Vector& 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& 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&& rhs); @@ -280,20 +308,19 @@ class Vector { Result, FudStatus> span() const { - using RetType = Result, 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, FudStatus> span() { using RetType = Result, 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, FudStatus> span(size_t count) const -- cgit v1.2.3