diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2025-03-30 23:08:43 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2025-03-30 23:08:43 -0500 |
commit | cb9fa588ba8144fcdd52ba4b83d69d93fb18066f (patch) | |
tree | 214574ca68c1551ec76e7fbb9e0263793180231d /include/fud_vector.hpp | |
parent | 1d357adfa19725ee69fb267a363f1fd217b1272f (diff) |
Add hash map.
Diffstat (limited to 'include/fud_vector.hpp')
-rw-r--r-- | include/fud_vector.hpp | 62 |
1 files changed, 46 insertions, 16 deletions
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 <typename T> 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<T>& rhs) = delete; constexpr Vector(Vector<T>&& 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<T> NullVector() noexcept { + static constexpr Vector<T> NullVector() noexcept + { Vector<T> output{}; output.m_allocator = &globalNullAllocator; return output; @@ -134,10 +141,7 @@ class Vector { return Okay<Vector<T>>{std::move(output)}; } - static FudStatus initializeWithSize( - Vector<T>& output, - size_t count, - Allocator* allocator = &globalFudAllocator) + static FudStatus initializeWithSize(Vector<T>& 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 <size_t Size> @@ -235,7 +239,7 @@ class Vector { if (status != FudStatus::Success) { return status; } - return output; + return Okay{std::move(output)}; } template <size_t Size> @@ -251,6 +255,30 @@ class Vector { } } + template <typename... Args> + static Result<Vector<T>, FudStatus> from(Option<Allocator*> allocatorOpt, Args&&... args) + { + constexpr size_t size = sizeof...(args); + Vector<T> 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<T>(args); + ++index; + } (), ...); + return Okay{std::move(output)}; + } + static Vector<T> move(Vector<T>&& rhs) noexcept { return Vector<T>{std::move(rhs)}; @@ -270,6 +298,11 @@ class Vector { return m_capacity; } + [[nodiscard]] bool empty() const + { + return m_length == 0; + } + Result<Span<const T>, FudStatus> span() const { using RetType = Result<Span<const T>, 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<std::byte*>(m_data), m_capacity); + m_allocator->deallocate(reinterpret_cast<std::byte*>(m_data), m_capacity * ElementSize); } m_data = dataPtr; @@ -647,8 +680,7 @@ class Vector { if (std::numeric_limits<size_t>::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<size_t>::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<std::byte*>(m_data), m_capacity); + m_allocator->deallocate(reinterpret_cast<std::byte*>(m_data), m_capacity * ElementSize); } m_allocator = nullptr; |