From 096d72bbb11e3a2ec227de9eda4dfac02ed970aa Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 26 Jun 2025 11:33:58 +1000 Subject: Delay destruction of item in hash map without returning an optional Returning an optional of the removed item is not very idiomatic and we currently have no use case for this. --- src/hash_map.hh | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/hash_map.hh b/src/hash_map.hh index dc6263e5..60ebc6ab 100644 --- a/src/hash_map.hh +++ b/src/hash_map.hh @@ -3,7 +3,6 @@ #include "hash.hh" #include "memory.hh" -#include "optional.hh" #include "vector.hh" namespace Kakoune @@ -269,56 +268,50 @@ struct HashMap } template requires IsHashCompatible - constexpr Optional remove(const KeyType& key) + constexpr void remove(const KeyType& key) { - Optional removed; const auto hash = hash_value(key); int index = find_index(key, hash); if (index >= 0) { - removed.emplace(std::move(m_items[index])); + [[maybe_unused]] Item keepalive = std::move(m_items[index]); m_items.erase(m_items.begin() + index); m_index.remove(hash, index); m_index.ordered_fix_entries(index); } - return removed; } template requires IsHashCompatible - constexpr Optional unordered_remove(const KeyType& key) + constexpr void unordered_remove(const KeyType& key) { - Optional removed; const auto hash = hash_value(key); int index = find_index(key, hash); if (index >= 0) { - removed.emplace(std::move(m_items[index])); + [[maybe_unused]] Item keepalive = std::move(m_items[index]); m_items[index] = std::move(m_items.back()); m_items.pop_back(); m_index.remove(hash, index); if (index != m_items.size()) m_index.unordered_fix_entries(hash_value(item_key(m_items[index])), m_items.size(), index); } - return removed; } template requires IsHashCompatible - constexpr Optional erase(const KeyType& key) { return unordered_remove(key); } + constexpr void erase(const KeyType& key) { return unordered_remove(key); } template requires IsHashCompatible - constexpr Vector remove_all(const KeyType& key) + constexpr void remove_all(const KeyType& key) { - Vector removed; const auto hash = hash_value(key); for (int index = find_index(key, hash); index >= 0; index = find_index(key, hash)) { - removed.push_back(std::move(m_items[index])); + [[maybe_unused]] Item keepalive = std::move(m_items[index]); m_items.erase(m_items.begin() + index); m_index.remove(hash, index); m_index.ordered_fix_entries(index); } - return removed; } using iterator = typename ContainerType::iterator; -- cgit v1.2.3