Skip to content

Commit 00fe07e

Browse files
author
Julian LALU
committed
Improve hashmap and hashset
Add hashset test Update hashmap test
1 parent ca41a19 commit 00fe07e

File tree

7 files changed

+1356
-454
lines changed

7 files changed

+1356
-454
lines changed

interface/core/containers/hashset.h

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ namespace hud
165165
* @return A reference to the element at the specified index.
166166
*/
167167
template<usize idx_to_reach>
168-
[[nodiscard]] friend constexpr decltype(auto) get(hashset_storage &s) noexcept
168+
[[nodiscard]] friend constexpr const key_type &get(hashset_storage &s) noexcept
169169
{
170170
return s.element_;
171171
}
@@ -177,7 +177,7 @@ namespace hud
177177
* @return A const reference to the element at the specified index.
178178
*/
179179
template<usize idx_to_reach>
180-
[[nodiscard]] friend constexpr decltype(auto) get(const hashset_storage &s) noexcept
180+
[[nodiscard]] friend constexpr const key_type &get(const hashset_storage &s) noexcept
181181
{
182182
return s.element_;
183183
}
@@ -191,7 +191,7 @@ namespace hud
191191
template<usize idx_to_reach>
192192
[[nodiscard]] friend constexpr decltype(auto) get(hashset_storage &&s) noexcept
193193
{
194-
return hud::forward<hashset_storage>(s).element_;
194+
return hud::forward<key_type &&>(s.element_);
195195
}
196196

197197
/**
@@ -203,7 +203,7 @@ namespace hud
203203
template<usize idx_to_reach>
204204
[[nodiscard]] friend constexpr decltype(auto) get(const hashset_storage &&s) noexcept
205205
{
206-
return hud::forward<const hashset_storage>(s).element_;
206+
return hud::forward<const key_type &&>(s.element_);
207207
}
208208

209209
/**
@@ -797,21 +797,21 @@ namespace hud
797797
slot_type *slot_ptr_;
798798
};
799799

800-
template<bool is_transparent>
801-
struct KeyArg
802-
{
803-
// Transparent. Forward `K`.
804-
template<typename K, typename key_type>
805-
using type = K;
806-
};
807-
808-
template<>
809-
struct KeyArg<false>
810-
{
811-
// Not transparent. Always use `key_type`.
812-
template<typename K, typename key_type>
813-
using type = key_type;
814-
};
800+
// template<bool is_transparent>
801+
// struct KeyArg
802+
// {
803+
// // Transparent. Forward `K`.
804+
// template<typename K, typename key_type>
805+
// using type = K;
806+
// };
807+
808+
// template<>
809+
// struct KeyArg<false>
810+
// {
811+
// // Not transparent. Always use `key_type`.
812+
// template<typename K, typename key_type>
813+
// using type = key_type;
814+
// };
815815

816816
template<
817817
typename storage_t,
@@ -1173,36 +1173,35 @@ namespace hud
11731173
* @param args The arguments forwarded to the `slot_type` constructor after the key.
11741174
* @return An iterator to the inserted or existing value.
11751175
*/
1176-
template<typename... args_t>
1177-
requires(hud::is_constructible_v<slot_type, key_type, args_t...>)
1178-
constexpr iterator add_impl(key_type &&key, args_t &&...args) noexcept
1176+
template<typename K, typename... args_t>
1177+
constexpr iterator add_impl(K &&key, args_t &&...args) noexcept
11791178
{
1180-
hud::pair<iterator, bool> res {find_or_insert_no_construct(key)};
1179+
hud::pair<iterator, bool> res {find_or_insert_no_construct(hud::forward<K>(key))};
11811180
if (res.second)
11821181
{
1183-
hud::memory::construct_object_at(res.first.slot_ptr_, hud::move(key), hud::forward<args_t>(args)...);
1182+
hud::memory::construct_object_at(res.first.slot_ptr_, hud::forward<K>(key), hud::forward<args_t>(args)...);
11841183
}
11851184
return res.first;
11861185
}
11871186

1188-
/**
1189-
* Finds or inserts a slot corresponding to the given key.
1190-
* If the key is not found, a new slot is created by constructing it with the key followed by `args`.
1191-
* @param key The key used to find or insert the slot.
1192-
* @param args The arguments forwarded to the `slot_type` constructor after the key.
1193-
* @return An iterator to the inserted or existing value.
1194-
*/
1195-
template<typename... args_t>
1196-
requires(hud::is_constructible_v<slot_type, const key_type &, args_t...>)
1197-
constexpr iterator add_impl(const key_type &key, args_t &&...args) noexcept
1198-
{
1199-
hud::pair<iterator, bool> res {find_or_insert_no_construct(key)};
1200-
if (res.second)
1201-
{
1202-
hud::memory::construct_object_at(res.first.slot_ptr_, key, hud::forward<args_t>(args)...);
1203-
}
1204-
return res.first;
1205-
}
1187+
// /**
1188+
// * Finds or inserts a slot corresponding to the given key.
1189+
// * If the key is not found, a new slot is created by constructing it with the key followed by `args`.
1190+
// * @param key The key used to find or insert the slot.
1191+
// * @param args The arguments forwarded to the `slot_type` constructor after the key.
1192+
// * @return An iterator to the inserted or existing value.
1193+
// */
1194+
// template<typename... args_t>
1195+
// requires(hud::is_constructible_v<slot_type, const key_type &, args_t...>)
1196+
// constexpr iterator add_impl(const key_type &key, args_t &&...args) noexcept
1197+
// {
1198+
// hud::pair<iterator, bool> res {find_or_insert_no_construct(key)};
1199+
// if (res.second)
1200+
// {
1201+
// hud::memory::construct_object_at(res.first.slot_ptr_, key, hud::forward<args_t>(args)...);
1202+
// }
1203+
// return res.first;
1204+
// }
12061205

