Skip to content

Commit 61d4c0a

Browse files
author
Julian LALU
committed
Improve hashmap
1 parent fcd84b9 commit 61d4c0a

1 file changed

Lines changed: 47 additions & 89 deletions

File tree

interface/core/containers/hashset.h

Lines changed: 47 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ namespace hud
831831
// Copy slot
832832
hud::memory::construct_object_at(slot_ptr_ + slot_index, *slot_ptr);
833833
};
834-
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, insert_slot_by_copy);
834+
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, other.count_, other.max_slot_count_, insert_slot_by_copy);
835835
}
836836
else
837837
{
@@ -894,7 +894,7 @@ namespace hud
894894
// Copy slot
895895
hud::memory::construct_object_at(slot_ptr_ + slot_index, *slot_ptr);
896896
};
897-
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, insert_slot_by_copy);
897+
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, other.count_, other.max_slot_count_, insert_slot_by_copy);
898898
}
899899
else
900900
{
@@ -956,25 +956,7 @@ namespace hud
956956
// Copy slot
957957
hud::memory::construct_object_at(slot_ptr_ + slot_index, hud::move(*slot_ptr));
958958
};
959-
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, insert_slot_by_copy);
960-
961-
// auto [control_full_or_sentinel, slot_full_or_sentinel] = skip_empty_or_deleted(other.control_ptr_, other.slot_ptr_);
962-
// while (control_full_or_sentinel != other.control_ptr_sentinel())
963-
// {
964-
// // Compute the hash
965-
// u64 hash {hasher_type {}(slot_full_or_sentinel->key())};
966-
// // Find H1 slot index
967-
// u64 h1 {H1(hash)};
968-
// usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
969-
// // Save h2 in control h1 index
970-
// control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
971-
// // Move old slot to new slot
972-
// hud::memory::move_or_copy_construct_object_then_destroy(slot_ptr_ + slot_index, hud::move(*slot_full_or_sentinel));
973-
974-
// control_type *full_or_sentinel {control::skip_empty_or_deleted(control_full_or_sentinel + 1)};
975-
// slot_full_or_sentinel += full_or_sentinel - control_full_or_sentinel;
976-
// control_full_or_sentinel = full_or_sentinel;
977-
// }
959+
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, other.count_, other.max_slot_count_, insert_slot_by_copy);
978960
}
979961
}
980962
else
@@ -1039,24 +1021,7 @@ namespace hud
10391021
// Copy slot
10401022
hud::memory::construct_object_at(slot_ptr_ + slot_index, hud::move(*slot_ptr));
10411023
};
1042-
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, insert_slot_by_copy);
1043-
// auto [control_full_or_sentinel, slot_full_or_sentinel] = skip_empty_or_deleted(other.control_ptr_, other.slot_ptr_);
1044-
// while (control_full_or_sentinel != other.control_ptr_sentinel())
1045-
// {
1046-
// // Compute the hash
1047-
// u64 hash {hasher_type {}(slot_full_or_sentinel->key())};
1048-
// // Find H1 slot index
1049-
// u64 h1 {H1(hash)};
1050-
// usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
1051-
// // Save h2 in control h1 index
1052-
// control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
1053-
// // Move old slot to new slot
1054-
// hud::memory::move_or_copy_construct_object_then_destroy(slot_ptr_ + slot_index, hud::move(*slot_full_or_sentinel));
1055-
1056-
// control_type *full_or_sentinel {control::skip_empty_or_deleted(control_full_or_sentinel + 1)};
1057-
// slot_full_or_sentinel += full_or_sentinel - control_full_or_sentinel;
1058-
// control_full_or_sentinel = full_or_sentinel;
1059-
// }
1024+
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, other.count_, other.max_slot_count_, insert_slot_by_copy);
10601025
}
10611026
}
10621027
else
@@ -1342,35 +1307,47 @@ namespace hud
13421307
// If we have elements to copy, copy them
13431308
if (count_ > 0)
13441309
{
1345-
auto compute_hash = [](const auto *slot) noexcept
1346-
{
1347-
if constexpr (hud::is_same_v<key_type, typename u_storage_t::key_type>)
1348-
{
1349-
return hasher_type {}(slot->key());
1350-
}
1351-
else
1352-
{
1353-
return hasher_type {}(static_cast<key_type>(slot->key()));
1354-
}
1355-
};
13561310

1357-
// Skip empty or deleted
1358-
auto [control_full_or_sentinel, slot_full_or_sentinel] = skip_empty_or_deleted(other.control_ptr_, other.slot_ptr_);
1359-
while (control_full_or_sentinel != other.control_ptr_sentinel())
1311+
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
13601312
{
1361-
u64 hash {compute_hash(slot_full_or_sentinel)};
1313+
u64 hash {hasher_type {}(static_cast<const key_type &>(slot_ptr->key()))};
13621314
// Find H1 slot index
13631315
u64 h1 {H1(hash)};
13641316
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
13651317
// Save h2 in control h1 index
13661318
control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
13671319
// Copy slot
1368-
hud::memory::construct_object_at(slot_ptr_ + slot_index, *slot_full_or_sentinel);
1320+
hud::memory::construct_object_at(slot_ptr_ + slot_index, *slot_ptr);
1321+
};
1322+
iterate_over_full_slots(other.control_ptr_, other.slot_ptr_, count_, other.max_slot_count_, insert_slot_by_copy);
1323+
// auto compute_hash = [](const auto *slot) noexcept
1324+
// {
1325+
// if constexpr (hud::is_same_v<key_type, typename u_storage_t::key_type>)
1326+
// {
1327+
// return hasher_type {}(slot->key());
1328+
// }
1329+
// else
1330+
// {
1331+
// return hasher_type {}(static_cast<key_type>(slot->key()));
1332+
// }
1333+
// };
1334+
// Skip empty or deleted
1335+
// auto [control_full_or_sentinel, slot_full_or_sentinel] = skip_empty_or_deleted(other.control_ptr_, other.slot_ptr_);
1336+
// while (control_full_or_sentinel != other.control_ptr_sentinel())
1337+
// {
1338+
// u64 hash {compute_hash(slot_full_or_sentinel)};
1339+
// // Find H1 slot index
1340+
// u64 h1 {H1(hash)};
1341+
// usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
1342+
// // Save h2 in control h1 index
1343+
// control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
1344+
// // Copy slot
1345+
// hud::memory::construct_object_at(slot_ptr_ + slot_index, *slot_full_or_sentinel);
13691346

1370-
control_type *full_or_sentinel {control::skip_empty_or_deleted(control_full_or_sentinel + 1)};
1371-
slot_full_or_sentinel += full_or_sentinel - control_full_or_sentinel;
1372-
control_full_or_sentinel = full_or_sentinel;
1373-
}
1347+
// control_type *full_or_sentinel {control::skip_empty_or_deleted(control_full_or_sentinel + 1)};
1348+
// slot_full_or_sentinel += full_or_sentinel - control_full_or_sentinel;
1349+
// control_full_or_sentinel = full_or_sentinel;
1350+
// }
13741351
}
13751352
}
13761353

