Skip to content

Commit

Permalink
Continue hashmap implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian LALU committed Jan 1, 2025
1 parent 305ba47 commit f08c2a0
Show file tree
Hide file tree
Showing 7 changed files with 1,993 additions and 220 deletions.
50 changes: 48 additions & 2 deletions interface/core/containers/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ namespace hud
{

template<typename key_t, typename value_t>
struct slot
class slot
: hud::pair<key_t, value_t>
{
public:
using super = hud::pair<key_t, value_t>;
using super::super;
using type = super;
using key_type = typename hud::pair<key_t, value_t>::first_type;
using value_type = typename hud::pair<key_t, value_t>::second_type;

using super::super;

explicit constexpr slot(const key_type &key) noexcept
: super(key, value_type {})
{
Expand Down Expand Up @@ -100,7 +102,51 @@ namespace hud
/** Type of the key. */
using typename super::key_type;
/** Type of the value. */
using super::add;
using super::reserve;
using super::super;
using typename super::allocator_type;
using typename super::const_iterator;
using typename super::iterator;
using typename super::value_type;
explicit constexpr hashmap() noexcept = default;

constexpr explicit hashmap(const allocator_type &allocator) noexcept
: super(allocator)
{
}

constexpr hashmap(std::initializer_list<hud::pair<key_t, value_t>> list, const allocator_type &allocator = allocator_type()) noexcept
: super(allocator)
{
reserve(list.size());
for (auto &pair : list)
{
add(pair);
}
}

/**
* Insert a key in the hashset.
* @param key The key associated with the `value`
* @param args List of arguments pass to `value_type` constructor after the `key` itself
* @return Iterator to the `value`
*/
constexpr iterator add(const hud::pair<key_t, value_t> &pair) noexcept
{
return add(pair.first, pair.second);
}

/**
* Insert a key in the hashset.
* @param key The key associated with the `value`
* @param args List of arguments pass to `value_type` constructor after the `key` itself
* @return Iterator to the `value`
*/
constexpr iterator add(hud::pair<key_t, value_t> &&pair) noexcept
{
return add(hud::move(pair.first), hud::move(pair.second));
}
};
} // namespace hud

Expand Down
62 changes: 40 additions & 22 deletions interface/core/containers/hashset.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,30 @@ namespace hud
namespace details::hashset
{
template<typename value_t>
struct slot
class slot
{
public:
using type = value_t;
using key_type = value_t;
using value_type = value_t;

[[nodiscard]] constexpr const key_type &key() const noexcept
explicit constexpr slot(const key_type &key) noexcept
: key_(key)
{
return value_;
}

[[nodiscard]] constexpr const value_type &value() const noexcept
explicit constexpr slot(key_type &&key) noexcept
: key_(std::move(key))
{
return value_;
}

[[nodiscard]] constexpr value_type &value() noexcept
[[nodiscard]] constexpr const key_type &key() const noexcept
{
return value_;
return key_;
}

value_t value_;
private:
type key_;
};

template<typename key_t>
Expand Down Expand Up @@ -423,16 +425,26 @@ namespace hud
constexpr iterator(control_type *control_ptr)
: control_ptr_(control_ptr)
{
hud::check(control_ptr != nullptr);
HD_ASSUME((control_ptr != nullptr));
hud::check(control_ptr_ != nullptr);
HD_ASSUME((control_ptr_ != nullptr));
}

constexpr iterator(control_type *control_ptr, slot_type *slot_ptr)
: control_ptr_(control_ptr)
, slot_ptr_(slot_ptr)
{
hud::check(control_ptr != nullptr);
HD_ASSUME(control_ptr != nullptr);
hud::check(control_ptr_ != nullptr);
HD_ASSUME(control_ptr_ != nullptr);
hud::check(slot_ptr_ != nullptr);
HD_ASSUME(slot_ptr_ != nullptr);
}

constexpr iterator(hud::pair<control_type *, slot_type *> &&ctrl_slot_ptr)
: control_ptr_(ctrl_slot_ptr.first)
, slot_ptr_(ctrl_slot_ptr.second)
{
hud::check(control_ptr_ != nullptr);
HD_ASSUME(control_ptr_ != nullptr);
hud::check(slot_ptr_ != nullptr);
HD_ASSUME(slot_ptr_ != nullptr);
}
Expand Down Expand Up @@ -523,6 +535,14 @@ namespace hud
using memory_allocation_type = typename allocator_type::template memory_allocation_type<slot_type>;

public:
/** Default constructor. */
explicit constexpr hashset_impl() noexcept = default;

constexpr explicit hashset_impl(const allocator_type &allocator) noexcept
: allocator_(allocator)
{
}

constexpr ~hashset_impl() noexcept
{
clear_shrink();
Expand Down Expand Up @@ -722,26 +742,24 @@ namespace hud
/** Retrieves an iterator to the end of the array. */
[[nodiscard]] constexpr iterator begin() noexcept
{
return find_first_full();
return iterator {find_first_full()};
}

/** Retrieves an iterator to the end of the array. */
[[nodiscard]]
constexpr const_iterator begin() const noexcept
[[nodiscard]] constexpr const_iterator begin() const noexcept

{
return find_first_full();
return const_iterator(find_first_full());
}

/** Retrieves an iterator to the end of the array. */
[[nodiscard]]
constexpr iterator end() noexcept
[[nodiscard]] constexpr iterator end() noexcept
{
return iterator(control_ptr_sentinel());
}

/** Retrieves an iterator to the end of the array. */
[[nodiscard]]
constexpr const_iterator end() const noexcept
[[nodiscard]] constexpr const_iterator end() const noexcept
{
return const_iterator(control_ptr_sentinel());
}
Expand Down Expand Up @@ -910,7 +928,7 @@ namespace hud
}

[[nodiscard]]
constexpr iterator find_first_full() noexcept
constexpr hud::pair<control_type *, slot_type *> find_first_full() const noexcept
{
hud::check(hud::bits::is_valid_power_of_two_mask(max_slot_count_) && "Not a mask");
usize slot_index = 0;
Expand All @@ -927,7 +945,7 @@ namespace hud
// Advance to next group (Maybe a control iterator that iterate over groups can be better alternative)
slot_index += group_type::SLOT_PER_GROUP;
}
return end();
return {control_ptr_sentinel(), nullptr};
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ set( src
hash/hash_64.cpp
hashmap/hashmap_add.cpp
hashmap/hashmap_constructors.cpp
hashmap/hashmap_misc.cpp
hashmap/hashmap_find.cpp
hashmap/hashmap_iterator.cpp
hashmap/hashmap_misc.cpp
hashset/hashset_add.cpp
hashset/hashset_constructors.cpp
hashset/hashset_misc.cpp
Expand Down
2 changes: 1 addition & 1 deletion test/array/array_constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,11 +1403,11 @@ GTEST_TEST(array, construct_by_copying_raw_data_array_of_non_bitwise_copy_constr

GTEST_TEST(array, construct_with_initializer_list_of_bitwise_copy_constructible_same_type)
{

using type = i32;

static_assert(hud::is_bitwise_copy_constructible_v<type>);

// Test with default allocator and no extra
{
auto test_default_allocator = [](std::initializer_list<type> initializer)
{
Expand Down
Loading

0 comments on commit f08c2a0

Please sign in to comment.