12071206
/**
12081207
* Adds a new element to the container using piecewise construction of the key and value.
@@ -1225,7 +1224,7 @@ namespace hud
12251224
template<typename key_tuple_t, typename... u_tuple_t>
12261225
constexpr iterator add_impl(hud::tag_piecewise_construct_t, key_tuple_t &&key_tuple, u_tuple_t &&...tuples) noexcept
12271226
{
1228-
hud::pair<iterator, bool> res = find_or_insert_no_construct(forward_key(hud::forward<key_tuple_t>(key_tuple)));
1227+
hud::pair<iterator, bool> res = find_or_insert_no_construct(hud::forward<key_tuple_t>(key_tuple));
12291228
if (res.second)
12301229
{
12311230
hud::memory::construct_object_at(res.first.slot_ptr_, hud::tag_piecewise_construct, hud::forward<key_tuple_t>(key_tuple), hud::forward<u_tuple_t>(tuples)...);
@@ -2060,6 +2059,28 @@ namespace hud
20602059
}
20612060
}
20622061

2062+
/**
2063+
* Constructor that initializes the hash map with a list of key-value pairs and additional capacity.
2064+
* @tparam u_key_t The type of the keys in the initializer list.
2065+
* @tparam u_value_t The type of the values in the initializer list.
2066+
* @param list The initializer list of key-value pairs.
2067+
* @param extra_element_count Additional capacity to reserve.
2068+
* @param allocator The allocator to use for memory management.
2069+
*/
2070+
2071+
template<typename u_key_t = key_type>
2072+
requires(hud::is_copy_constructible_v<key_type, u_key_t>)
2073+
constexpr hashset(std::initializer_list<u_key_t> list, const usize extra_element_count, const allocator_type &allocator = allocator_type()) noexcept
2074+
: super(allocator)
2075+
{
2076+
static_assert(hud::is_nothrow_copy_constructible_v<key_type, u_key_t>, "key_type(const u_key_t&) copy constructor is throwable. hashset_storage is not designed to allow throwable copy constructible components");
2077+
reserve(list.size() + extra_element_count);
2078+
for (const auto &element : list)
2079+
{
2080+
add(element);
2081+
}
2082+
}
2083+
20632084
/**
20642085
* Insert a key in the hashset.
20652086
* @param key The key
@@ -2098,16 +2119,16 @@ namespace hud
20982119
};
20992120

21002121
template<typename key_t>
2101-
struct tuple_size<details::hashset::slot<key_t>>
2122+
struct tuple_size<details::hashset::hashset_storage<key_t>>
21022123
: hud::integral_constant<usize, 1>
21032124
{
21042125
};
21052126

21062127
template<usize idx_to_reach, typename key_t>
2107-
struct tuple_element<idx_to_reach, details::hashset::slot<key_t>>
2128+
struct tuple_element<idx_to_reach, details::hashset::hashset_storage<key_t>>
21082129
{
2109-
static_assert(idx_to_reach < 1, "hashset slot index out of bounds");
2110-
using type = const typename details::hashset::slot<key_t>::key_type;
2130+
static_assert(idx_to_reach < 1, "hashset hashset_storage index out of bounds");
2131+
using type = const typename details::hashset::hashset_storage<key_t>::key_type;
21112132
};
21122133

21132134
template<typename key_t, typename hasher_t, typename key_equal_t, typename allocator_t>
@@ -2141,14 +2162,14 @@ namespace hud
21412162
namespace std
21422163
{
21432164
template<typename key_t>
2144-
struct tuple_size<hud::details::hashset::slot<key_t>>
2145-
: hud::tuple_size<hud::details::hashset::slot<key_t>>
2165+
struct tuple_size<hud::details::hashset::hashset_storage<key_t>>
2166+
: hud::tuple_size<hud::details::hashset::hashset_storage<key_t>>
21462167
{
21472168
};
21482169

21492170
template<std::size_t idx_to_reach, typename key_t>
2150-
struct tuple_element<idx_to_reach, hud::details::hashset::slot<key_t>>
2151-
: hud::tuple_element<idx_to_reach, hud::details::hashset::slot<key_t>>
2171+
struct tuple_element<idx_to_reach, hud::details::hashset::hashset_storage<key_t>>
2172+
: hud::tuple_element<idx_to_reach, hud::details::hashset::hashset_storage<key_t>>
21522173
{
21532174
};
21542175

0 commit comments

Comments
 (0)