Skip to content

Commit

Permalink
FindKeyIndex for fixed unordered map
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunday111 committed Nov 15, 2024
1 parent f5db581 commit 2b5e744
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions include/ass/fixed_unordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <type_traits>

#include "fixed_bitset.hpp"
#include "invalid_index.hpp"

namespace ass
{
Expand Down Expand Up @@ -85,7 +86,7 @@ class FixedUnorderedMap
constexpr bool Contains(const Key key) const
{
const size_t index = FindKeyIndex(key);
return index != capacity && has_index_.Get(index);
return index != kInvalidIndex && has_index_.Get(index);
}

static constexpr size_t Capacity() noexcept
Expand All @@ -96,22 +97,32 @@ class FixedUnorderedMap
constexpr Value& Get(const Key key)
{
const size_t index = FindKeyIndex(key);
assert(index != capacity && has_index_.Get(index));
assert(index != kInvalidIndex && has_index_.Get(index));
return values_[index];
}

constexpr const Value& Get(const Key key) const
constexpr size_t FindKeyIndex(const Key key) const
{
const size_t index = FindKeyIndex(key);
assert(index != capacity && has_index_.Get(index));
constexpr bool stop_at_deleted = false;
return FindIndexForKey<stop_at_deleted>(key);
}

constexpr const Value& GetAtIndex(const size_t index) const
{
assert(index < capacity && has_index_.Get(index));
return values_[index];
}

constexpr const Value& Get(const Key key) const
{
return GetAtIndex(FindKeyIndex(key));
}

template <typename... Args>
constexpr Value* TryEmplace(const Key key, Args&&... args)
{
const size_t index = FindFreeIndexForKey(key);
if (index == capacity)
if (index == kInvalidIndex)
{
return nullptr;
}
Expand All @@ -134,7 +145,7 @@ class FixedUnorderedMap
constexpr Value* TryAdd(const Key key, std::optional<Value> value = std::nullopt)
{
const size_t index = FindFreeIndexForKey(key);
if (index == capacity)
if (index == kInvalidIndex)
{
return nullptr;
}
Expand Down Expand Up @@ -170,7 +181,7 @@ class FixedUnorderedMap
constexpr std::optional<Value> Remove(const Key key)
{
const size_t index = FindKeyIndex(key);
if (index != capacity && has_index_.Set(index, false))
if (index != kInvalidIndex && has_index_.Set(index, false))
{
was_deleted_.Set(index, true);
return std::move(values_[index]);
Expand Down Expand Up @@ -240,12 +251,6 @@ class FixedUnorderedMap
return It(*this_, capacity);
}

constexpr size_t FindKeyIndex(const Key key) const
{
constexpr bool stop_at_deleted = false;
return FindIndexForKey<stop_at_deleted>(key);
}

constexpr size_t FindFreeIndexForKey(const Key key) const
{
constexpr bool stop_at_deleted = true;
Expand Down Expand Up @@ -282,7 +287,7 @@ class FixedUnorderedMap
}
}

return capacity;
return kInvalidIndex;
}

constexpr bool HasValueAtIndex(size_t index) const noexcept
Expand Down

0 comments on commit 2b5e744

Please sign in to comment.