Skip to content

Commit 0df7225

Browse files
author
Julian LALU
committed
Improve Hashset
1 parent 5fd5235 commit 0df7225

35 files changed

Lines changed: 2712 additions & 2642 deletions

interface/core/containers/array.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ namespace hud
10081008
* @return first index of the element where the predicate array[index] == element is true, hud::index_none otherwise
10091009
*/
10101010
template<typename compared_t>
1011-
requires(hud::is_comparable_with_equal_v<type_t, compared_t>)
1011+
requires(hud::is_comparable_with_equal_operator_v<type_t, compared_t>)
10121012
[[nodiscard]] usize find_first_index(const compared_t &to_find) const
10131013
{
10141014
const type_t *HD_RESTRICT begin = data();
@@ -1032,7 +1032,7 @@ namespace hud
10321032
* @return last index of the element where the predicate array[index] == element is true, hud::index_none otherwise
10331033
*/
10341034
template<typename compared_t>
1035-
requires(hud::is_comparable_with_equal_v<type_t, compared_t>)
1035+
requires(hud::is_comparable_with_equal_operator_v<type_t, compared_t>)
10361036
[[nodiscard]] usize find_last_index(const compared_t &to_find) const
10371037
{
10381038
const type_t *HD_RESTRICT begin = data();
@@ -1101,7 +1101,7 @@ namespace hud
11011101
* @return true if the element is contained in the array, false otherwise
11021102
*/
11031103
template<typename compared_t>
1104-
requires(hud::is_comparable_with_equal_v<type_t, compared_t>)
1104+
requires(hud::is_comparable_with_equal_operator_v<type_t, compared_t>)
11051105
[[nodiscard]] bool contains(const compared_t &to_find) const
11061106
{
11071107
return find_first_index(to_find) != hud::index_none;

interface/core/containers/hashmap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ namespace hud
356356
return super::add(hud::forward<u_key_t>(key), hud::forward<u_value_t>(value));
357357
}
358358

359+
/**
360+
* Insert a key and value in the hashmap by piecewise construct them in place.
361+
* If the `key_type` is not hashable with the `key_tuple_t` a temporary key is created to find it in the hashmap
362+
* To make the `key_type` hashable with the `key_tuple_t` you must specialize the `hud::equal<key_type>` functor by adding the function
363+
* ` template<typename T1, typename T2> [[nodiscard]] constexpr bool operator()(const hud::tuple<T1,T2> &rhs) const noexcept` or by
364+
*/
359365
template<typename key_tuple_t, typename value_tuple_t>
360366
constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple) noexcept
361367
{

interface/core/containers/hashset.h

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "pair.h"
1010
#include "../hash.h"
1111
#include "../bits.h"
12-
#include "../traits/is_comparable_with_equal.h"
12+
#include "../traits/is_comparable_with_equal_operator.h"
1313
#include "../traits/is_trivially_copy_constructible.h"
1414
#include "tuple_size.h"
1515
#include "tuple_element.h"
@@ -792,8 +792,8 @@ namespace hud
792792
* HashableAndComparableArgType is used to select the type to be used by a fonction depending of the hashable and comparable possiblity.
793793
* If the type K est not hashable or comparable, the type is `key_type`, else the type is `K`
794794
*/
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>;
795+
template<typename K>
796+
static constexpr bool is_hashable_and_comparable = hud::conjunction_v<hud::is_hashable_64<key_type, K>, hud::is_comparable_with_equal<key_type, K>>;
797797

798798
template<typename u_storage_t, typename u_hasher_t, typename u_key_equal_t, typename u_allocator_t>
799799
friend class hashset_impl; // Friend with other hashset_impl of other types
@@ -972,7 +972,7 @@ namespace hud
972972
[[nodiscard]]
973973
constexpr iterator find(K &&key) noexcept
974974
{
975-
if constexpr (hud::is_hashable_64_v<K> && hud::is_comparable_with_equal_v<key_type, K>)
975+
if constexpr (is_hashable_and_comparable<K>)
976976
{
977977
return find_impl(hud::forward<K>(key));
978978
}
@@ -1163,10 +1163,24 @@ namespace hud
11631163
* @return An iterator to the inserted or existing value.
11641164
*/
11651165
template<typename key_tuple_t, typename value_tuple_t>
1166-
requires(hud::is_hashable_64_v<key_type, key_tuple_t> && hud::is_comparable_with_equal_v<key_type, key_tuple_t>)
11671166
constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple) noexcept
11681167
{
1169-
hud::pair<usize, bool> res = find_or_insert_no_construct(hud::forward<key_tuple_t>(key_tuple));
1168+
constexpr auto forward_key = []<usize... indices_key>(
1169+
key_tuple_t &&key_tuple,
1170+
hud::index_sequence<indices_key...>
1171+
) -> decltype(auto)
1172+
{
1173+
if constexpr (is_hashable_and_comparable<key_tuple_t>)
1174+
{
1175+
return hud::forward<key_tuple_t>(key_tuple);
1176+
}
1177+
else
1178+
{
1179+
return key_type(hud::get<indices_key>(key_tuple)...);
1180+
}
1181+
};
1182+
1183+
hud::pair<usize, bool> res = find_or_insert_no_construct(forward_key(hud::forward<key_tuple_t>(key_tuple), hud::make_index_sequence<hud::tuple_size_v<key_tuple_t>> {}));
11701184
slot_type *slot_ptr {slot_ptr_ + res.first};
11711185
if (res.second)
11721186
{
@@ -1175,25 +1189,52 @@ namespace hud
11751189
return {control_ptr_ + res.first, slot_ptr};
11761190
}
11771191

1178-
template<typename key_tuple_t, typename value_tuple_t>
1179-
constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple) noexcept
1180-
{
1181-
return add(hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple), hud::make_index_sequence<hud::tuple_size_v<key_tuple_t>> {}, hud::make_index_sequence<hud::tuple_size_v<value_tuple_t>> {});
1182-
}
1192+
// template<typename key_tuple_t, typename value_tuple_t>
1193+
// requires(hud::is_hashable_64_v<key_type, key_tuple_t> && hud::is_comparable_with_equal_v<key_type, key_tuple_t>)
1194+
// constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple) noexcept
1195+
// {
1196+
// hud::pair<usize, bool> res = find_or_insert_no_construct(hud::forward<key_tuple_t>(key_tuple));
1197+
// slot_type *slot_ptr {slot_ptr_ + res.first};
1198+
// if (res.second)
1199+
// {
1200+
// hud::memory::construct_object_at(slot_ptr, hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple));
1201+
// }
1202+
// return {control_ptr_ + res.first, slot_ptr};
1203+
// }
1204+
1205+
// template<typename key_tuple_t, typename value_tuple_t>
1206+
// constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple) noexcept
1207+
// {
1208+
// return add(hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple), hud::make_index_sequence<hud::tuple_size_v<key_tuple_t>> {});
1209+
// }
11831210

11841211
private:
1185-
template<typename key_tuple_t, usize... indices_key, typename value_tuple_t, usize... indices_value>
1186-
constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple, hud::index_sequence<indices_key...>, hud::index_sequence<indices_value...>) noexcept
1187-
{
1188-
key_type key(hud::get<indices_key>(key_tuple)...);
1189-
hud::pair<usize, bool> res = find_or_insert_no_construct(key);
1190-
slot_type *slot_ptr {slot_ptr_ + res.first};
1191-
if (res.second)
1192-
{
1193-
hud::memory::construct_object_at(slot_ptr, hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple));
1194-
}
1195-
return {control_ptr_ + res.first, slot_ptr};
1196-
}
1212+
// template<typename key_tuple_t, usize... indices_key, typename value_tuple_t>
1213+
// constexpr iterator add(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, value_tuple_t &&value_tuple, hud::index_sequence<indices_key...>) noexcept
1214+
// {
1215+
// key_type key(hud::get<indices_key>(key_tuple)...);
1216+
// hud::pair<usize, bool> res = find_or_insert_no_construct(key);
1217+
// slot_type *slot_ptr {slot_ptr_ + res.first};
1218+
// if (res.second)
1219+
// {
1220+
// hud::memory::construct_object_at(slot_ptr, hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<value_tuple_t>(value_tuple));
1221+
// }
1222+
// return {control_ptr_ + res.first, slot_ptr};
1223+
// }
1224+
1225+
// template<typename key_tuple_t, usize... indices_key>
1226+
// [[nodiscard]]
1227+
// constexpr decltype(auto) forward_key(key_tuple_t &&key_tuple, hud::index_sequence<indices_key...>) noexcept
1228+
// {
1229+
// if constexpr (is_hashable_and_comparable<key_tuple_t>)
1230+
// {
1231+
// return hud::forward<key_tuple_t>(key_tuple);
1232+
// }
1233+
// else
1234+
// {
1235+
// return key_type(hud::get<indices_key>(key_tuple)...);
1236+
// }
1237+
// }
11971238

11981239
template<typename K>
11991240
[[nodiscard]]
@@ -1236,7 +1277,10 @@ namespace hud
12361277
{
12371278
return hasher_(key);
12381279
}
1239-
return hasher_(key_type(key));
1280+
else
1281+
{
1282+
return hasher_(key_type(key));
1283+
}
12401284
}
12411285

12421286
constexpr bool should_be_mark_as_empty_if_deleted(usize index) const noexcept
@@ -1558,11 +1602,14 @@ namespace hud
15581602
[[nodiscard]]
15591603
constexpr hud::pair<usize, bool> find_or_insert_no_construct(K &&key) noexcept
15601604
{
1561-
if constexpr (hud::is_hashable_64_v<key_type, K> && hud::is_comparable_with_equal_v<key_type, K>)
1605+
if constexpr (is_hashable_and_comparable<K>)
15621606
{
15631607
return find_or_insert_no_construct_impl(hud::forward<K>(key));
15641608
}
1565-
return find_or_insert_no_construct_impl(key_type(key));
1609+
else
1610+
{
1611+
return find_or_insert_no_construct_impl(key_type(key));
1612+
}
15661613
}
15671614

15681615
/**

0 commit comments

Comments
 (0)