From cb9fa588ba8144fcdd52ba4b83d69d93fb18066f Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 30 Mar 2025 23:08:43 -0500 Subject: Add hash map. --- include/fud_vector.hpp | 62 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 16 deletions(-) (limited to 'include/fud_vector.hpp') diff --git a/include/fud_vector.hpp b/include/fud_vector.hpp index 0a9abf3..fd01cd0 100644 --- a/include/fud_vector.hpp +++ b/include/fud_vector.hpp @@ -33,14 +33,20 @@ namespace fud { +/** \brief Vector storing elements of T using a custom allocator. */ template class Vector { static constexpr size_t ElementSize = sizeof(T); static constexpr size_t Alignment = alignof(T); public: + /** \brief Construct a vector implictly using the globalFudAllocator. */ constexpr Vector() noexcept = default; - constexpr explicit Vector(Allocator& allocator) noexcept : m_allocator{&allocator} {} + + /** \brief Construct a vector explicitly using the specified allocator. */ + constexpr explicit Vector(Allocator& allocator) noexcept : m_allocator{&allocator} + { + } constexpr Vector(const Vector& rhs) = delete; constexpr Vector(Vector&& rhs) noexcept : m_allocator(rhs.m_allocator), m_data(rhs.m_data), m_length{rhs.m_length}, m_capacity{rhs.m_capacity} @@ -77,7 +83,8 @@ class Vector { return *this; } - static constexpr Vector NullVector() noexcept { + static constexpr Vector NullVector() noexcept + { Vector output{}; output.m_allocator = &globalNullAllocator; return output; @@ -134,10 +141,7 @@ class Vector { return Okay>{std::move(output)}; } - static FudStatus initializeWithSize( - Vector& output, - size_t count, - Allocator* allocator = &globalFudAllocator) + static FudStatus initializeWithSize(Vector& output, size_t count, Allocator* allocator = &globalFudAllocator) { if (output.m_data != nullptr) { return FudStatus::AlreadyInitialized; @@ -224,7 +228,7 @@ class Vector { if (status != FudStatus::Success) { return status; } - return output; + return Okay{std::move(output)}; } template @@ -235,7 +239,7 @@ class Vector { if (status != FudStatus::Success) { return status; } - return output; + return Okay{std::move(output)}; } template @@ -251,6 +255,30 @@ class Vector { } } + template + static Result, FudStatus> from(Option allocatorOpt, Args&&... args) + { + constexpr size_t size = sizeof...(args); + Vector output{}; + Allocator* allocator = allocatorOpt.hasValue() ? allocatorOpt.value() : &globalFudAllocator; + auto status = Vector::initializeWithCapacity(output, size, allocator); + if (status != FudStatus::Success) { + return Error{status}; + } + output.m_length = size; + size_t index = 0; + /* + for (size_t index = 0; index < output.m_length; ++index) { + + } + */ + ([&]() { + output.m_data[index] = std::forward(args); + ++index; + } (), ...); + return Okay{std::move(output)}; + } + static Vector move(Vector&& rhs) noexcept { return Vector{std::move(rhs)}; @@ -270,6 +298,11 @@ class Vector { return m_capacity; } + [[nodiscard]] bool empty() const + { + return m_length == 0; + } + Result, FudStatus> span() const { using RetType = Result, FudStatus>; @@ -367,7 +400,7 @@ class Vector { auto status = FudStatus::Success; if (m_capacity > 0) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(m_data), m_capacity); + m_allocator->deallocate(reinterpret_cast(m_data), m_capacity * ElementSize); } m_data = dataPtr; @@ -647,8 +680,7 @@ class Vector { if (std::numeric_limits::max() - Size < m_length) { return FudStatus::Failure; } - if (m_length + Size > m_capacity) - { + if (m_length + Size > m_capacity) { size_t currentLength = m_length; auto status = resize(m_length + Size); m_length = currentLength; @@ -674,8 +706,7 @@ class Vector { if (std::numeric_limits::max() - span.size() < m_length) { return FudStatus::Failure; } - if (m_length + span.size() > m_capacity) - { + if (m_length + span.size() > m_capacity) { size_t currentLength = m_length; auto status = resize(m_length + span.size()); m_length = currentLength; @@ -700,8 +731,7 @@ class Vector { } m_data[index].~T(); - for (size_t fwdIndex = index; fwdIndex + 1 < m_length; fwdIndex++) - { + for (size_t fwdIndex = index; fwdIndex + 1 < m_length; fwdIndex++) { m_data[fwdIndex] = std::move(m_data[fwdIndex + 1]); } m_data[m_length - 1].~T(); @@ -743,7 +773,7 @@ class Vector { if (m_data != nullptr && m_allocator != nullptr) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(m_data), m_capacity); + m_allocator->deallocate(reinterpret_cast(m_data), m_capacity * ElementSize); } m_allocator = nullptr; -- cgit v1.2.3