@@ -1468,37 +1445,19 @@ namespace hud
14681445
{
14691446
// Move elements to new buffer if any
14701447
// Relocate slots to newly allocated buffer
1471-
// auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
1472-
// {
1473-
// // Compute the hash
1474-
// u64 hash {hasher_type {}(slot_ptr->key())};
1475-
// // Find H1 slot index
1476-
// u64 h1 {H1(hash)};
1477-
// usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
1478-
// // Save h2 in control h1 index
1479-
// control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
1480-
// // Move old slot to new slot
1481-
// hud::memory::move_or_copy_construct_object_then_destroy(slot_ptr_ + slot_index, hud::move(*slot_ptr));
1482-
// };
1483-
// iterate_over_full_slots(old_control_ptr, old_slot_ptr, insert_slot_by_copy);
1484-
1485-
auto [control_full_or_sentinel, slot_full_or_sentinel] = skip_empty_or_deleted(old_control_ptr, old_slot_ptr);
1486-
while (control_full_or_sentinel != old_control_ptr + old_max_slot_count)
1448+
auto insert_slot_by_copy = [this](control_type *control_ptr, auto *slot_ptr)
14871449
{
14881450
// Compute the hash
1489-
u64 hash {hasher_type {}(slot_full_or_sentinel->key())};
1451+
u64 hash {hasher_type {}(slot_ptr->key())};
14901452
// Find H1 slot index
14911453
u64 h1 {H1(hash)};
14921454
usize slot_index {find_first_empty_or_deleted(control_ptr_, max_slot_count_, h1)};
14931455
// Save h2 in control h1 index
14941456
control::set_h2(control_ptr_, slot_index, H2(hash), max_slot_count_);
14951457
// Move old slot to new slot
1496-
hud::memory::move_or_copy_construct_object_then_destroy(slot_ptr_ + slot_index, hud::move(*slot_full_or_sentinel));
1497-
1498-
control_type *full_or_sentinel {control::skip_empty_or_deleted(control_full_or_sentinel + 1)};
1499-
slot_full_or_sentinel += full_or_sentinel - control_full_or_sentinel;
1500-
control_full_or_sentinel = full_or_sentinel;
1501-
}
1458+
hud::memory::move_or_copy_construct_object_then_destroy(slot_ptr_ + slot_index, hud::move(*slot_ptr));
1459+
};
1460+
iterate_over_full_slots(old_control_ptr, old_slot_ptr, count_, old_max_slot_count, insert_slot_by_copy);
15021461
free_control_and_slot(old_control_ptr, old_slot_ptr, old_max_slot_count);
15031462
}
15041463
}
@@ -1670,18 +1629,18 @@ namespace hud
16701629
{
16711630
hud::memory::destroy_object(slot_ptr);
16721631
};
1673-
iterate_over_full_slots(control_ptr_, slot_ptr_, destroy_slot);
1632+
iterate_over_full_slots(control_ptr_, slot_ptr_, count_, max_slot_count_, destroy_slot);
16741633
}
16751634
}
16761635

