summaryrefslogtreecommitdiff
path: root/src/hash_map.hh
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2022-08-05 19:20:00 +1000
committerMaxime Coste <mawww@kakoune.org>2022-08-05 19:20:00 +1000
commit9fb7d90449ce798bbc014ac6d17261a9df5ee3f2 (patch)
tree8983b0fa1b8c3a618e977e6df00914e4d6d56bb6 /src/hash_map.hh
parent31e9fc3cefcbea03d4eaad75ed0dab3da8f3dc6f (diff)
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
Diffstat (limited to 'src/hash_map.hh')
-rw-r--r--src/hash_map.hh20
1 files changed, 18 insertions, 2 deletions
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<typename Key, typename Value,
MemoryDomain domain = MemoryDomain::Undefined,
- template<typename, MemoryDomain> class Container = Vector>
+ template<typename, MemoryDomain> class Container = Vector,
+ bool multi_key = false>
struct HashMap
{
using Item = HashItem<Key, Value>;
@@ -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<domain, Container> m_index;
};
+template<typename Key, typename Value,
+ MemoryDomain domain = MemoryDomain::Undefined,
+ template<typename, MemoryDomain> class Container = Vector>
+ using MultiHashMap = HashMap<Key, Value, domain, Container, true>;
+
void profile_hash_maps();
}