From 9fb7d90449ce798bbc014ac6d17261a9df5ee3f2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 5 Aug 2022 19:20:00 +1000 Subject: [PATCH] Change HashMap not to support multiple identical keys by default We do not seem to have any uses for this remaining, and this is better opt-in with MultiHashMap --- src/hash_map.cc | 13 ++++++++++++- src/hash_map.hh | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/hash_map.cc b/src/hash_map.cc index 4e45ab26..6972f34b 100644 --- a/src/hash_map.cc +++ b/src/hash_map.cc @@ -30,12 +30,23 @@ UnitTest test_hash_map{[] { kak_assert(map.size() == 2); } - // Multiple entries with the same key + // Replace Multiple entries with the same key { HashMap map; map.insert({10, 1}); map.insert({10, 2}); kak_assert(map.find_index(10) == 0); + kak_assert(map[10] == 2); + map.remove(10); + kak_assert(map.find_index(10) == -1); + } + + // Multiple entries with the same key + { + MultiHashMap map; + map.insert({10, 1}); + map.insert({10, 2}); + kak_assert(map.find_index(10) == 0); map.remove(10); kak_assert(map.find_index(10) == 0); map.remove(10); diff --git a/src/hash_map.hh b/src/hash_map.hh index b7ae4494..759cdbe7 100644 --- a/src/hash_map.hh +++ b/src/hash_map.hh @@ -160,7 +160,8 @@ struct HashItem template class Container = Vector> + template class Container = Vector, + bool multi_key = false> struct HashMap { using Item = HashItem; @@ -176,8 +177,18 @@ struct HashMap constexpr Value& insert(Item item) { + const auto hash = hash_value(item.key); + if constexpr (not multi_key) + { + if (auto index = find_index(item.key, hash); index >= 0) + { + m_items[index] = std::move(item); + return m_items[index].value; + } + } + m_index.reserve(m_items.size()+1); - m_index.add(hash_value(item.key), (int)m_items.size()); + m_index.add(hash, (int)m_items.size()); m_items.push_back(std::move(item)); return m_items.back().value; } @@ -315,6 +326,11 @@ private: HashIndex m_index; }; +template class Container = Vector> + using MultiHashMap = HashMap; + void profile_hash_maps(); }