Skip to content

Commit 6592109

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

2 files changed

Lines changed: 43 additions & 53 deletions

File tree

interface/core/containers/compressed_tuple.h

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ namespace hud
9191

9292
/** Default constructor. Value-initialize content. */
9393
HD_FORCEINLINE constexpr compressed_tuple_leaf(hud::tag_init_t) noexcept
94-
: type_t()
94+
: compressed_tuple_leaf()
9595
{
96-
static_assert(hud::is_nothrow_default_constructible_v<type_t>, "type_t() default constructor is throwable. compressed_tuple is not designed to allow throwable default constructible components");
9796
}
9897

9998
/** Default constructor. Do not initializes content. */
@@ -127,26 +126,26 @@ namespace hud
127126
}
128127

129128
/**
130-
* Piecewise constructor forwarding a compressed_tuple of arguments.
131-
* @tparam Args Types of the elements in the compressed_tuple used for construction.
132-
* @param compressed_tuple Tuple of arguments to forward to the constructor of type_t.
129+
* Piecewise constructor forwarding a tuple of arguments.
130+
* @tparam Args Types of the elements in the tuple used for construction.
131+
* @param tuple Tuple of arguments to forward to the constructor of type_t.
133132
*/
134133
template<typename... Args>
135-
constexpr compressed_tuple_leaf(hud::tag_piecewise_construct_t, hud::compressed_tuple<Args...> &&compressed_tuple) noexcept
136-
: compressed_tuple_leaf(hud::forward<hud::compressed_tuple<Args...>>(compressed_tuple), hud::make_index_sequence_for<Args...> {})
134+
constexpr compressed_tuple_leaf(hud::tag_piecewise_construct_t, hud::tuple<Args...> &&tuple) noexcept
135+
: compressed_tuple_leaf(hud::forward<hud::tuple<Args...>>(tuple), hud::make_index_sequence_for<Args...> {})
137136
{
138137
}
139138

140139
/**
141-
* Piecewise constructor that unpacks a compressed_tuple into individual arguments.
140+
* Piecewise constructor that unpacks a tuple into individual arguments.
142141
*
143-
* @tparam Args Types of the elements in the compressed_tuple used for construction.
144-
* @tparam indices Index sequence used to extract each element from the compressed_tuple.
145-
* @param compressed_tuple Tuple whose elements are forwarded to the constructor of type_t.
142+
* @tparam Args Types of the elements in the tuple used for construction.
143+
* @tparam indices Index sequence used to extract each element from the tuple.
144+
* @param tuple Tuple whose elements are forwarded to the constructor of type_t.
146145
*/
147146
template<typename... Args, usize... indices>
148-
constexpr compressed_tuple_leaf(hud::compressed_tuple<Args...> &&compressed_tuple, hud::index_sequence<indices...>) noexcept
149-
: type_t(hud::forward<Args>(hud::get<indices>(compressed_tuple))...)
147+
constexpr compressed_tuple_leaf(hud::tuple<Args...> &&tuple, hud::index_sequence<indices...>) noexcept
148+
: type_t(hud::forward<Args>(hud::get<indices>(tuple))...)
150149
{
151150
static_assert(hud::is_nothrow_constructible_v<type_t, Args...>, "type_t(Args&&...) constructor is throwable. pair is not designed to allow throwable constructible components");
152151
}
@@ -278,25 +277,6 @@ namespace hud
278277
struct compressed_tuples_cat_impl
279278
: details::tuple::tuples_cat_impl<tuples_t...>
280279
{
281-
// using return_type = typename compressed_cat_tuples_arg<tuples_t...>::tuple_type;
282-
// using element_index_seq = typename details::tuple::cat_element_index<hud::make_index_sequence_for<>, tuples_t...>::element_index_seq;
283-
// using mask_index_seq = typename details::tuple::cat_mask_index<0, hud::make_index_sequence_for<>, tuples_t...>::mask_index_seq;
284-
285-
// /**
286-
// * Constructs a tuple that is a concatenation of all tuples in the given "tuple of tulpes" in the same order.
287-
// * @tparam tuple The tuple type of tuples to concatenate
288-
// * @tparam element_indices List element index
289-
// * @tparam mask_indices List index mask to match the element index hud::index_sequence
290-
// * @param hud::index_sequence of element_indices
291-
// * @param hud::index_sequence of mask_indices
292-
// * @param tulpe The tuple of tulpes to concatenate
293-
// * @return The concatenated tuple
294-
// */
295-
// template<typename tuple_t, usize... element_indices, usize... mask_indices>
296-
// static HD_FORCEINLINE constexpr return_type concatenate(hud::index_sequence<element_indices...>, hud::index_sequence<mask_indices...>, tuple_t &&tuple) noexcept
297-
// {
298-
// return return_type(get<element_indices>(get<mask_indices>(hud::forward<tuple>(tuple)))...);
299-
// }
300280
};
301281

