Skip to content

Commit b564097

Browse files
author
Julian LALU
committed
Improve Hashset
1 parent 90c0452 commit b564097

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

interface/core/containers/hashmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ namespace hud
346346
template<typename key_tuple_t, typename value_tuple_t>
347347
constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t key_tuple, value_tuple_t value_tuple) noexcept
348348
{
349+
// return super::add(hud::tag_piecewise_construct_t, key_tuple, value_tuple);
349350
}
350351
};
351352

interface/core/containers/hashset.h

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,13 @@ namespace hud
787787

788788
static_assert(hud::is_hashable_64_v<key_type>, "key_type is not hashable");
789789
static_assert(hud::is_comparable_with_equal_v<key_type, key_type>, "key_type is not comparable with equal");
790-
template<typename K>
791-
using key_arg_type = typename KeyArg<hud::is_hashable_64_v<K> && hud::is_comparable_with_equal_v<key_type, K>>::template type<K, key_type>;
790+
791+
/**
792+
* HashableAndComparableArgType is used to select the type to be used by a fonction depending of the hashable and comparable possiblity.
793+
* If the type K est not hashable or comparable, the type is `key_type`, else the type is `K`
794+
*/
795+
// template<typename K>
796+
// using HashableAndComparableArgType = typename KeyArg<hud::is_hashable_64_v<key_type, K> && hud::is_comparable_with_equal_v<key_type, K>>::template type<K, key_type>;
792797

793798
template<typename u_storage_t, typename u_hasher_t, typename u_key_equal_t, typename u_allocator_t>
794799
friend class hashset_impl; // Friend with other hashset_impl of other types
@@ -1150,6 +1155,26 @@ namespace hud
11501155
return {control_ptr_ + res.first, slot_ptr};
11511156
}
11521157

1158+
/**
1159+
* Finds or inserts a slot corresponding to the given key.
1160+
* If the key is not found, a new slot is created by constructing it with the key followed by `args`.
1161+
* @param key The key used to find or insert the slot.
1162+
* @param args The arguments forwarded to the `slot_type` constructor after the key.
1163+
* @return An iterator to the inserted or existing value.
1164+
*/
1165+
// template<typename key_tuple_t, typename value_tuple_t>
1166+
// requires(hud::is_constructible_v<slot_type, key_type, args_t...>)
1167+
// constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t key_tuple, value_tuple_t value_tuple) noexcept
1168+
// {
1169+
// hud::pair<usize, bool> res {find_or_insert_no_construct(key)};
1170+
// slot_type *slot_ptr {slot_ptr_ + res.first};
1171+
// if (res.second)
1172+
// {
1173+
// hud::memory::construct_object_at(slot_ptr, hud::move(key), hud::forward<args_t>(args)...);
1174+
// }
1175+
// return {control_ptr_ + res.first, slot_ptr};
1176+
// }
1177+
11531178
private:
11541179
template<typename K>
11551180
[[nodiscard]]
@@ -1188,7 +1213,7 @@ namespace hud
11881213
template<typename K>
11891214
[[nodiscard]] constexpr u64 compute_hash(const K &key) noexcept
11901215
{
1191-
if constexpr (hud::is_same_v<key_type, K>)
1216+
if constexpr (hud::is_hashable_64_v<key_type, K>)
11921217
{
11931218
return hasher_(key);
11941219
}
@@ -1510,12 +1535,24 @@ namespace hud
15101535
}
15111536
}
15121537

1538+
template<typename K>
1539+
[[nodiscard]]
1540+
constexpr hud::pair<usize, bool> find_or_insert_no_construct(K &&key) noexcept
1541+
{
1542+
if constexpr (hud::is_hashable_64_v<key_type, K> && hud::is_comparable_with_equal_v<key_type, K>)
1543+
{
1544+
return find_or_insert_no_construct_impl(hud::forward<K>(key));
1545+
}
1546+
return find_or_insert_no_construct_impl(key_type(key));
1547+
}
1548+
15131549
/**
15141550
* Find the key and add the H2 hash in the control
15151551
* If the key is found, return the iterator
15161552
* If not found insert the key but do not construct the value.
15171553
*/
1518-
[[nodiscard]] constexpr hud::pair<usize, bool> find_or_insert_no_construct(const key_type &key) noexcept
1554+
template<typename K>
1555+
[[nodiscard]] constexpr hud::pair<usize, bool> find_or_insert_no_construct_impl(K &&key) noexcept
15191556
{
15201557
u64 hash {compute_hash(key)};
15211558
u64 h1(H1(hash));

interface/core/hash.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -495,38 +495,50 @@ namespace hud
495495
};
496496

497497
// Traits used to check if a type is hashable
498-
template<typename type_t, typename = void>
498+
template<typename type_t, typename u_type_t = type_t, typename = void>
499499
struct is_hashable_64
500500
: hud::false_type
501501
{
502502
};
503503

504504
template<typename type_t>
505-
struct is_hashable_64<type_t, void_t<decltype(hud::hash_64<hud::remove_cv_t<type_t>> {}(hud::declval<type_t>()))>>
505+
struct is_hashable_64<type_t, type_t, void_t<decltype(hud::hash_64<hud::remove_cv_t<type_t>> {}(hud::declval<type_t>()))>>
506+
: hud::true_type
507+
{
508+
};
509+
510+
template<typename type_t, typename u_type_t>
511+
struct is_hashable_64<type_t, u_type_t, void_t<decltype(hud::hash_64<hud::remove_cv_t<type_t>> {}(hud::declval<u_type_t>()))>>
506512
: hud::true_type
507513
{
508514
};
509515

510516
/** Equivalent of is_hashable<type_t>::value. */
511-
template<typename type_t>
512-
constexpr bool is_hashable_64_v = is_hashable_64<type_t>::value;
517+
template<typename type_t, typename u_type_t = type_t>
518+
constexpr bool is_hashable_64_v = is_hashable_64<type_t, u_type_t>::value;
513519

514520
// Traits used to check if a type is hashable
515-
template<typename type_t, typename = void>
521+
template<typename type_t, typename u_type_t = type_t, typename = void>
516522
struct is_hashable_32
517523
: hud::false_type
518524
{
519525
};
520526

521527
template<typename type_t>
522-
struct is_hashable_32<type_t, void_t<decltype(hud::hash_32<hud::remove_cv_t<type_t>> {}(hud::declval<type_t>()))>>
528+
struct is_hashable_32<type_t, type_t, void_t<decltype(hud::hash_32<hud::remove_cv_t<type_t>> {}(hud::declval<type_t>()))>>
529+
: hud::true_type
530+
{
531+
};
532+
533+
template<typename type_t, typename u_type_t>
534+
struct is_hashable_32<type_t, u_type_t, void_t<decltype(hud::hash_32<hud::remove_cv_t<type_t>> {}(hud::declval<u_type_t>()))>>
523535
: hud::true_type
524536
{
525537
};
526538

527539
/** Equivalent of is_hashable<type_t>::value. */
528-
template<typename type_t>
529-
constexpr bool is_hashable_32_v = is_hashable_32<type_t>::value;
540+
template<typename type_t, typename u_type_t = type_t>
541+
constexpr bool is_hashable_32_v = is_hashable_32<type_t, u_type_t>::value;
530542
} // namespace hud
531543

532544
#endif // HD_INC_CORE_HASH_H

0 commit comments

Comments
 (0)