Skip to content

Commit b60476f

Browse files
author
Julian LALU
committed
Improve hashset hashmap
1 parent e82d869 commit b60476f

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

interface/core/containers/hashmap.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ namespace hud
282282
public:
283283
/** Type of the hash function. */
284284
using typename super::hasher_type;
285+
/** Type of the equal function. */
286+
using typename super::key_equal_type;
287+
/** Type of the iterator. */
288+
using typename super::iterator;
289+
/** Type of the const iterator. */
290+
using typename super::const_iterator;
291+
/** Type of the allocator. */
292+
using typename super::allocator_type;
285293
/** Type of the storage used to store key-value pairs. */
286294
using storage_type = typename super::storage_type;
287295
/** Type of the key. */
@@ -292,9 +300,6 @@ namespace hud
292300
/** Inherit constructors and methods from the base class. */
293301
using super::reserve;
294302
using super::super;
295-
using typename super::allocator_type;
296-
using typename super::const_iterator;
297-
using typename super::iterator;
298303
using super::operator=;
299304

300305
/** Default constructor. */

interface/core/containers/hashset.h

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -797,22 +797,6 @@ 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-
// };
815-
816800
template<
817801
typename storage_t,
818802
typename hasher_t,
@@ -1184,25 +1168,6 @@ namespace hud
11841168
return res.first;
11851169
}
11861170

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-
// }
1205-
12061171
/**
12071172
* Adds a new element to the container using piecewise construction of the key and value.
12081173
*
@@ -2034,16 +1999,22 @@ namespace hud
20341999
public:
20352000
/** Type of the hash function. */
20362001
using typename super::hasher_type;
2037-
/** Type of the storage used to store key-value pairs. */
2002+
/** Type of the equal function. */
2003+
using typename super::key_equal_type;
2004+
/** Type of the iterator. */
2005+
using typename super::iterator;
2006+
/** Type of the const iterator. */
2007+
using typename super::const_iterator;
2008+
/** Type of the allocator. */
2009+
using typename super::allocator_type;
2010+
/** Type of the storage used to store key. */
20382011
using storage_type = typename super::storage_type;
20392012
/** Type of the key. */
20402013
using key_type = typename storage_type::key_type;
2014+
20412015
/** Inherit constructors and methods from the base class. */
20422016
using super::reserve;
20432017
using super::super;
2044-
using typename super::allocator_type;
2045-
using typename super::const_iterator;
2046-
using typename super::iterator;
20472018
using super::operator=;
20482019

20492020
explicit constexpr hashset() noexcept = default;

test/hashmap/hashmap_misc.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,22 @@ GTEST_TEST(hashmap, count_return_count_of_element)
130130
hud_assert_true(std::get<2>(result));
131131
}
132132
}
133+
134+
GTEST_TEST(hashmap, sizeof_map_is_correct)
135+
{
136+
// Size of hashmap do not depends of the key and value types
137+
hud_assert_true(sizeof(hud::hashmap<i32, i32>) == sizeof(hud::hashmap<i64, i64>));
138+
139+
// hashmp is compressed, all empty element like allocator, comparator, hasher must be 0 size
140+
// This test must be updated if the member of hashmap is changed
141+
constexpr usize allocator_size = sizeof(hud::hashmap<i32, i32>::allocator_type);
142+
constexpr usize hasher_size = sizeof(hud::hashmap<i32, i32>::hasher_type);
143+
constexpr usize equal_size = sizeof(hud::hashmap<i32, i32>::key_equal_type);
144+
constexpr usize control_ptr_size = sizeof(void *);
145+
constexpr usize slot_ptr_size = sizeof(void *);
146+
constexpr usize count_and_max_count_and_free_slot_before_grow_size = 3 * sizeof(usize);
147+
148+
constexpr usize sizeof_map = sizeof(hud::hashmap<i32, i32>);
149+
hud_assert_true(sizeof_map == 48);
150+
hud_assert_true(sizeof_map >= (allocator_size + hasher_size + equal_size + control_ptr_size + slot_ptr_size + count_and_max_count_and_free_slot_before_grow_size));
151+
}

0 commit comments

Comments
 (0)