302282
template<>
@@ -312,7 +292,7 @@ namespace hud
312292
* @return The concatenated tuple
313293
*/
314294
template<typename EmptyTuple, usize... element_indices, usize... mask_indices>
315-
static HD_FORCEINLINE constexpr EmptyTuple concatenate(hud::index_sequence<element_indices...>, hud::index_sequence<mask_indices...>, EmptyTuple &&) noexcept
295+
static constexpr EmptyTuple concatenate(hud::index_sequence<element_indices...>, hud::index_sequence<mask_indices...>, EmptyTuple &&) noexcept
316296
{
317297
return EmptyTuple();
318298
}
@@ -333,7 +313,7 @@ namespace hud
333313
* @return The concatenated tuple
334314
*/
335315
template<typename tuple_t>
336-
static HD_FORCEINLINE constexpr return_type concatenate(tuple_t &&tuple) noexcept
316+
static constexpr return_type concatenate(tuple_t &&tuple) noexcept
337317
{
338318
return tuples_cat_impl_type::concatenate(element_index_seq(), mask_index_seq(), hud::forward<tuple_t>(tuple));
339319
}
@@ -593,7 +573,7 @@ namespace hud
593573
* @param indices... The hud::index_sequence of indices
594574
*/
595575
template<typename tuple_t, usize... indices>
596-
HD_FORCEINLINE constexpr explicit compressed_tuple(tuple_t &&compressed_tuple, hud::index_sequence<indices...>) noexcept
576+
constexpr explicit compressed_tuple(tuple_t &&compressed_tuple, hud::index_sequence<indices...>) noexcept
597577
: super_type(hud::get<indices>(hud::forward<tuple_t>(compressed_tuple))...)
598578
{
599579
}
@@ -663,7 +643,7 @@ namespace hud
663643
* @return `T&` reference to the selected element.
664644
*/
665645
template<usize idx_to_reach, typename... types_t>
666-
[[nodiscard]] HD_FORCEINLINE constexpr tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &get(compressed_tuple<types_t...> &t) noexcept
646+
[[nodiscard]] constexpr tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &get(compressed_tuple<types_t...> &t) noexcept
667647
{
668648
using type_t = tuple_element_t<idx_to_reach, compressed_tuple<types_t...>>;
669649
return static_cast<typename details::compressed_tuple::tuple_leaf_select<idx_to_reach, type_t>::type &>(t).element();
@@ -677,7 +657,7 @@ namespace hud
677657
* @return `const T&` reference to the selected element.
678658
*/
679659
template<usize idx_to_reach, typename... types_t>
680-
[[nodiscard]] HD_FORCEINLINE constexpr const tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &get(const compressed_tuple<types_t...> &t) noexcept
660+
[[nodiscard]] constexpr const tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &get(const compressed_tuple<types_t...> &t) noexcept
681661
{
682662
using type_t = tuple_element_t<idx_to_reach, compressed_tuple<types_t...>>;
683663
return static_cast<const typename details::compressed_tuple::tuple_leaf_select<idx_to_reach, type_t>::type &>(t).element();
@@ -691,7 +671,7 @@ namespace hud
691671
* @return `T&&` reference to the selected element, preserving cv‑qualifiers.
692672
*/
693673
template<usize idx_to_reach, typename... types_t>
694-
[[nodiscard]] HD_FORCEINLINE constexpr tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &&get(compressed_tuple<types_t...> &&t) noexcept
674+
[[nodiscard]] constexpr tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &&get(compressed_tuple<types_t...> &&t) noexcept
695675
{
696676
using type_t = tuple_element_t<idx_to_reach, compressed_tuple<types_t...>>;
697677
return hud::forward<type_t>(static_cast<typename details::compressed_tuple::tuple_leaf_select<idx_to_reach, type_t>::type &&>(t).element());
@@ -705,7 +685,7 @@ namespace hud
705685
* @return `const T&&` reference to the selected element.
706686
*/
707687
template<usize idx_to_reach, typename... types_t>
708-
[[nodiscard]] HD_FORCEINLINE constexpr const tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &&get(const compressed_tuple<types_t...> &&t) noexcept
688+
[[nodiscard]] constexpr const tuple_element_t<idx_to_reach, compressed_tuple<types_t...>> &&get(const compressed_tuple<types_t...> &&t) noexcept
709689
{
710690
using type_t = tuple_element_t<idx_to_reach, compressed_tuple<types_t...>>;
711691
return hud::forward<const type_t>(static_cast<const typename details::compressed_tuple::tuple_leaf_select<idx_to_reach, type_t>::type &&>(t).element());

interface/core/containers/hashset.h

Lines changed: 24 additions & 14 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_ {allocator, 0}
845+
: compressed_(hud::tag_piecewise_construct, hud::forward_as_tuple(allocator), 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_ {allocator, 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(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_ {allocator, 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(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_ {allocator, 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(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_ {allocator, 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(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
}
@@ -1235,7 +1235,7 @@ namespace hud
12351235
[[nodiscard]]
12361236
constexpr iterator find_impl(K &&key) noexcept
12371237
{
1238-
u64 hash {hasher_(hud::forward<K>(key))};
1238+
u64 hash {hasher()(hud::forward<K>(key))};
12391239
u64 h1(H1(hash));
12401240
hud::check(hud::bits::is_valid_power_of_two_mask(max_slot_count_) && "Not a mask");
12411241
usize slot_index(h1 & max_slot_count_);
@@ -1270,11 +1270,11 @@ namespace hud
12701270
{
12711271
if constexpr (hud::is_hashable_64_v<key_type, K>)
12721272
{
1273-
return hasher_(hud::forward<K>(key));
1273+
return hasher()(hud::forward<K>(key));
12741274
}
12751275
else
12761276
{
1277-
return hasher_(key_type(hud::forward<K>(key)));
1277+
return hasher()(key_type(hud::forward<K>(key)));
12781278
}
12791279
}
12801280

@@ -1346,7 +1346,7 @@ namespace hud
13461346
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
13471347
{
13481348
// Compute the hash
1349-
u64 hash {hasher_(slot_ptr->key())};
1349+
u64 hash {hasher()(slot_ptr->key())};
13501350
// Find H1 slot index
13511351
u64 h1 {H1(hash)};
13521352
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
@@ -1397,7 +1397,7 @@ namespace hud
13971397
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
13981398
{
13991399
// Compute the hash
1400-
u64 hash {hasher_(slot_ptr->key())};
1400+
u64 hash {hasher()(slot_ptr->key())};
14011401
// Find H1 slot index
14021402
u64 h1 {H1(hash)};
14031403
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
@@ -1953,33 +1953,43 @@ namespace hud
19531953

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

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

19641964
[[nodiscard]] constexpr allocator_type &allocator_mut() noexcept
19651965
{
19661966
return hud::get<0>(compressed_);
19671967
}
19681968

1969+
[[nodiscard]] constexpr hasher_type &hasher() noexcept
1970+
{
1971+
return hud::get<1>(compressed_);
1972+
}
1973+
1974+
[[nodiscard]] constexpr const hasher_type &hasher() const noexcept
1975+
{
1976+
return hud::get<1>(compressed_);
1977+
}
1978+
19691979
private:
19701980
/** Max count of slot in the map. Always a power of two mask value. */
19711981
usize max_slot_count_ {0};
19721982

19731983
/** The count of values in the hashmap. */
19741984
usize count_ {0};
19751985

1976-
hud::compressed_tuple<allocator_type, usize> compressed_ {hud::tag_init};
1986+
hud::compressed_tuple<allocator_type, hasher_type, usize> compressed_ {hud::tag_init};
19771987

19781988
/** The allocator. */
19791989
// allocator_type allocator_;
19801990

19811991
/** The hasher function. */
1982-
hasher_type hasher_;
1992+
// hasher_type hasher_;
19831993

19841994
/** The key equal function. */
19851995
key_equal_type key_equal_;

0 commit comments

Comments
 (0)