1677-
constexpr void iterate_over_full_slots(control_type *control_ptr, auto *slot_ptr, auto callback) noexcept
1636+
constexpr void iterate_over_full_slots(control_type *control_ptr, auto *slot_ptr, usize slot_count, usize max_slot_count, auto callback) noexcept
16781637
{
16791638
// When max slot count is less than the probing group
16801639
// We have cloned control in the group
16811640
// In this case, we start probing at the sentinel instead of 0
1682-
if (max_slot_count_ < group_type::SLOT_PER_GROUP - 1)
1641+
if (max_slot_count < group_type::SLOT_PER_GROUP - 1)
16831642
{
1684-
group_type group {control_ptr_sentinel()};
1643+
group_type group {control_ptr + max_slot_count};
16851644

16861645
// In the case of constant expression
16871646
// If the hashmap is empty, slot_ptr is nullptr, we don't want to decrement the pointer in the case
@@ -1697,14 +1656,13 @@ namespace hud
16971656
}
16981657
else
16991658
{
1700-
size_t remaining_slots {count()};
1701-
while (remaining_slots != 0)
1659+
while (slot_count != 0)
17021660
{
17031661
group_type group {control_ptr};
17041662
for (u32 full_index : group.mask_of_full_slot())
17051663
{
17061664
callback(control_ptr + full_index, slot_ptr + full_index);
1707-
--remaining_slots;
1665+
--slot_count;
17081666
}
17091667
control_ptr += group_type::SLOT_PER_GROUP;
17101668
slot_ptr += group_type::SLOT_PER_GROUP;

0 commit comments

Comments
 (0)