Skip to content

Commit 6c35f64

Browse files
author
Julian LALU
committed
Improve hashset hashmap
1 parent 6592109 commit 6c35f64

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

interface/core/containers/hashset.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ namespace hud
842842
explicit constexpr hashset_impl() noexcept = default;
843843

844844
constexpr explicit hashset_impl(const allocator_type &allocator) noexcept
845-
: compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple())
845+
: compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(), hud::forward_as_tuple())
846846
{
847847
}
848848

@@ -861,7 +861,7 @@ namespace hud
861861
constexpr explicit hashset_impl(const hashset_impl<u_storage_t, u_hasher_t, u_key_equal_t, u_allocator_t> &other, const allocator_type &allocator) noexcept
862862
: max_slot_count_ {other.max_count()}
863863
, count_ {other.count()}
864-
, compressed_ {hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(other.free_slot_before_grow_compressed())}
864+
, compressed_ {hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(), hud::forward_as_tuple(other.free_slot_before_grow_compressed())}
865865
{
866866
copy_construct(other);
867867
}
@@ -876,7 +876,7 @@ namespace hud
876876
constexpr explicit hashset_impl(const hashset_impl<u_storage_t, u_hasher_t, u_key_equal_t, u_allocator_t> &other, usize extra_max_count, const allocator_type &allocator) noexcept
877877
: max_slot_count_ {normalize_max_count(other.max_count() + extra_max_count)}
878878
, count_ {other.count()}
879-
, compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(max_slot_before_grow(max_slot_count_) - count_))
879+
, compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(), hud::forward_as_tuple(max_slot_before_grow(max_slot_count_) - count_))
880880
{
881881
copy_construct(other, extra_max_count);
882882
}
@@ -891,7 +891,7 @@ namespace hud
891891
constexpr explicit hashset_impl(hashset_impl<u_storage_t, u_hasher_t, u_key_equal_t, u_allocator_t> &&other, const allocator_type &allocator) noexcept
892892
: max_slot_count_ {other.max_count()}
893893
, count_ {other.count()}
894-
, compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(other.free_slot_before_grow_compressed()))
894+
, compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(), hud::forward_as_tuple(other.free_slot_before_grow_compressed()))
895895
{
896896
move_construct(hud::forward<hashset_impl<u_storage_t, u_hasher_t, u_key_equal_t, u_allocator_t>>(other));
897897
}
@@ -906,7 +906,7 @@ namespace hud
906906
constexpr explicit hashset_impl(hashset_impl<u_storage_t, u_hasher_t, u_key_equal_t, u_allocator_t> &&other, usize extra_max_count, const allocator_type &allocator) noexcept
907907
: max_slot_count_ {normalize_max_count(other.max_count() + extra_max_count)}
908908
, count_ {other.count()}
909-
, compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(max_slot_before_grow(max_slot_count_) - count_))
909+
, compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), hud::forward_as_tuple(), hud::forward_as_tuple(), hud::forward_as_tuple(max_slot_before_grow(max_slot_count_) - count_))
910910
{
911911
move_construct(hud::forward<hashset_impl<u_storage_t, u_hasher_t, u_key_equal_t, u_allocator_t>>(other), extra_max_count);
912912
}
@@ -1247,7 +1247,7 @@ namespace hud
12471247
{
12481248
usize slot_index_that_match_h2 {slot_index + group_index_that_match_h2 & max_slot_count_};
12491249
slot_type *slot_that_match_h2 {slot_ptr_ + slot_index_that_match_h2};
1250-
if (key_equal_(slot_that_match_h2->key(), hud::forward<K>(key))) [[likely]]
1250+
if (key_equal()(slot_that_match_h2->key(), hud::forward<K>(key))) [[likely]]
12511251
{
12521252
return iterator {control_ptr_ + slot_index_that_match_h2, slot_that_match_h2};
12531253
}
@@ -1626,7 +1626,7 @@ namespace hud
16261626
{
16271627
usize slot_index_that_match_h2 {slot_index + group_index_that_match_h2 & max_slot_count_};
16281628
slot_type *slot_that_match_h2 {slot_ptr_ + slot_index_that_match_h2};
1629-
if (key_equal_(slot_that_match_h2->key(), key)) [[likely]]
1629+
if (key_equal()(slot_that_match_h2->key(), key)) [[likely]]
16301630
{
16311631
return {
16321632
iterator {control_ptr_ + slot_index_that_match_h2, slot_that_match_h2},
@@ -1953,12 +1953,12 @@ namespace hud
19531953

19541954
[[nodiscard]] constexpr usize &free_slot_before_grow_compressed() noexcept
19551955
{
1956-
return hud::get<2>(compressed_);
1956+
return hud::get<3>(compressed_);
19571957
}
19581958

19591959
[[nodiscard]] constexpr const usize &free_slot_before_grow_compressed() const noexcept
19601960
{
1961-
return hud::get<2>(compressed_);
1961+
return hud::get<3>(compressed_);
19621962
}
19631963

19641964
[[nodiscard]] constexpr allocator_type &allocator_mut() noexcept
@@ -1976,14 +1976,24 @@ namespace hud
19761976
return hud::get<1>(compressed_);
19771977
}
19781978

1979+
[[nodiscard]] constexpr key_equal_type &key_equal() noexcept
1980+
{
1981+
return hud::get<2>(compressed_);
1982+
}
1983+
1984+
[[nodiscard]] constexpr const key_equal_type &key_equal() const noexcept
1985+
{
1986+
return hud::get<2>(compressed_);
1987+
}
1988+
19791989
private:
19801990
/** Max count of slot in the map. Always a power of two mask value. */
19811991
usize max_slot_count_ {0};
19821992

19831993
/** The count of values in the hashmap. */
19841994
usize count_ {0};
19851995

1986-
hud::compressed_tuple<allocator_type, hasher_type, usize> compressed_ {hud::tag_init};
1996+
hud::compressed_tuple<allocator_type, hasher_type, key_equal_type, usize> compressed_ {hud::tag_init};
19871997

19881998
/** The allocator. */
19891999
// allocator_type allocator_;
@@ -1992,7 +2002,7 @@ namespace hud
19922002
// hasher_type hasher_;
19932003

19942004
/** The key equal function. */
1995-
key_equal_type key_equal_;
2005+
// key_equal_type key_equal_;
19962006

19972007
/** The control of the hashmap. Initialized to sentinel. */
19982008
control_type *control_ptr_ {const_cast<control_type *>(&INIT_GROUP[16])};

test/hashmap/hashmap_misc.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,12 @@ GTEST_TEST(hashmap, sizeof_map_is_correct)
138138

139139
// hashmp is compressed, all empty element like allocator, comparator, hasher must be 0 size
140140
// This test must be updated if the member of hashmap is changed
141-
constexpr usize allocator_size = sizeof(hud::hashmap<i32, i32>::allocator_type);
142-
constexpr usize hasher_size = sizeof(hud::hashmap<i32, i32>::hasher_type);
143-
constexpr usize equal_size = sizeof(hud::hashmap<i32, i32>::key_equal_type);
144141
constexpr usize control_ptr_size = sizeof(void *);
145142
constexpr usize slot_ptr_size = sizeof(void *);
146-
constexpr usize count_and_max_count_and_free_slot_before_grow_size = 3 * sizeof(usize);
143+
constexpr usize count_size = sizeof(usize);
144+
constexpr usize max_count_size = sizeof(usize);
145+
constexpr usize free_slot_before_grow_size = sizeof(usize);
147146

148147
constexpr usize sizeof_map = sizeof(hud::hashmap<i32, i32>);
149-
// hud_assert_true(sizeof_map == 24);
150-
// hud_assert_true(sizeof_map <= (allocator_size + hasher_size + equal_size + control_ptr_size + slot_ptr_size + count_and_max_count_and_free_slot_before_grow_size));
151-
}
148+
hud_assert_true(sizeof_map == control_ptr_size + slot_ptr_size + count_size + max_count_size + free_slot_before_grow_size);
149+
}

test/hashset/hashset_misc.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,20 @@ GTEST_TEST(hashset, count_return_count_of_element)
127127
hud_assert_true(std::get<2>(result));
128128
}
129129
}
130+
131+
GTEST_TEST(hashset, sizeof_map_is_correct)
132+
{
133+
// Size of hashmap do not depends of the key and value types
134+
hud_assert_true(sizeof(hud::hashset<i32>) == sizeof(hud::hashset<i64>));
135+
136+
// hashmp is compressed, all empty element like allocator, comparator, hasher must be 0 size
137+
// This test must be updated if the member of hashmap is changed
138+
constexpr usize control_ptr_size = sizeof(void *);
139+
constexpr usize slot_ptr_size = sizeof(void *);
140+
constexpr usize count_size = sizeof(usize);
141+
constexpr usize max_count_size = sizeof(usize);
142+
constexpr usize free_slot_before_grow_size = sizeof(usize);
143+
144+
constexpr usize sizeof_map = sizeof(hud::hashset<i32>);
145+
hud_assert_true(sizeof_map == control_ptr_size + slot_ptr_size + count_size + max_count_size + free_slot_before_grow_size);
146+
}

0 commit comments

Comments
 (0)