From d02fe6b67e3d4cbe435630e703769aae7516871e Mon Sep 17 00:00:00 2001 From: Alex Budkar Date: Wed, 27 Sep 2023 12:20:09 -0700 Subject: [PATCH] Fix return value of erase in bytell_hash_* This is a fix for issue: https://github.com/skarupke/flat_hash_map/issues/41 --- bytell_hash_map.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/bytell_hash_map.hpp b/bytell_hash_map.hpp index 9c9a246..e2c3d19 100644 --- a/bytell_hash_map.hpp +++ b/bytell_hash_map.hpp @@ -597,6 +597,11 @@ class sherwood_v8_table : private ByteAlloc, private Hasher, private Equal AllocatorTraits::destroy(*this, std::addressof(*next)); next.set_metadata(Constants::magic_for_empty); previous.clear_next(); + // The iteration order in the map after erase is changed for future iterations over the map + // But it only changed in the portion of the map before `const_iterator to_erase` (indexes greater than current.index) + // We didn't touch the order of the elements after `const_iterator to_erase` (indexes smaller than current.index) + // so we can just return convertible_to_iterator{ to_erase.current, to_erase.index }, + // which will advance to the next element upon conversion to iterator } else { @@ -1019,19 +1024,16 @@ class sherwood_v8_table : private ByteAlloc, private Hasher, private Equal BlockPointer it; size_t index; + // We always have to move to the next element upon conversion, + // because if it's not empty it's something that we already + // visited and moved to the current position operator iterator() { - if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty) - return ++iterator{it, index}; - else - return { it, index }; + return ++iterator{it, index}; } operator const_iterator() { - if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty) - return ++iterator{it, index}; - else - return { it, index }; + return ++iterator{it, index}; } }; };