Skip to content

Commit 8f83edc

Browse files
author
Julian LALU
committed
Improve hashmap
1 parent 223dd46 commit 8f83edc

16 files changed

Lines changed: 338 additions & 322 deletions

interface/core/containers/hashmap.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,6 @@ namespace hud
211211

212212
} // namespace details::hashmap
213213

214-
/** The default hasher for hash maps. */
215-
using hashmap_default_hasher = details::hashset::default_hasher;
216-
217-
/** The default equality comparator for hash map keys. */
218-
template<typename key_t>
219-
using hashmap_default_key_equal = details::hashset::default_equal<key_t>;
220-
221214
/** The default allocator type for hash maps. */
222215
using hashmap_default_allocator = hud::heap_allocator;
223216

@@ -234,8 +227,8 @@ namespace hud
234227
template<
235228
typename key_t,
236229
typename value_t,
237-
typename hasher_t = hashmap_default_hasher,
238-
typename key_equal_t = hashmap_default_key_equal<key_t>,
230+
typename hasher_t = hud::hash_64<key_t>,
231+
typename key_equal_t = hud::equal<key_t>,
239232
typename allocator_t = hashmap_default_allocator>
240233
class hashmap
241234
: public details::hashset::hashset_impl<details::hashmap::hashmap_storage<key_t, value_t>, hasher_t, key_equal_t, allocator_t>

interface/core/containers/hashset.h

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -281,49 +281,49 @@ namespace hud
281281
key_type element_;
282282
};
283283

284-
/**
285-
* A default hasher struct for hashing keys.
286-
* This struct provides a mechanism to hash key and combine them with the current hasher value.
287-
*
288-
* @tparam key_t The type of the key to be hashed.
289-
*/
290-
struct default_hasher
291-
{
292-
using is_transparent = void;
293-
294-
/**
295-
* Operator to hash the value and combine it with the current hasher value.
296-
* This function uses variadic templates to accept multiple arguments.
297-
* @tparam type_t Types of the arguments to hash.
298-
* @param values Arguments to hash.
299-
* @return A 64-bit hash value.
300-
*/
301-
template<typename... type_t>
302-
[[nodiscard]] constexpr u64 operator()(type_t &&...values) noexcept
303-
{
304-
return hud::hash_64<hud::decay_t<type_t>...> {}(hud::forward<type_t>(values)...);
305-
}
306-
307-
/**
308-
* Function to hash the value and combine it with the current hasher value.
309-
* This function uses variadic templates to accept multiple arguments.
310-
* @tparam type_t Types of the arguments to hash.
311-
* @param values Arguments to hash.
312-
* @return A 64-bit hash value.
313-
*/
314-
template<typename... type_t>
315-
[[nodiscard]] constexpr u64 hash(type_t &&...values) noexcept
316-
{
317-
return (*this)(hud::forward<type_t>(values)...);
318-
}
319-
};
320-
321-
template<typename key_t>
322-
struct default_equal
323-
: hud::equal<key_t>
324-
{
325-
using is_transparent = void;
326-
};
284+
// /**
285+
// * A default hasher struct for hashing keys.
286+
// * This struct provides a mechanism to hash key and combine them with the current hasher value.
287+
// *
288+
// * @tparam key_t The type of the key to be hashed.
289+
// */
290+
// struct default_hasher
291+
// {
292+
// using is_transparent = void;
293+
294+
// /**
295+
// * Operator to hash the value and combine it with the current hasher value.
296+
// * This function uses variadic templates to accept multiple arguments.
297+
// * @tparam type_t Types of the arguments to hash.
298+
// * @param values Arguments to hash.
299+
// * @return A 64-bit hash value.
300+
// */
301+
// template<typename... type_t>
302+
// [[nodiscard]] constexpr u64 operator()(type_t &&...values) noexcept
303+
// {
304+
// return hud::hash_64<hud::decay_t<type_t>...> {}(hud::forward<type_t>(values)...);
305+
// }
306+
307+
// /**
308+
// * Function to hash the value and combine it with the current hasher value.
309+
// * This function uses variadic templates to accept multiple arguments.
310+
// * @tparam type_t Types of the arguments to hash.
311+
// * @param values Arguments to hash.
312+
// * @return A 64-bit hash value.
313+
// */
314+
// template<typename... type_t>
315+
// [[nodiscard]] constexpr u64 hash(type_t &&...values) noexcept
316+
// {
317+
// return (*this)(hud::forward<type_t>(values)...);
318+
// }
319+
// };
320+
321+
// template<typename key_t>
322+
// struct default_equal
323+
// : hud::equal<key_t>
324+
// {
325+
// using is_transparent = void;
326+
// };
327327

328328
using default_allocator = hud::heap_allocator;
329329

@@ -1041,9 +1041,9 @@ namespace hud
10411041
}
10421042

10431043
/** Find a key and return an iterator to the value. */
1044-
template<typename K = key_type>
1044+
template<typename K>
10451045
[[nodiscard]]
1046-
constexpr iterator find(const key_arg_type<K> &key) noexcept
1046+
constexpr iterator find(const K &key) noexcept
10471047
{
10481048
u64 hash {hasher_(key)};
10491049
u64 h1(H1(hash));
@@ -1210,7 +1210,17 @@ namespace hud
12101210
}
12111211

12121212
private:
1213-
constexpr bool should_be_mark_as_empty_if_deleted(usize index) noexcept
1213+
template<typename u_key_type>
1214+
[[nodiscard]] constexpr u64 compute_hash(const u_key_type &key) noexcept
1215+
{
1216+
if constexpr (hud::is_same_v<key_type, u_key_type>)
1217+
{
1218+
return hasher_(key);
1219+
}
1220+
return hasher_(key_type {key});
1221+
}
1222+
1223+
constexpr bool should_be_mark_as_empty_if_deleted(usize index) const noexcept
12141224
{
12151225
// If map fits entirely into a probing group.
12161226
if (max_slot_count_ <= group_type::SLOT_PER_GROUP)
@@ -1412,7 +1422,7 @@ namespace hud
14121422
{
14131423
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
14141424
{
1415-
u64 hash {hud::is_same_v<key_type, typename u_storage_t::key_type> ? hasher_(slot_ptr->key()) : hasher_(key_type {slot_ptr->key()})};
1425+
u64 hash {compute_hash(slot_ptr->key())};
14161426
// Find H1 slot index
14171427
u64 h1 {H1(hash)};
14181428
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
@@ -1487,7 +1497,7 @@ namespace hud
14871497
{
14881498
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
14891499
{
1490-
u64 hash {hud::is_same_v<key_type, typename u_storage_t::key_type> ? hasher_(slot_ptr->key()) : hasher_(key_type {slot_ptr->key()})};
1500+
u64 hash {compute_hash(slot_ptr->key())};
14911501
// Find H1 slot index
14921502
u64 h1 {H1(hash)};
14931503
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
@@ -1532,7 +1542,7 @@ namespace hud
15321542
*/
15331543
[[nodiscard]] constexpr hud::pair<usize, bool> find_or_insert_no_construct(const key_type &key) noexcept
15341544
{
1535-
u64 hash {hasher_(key)};
1545+
u64 hash {compute_hash(key)};
15361546
u64 h1(H1(hash));
15371547
hud::check(hud::bits::is_valid_power_of_two_mask(max_slot_count_) && "Not a mask");
15381548
usize slot_index(h1 & max_slot_count_);
@@ -1623,7 +1633,7 @@ namespace hud
16231633
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
16241634
{
16251635
// Compute the hash
1626-
u64 hash {hasher_(slot_ptr->key())};
1636+
u64 hash {compute_hash(slot_ptr->key())};
16271637
// Find H1 slot index
16281638
u64 h1 {H1(hash)};
16291639
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
@@ -1891,17 +1901,17 @@ namespace hud
18911901

18921902
} // namespace details::hashset
18931903

1894-
using hashset_default_hasher = details::hashset::default_hasher;
1904+
// using hashset_default_hasher = details::hashset::default_hasher;
18951905

1896-
template<typename element_t>
1897-
using hashset_default_key_equal = details::hashset::default_equal<element_t>;
1906+
// template<typename element_t>
1907+
// using hashset_default_key_equal = details::hashset::default_equal<element_t>;
18981908

18991909
using hashset_default_allocator = details::hashset::default_allocator;
19001910

19011911
template<
19021912
typename element_t,
1903-
typename hasher_t = hashset_default_hasher,
1904-
typename key_equal_t = hashset_default_key_equal<element_t>,
1913+
typename hasher_t = hud::hash_64<element_t>,
1914+
typename key_equal_t = hud::equal<element_t>,
19051915
typename allocator_t = hashset_default_allocator>
19061916
class hashset
19071917
: public details::hashset::hashset_impl<details::hashset::hashset_storage<element_t>, hasher_t, key_equal_t, allocator_t>

0 commit comments

Comments
 (0)