diff --git a/AK/HashMap.h b/AK/HashMap.h index 89c83a77e18..2fde4b9b7b6 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -119,17 +119,16 @@ public: return m_table.find(hash, predicate); } - // FIXME: Use some sort of Traits to get the comparison operation template Key> - requires(IsSame>) [[nodiscard]] IteratorType find(Key const& value) + requires(IsSame>) [[nodiscard]] IteratorType find(Key const& key) { - return m_table.find(Traits::hash(value), [&](auto& entry) { return value == entry.key; }); + return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(key, entry.key); }); } template Key> - requires(IsSame>) [[nodiscard]] ConstIteratorType find(Key const& value) const + requires(IsSame>) [[nodiscard]] ConstIteratorType find(Key const& key) const { - return m_table.find(Traits::hash(value), [&](auto& entry) { return value == entry.key; }); + return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(key, entry.key); }); } void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); } diff --git a/AK/HashTable.h b/AK/HashTable.h index 8ecf2958191..6b797b4fcbc 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -342,13 +342,12 @@ public: { return find(TraitsForT::hash(value), [&](auto& other) { return TraitsForT::equals(value, other); }); } - // FIXME: Use some Traits to get the comparison operation // FIXME: Support for predicates, while guaranteeing that the predicate call // does not call a non trivial constructor each time invoked template K> requires(IsSame>) [[nodiscard]] Iterator find(K const& value) { - return find(Traits::hash(value), [&](auto& other) { return value == other; }); + return find(Traits::hash(value), [&](auto& other) { return Traits::equals(other, value); }); } template K, typename TUnaryPredicate> @@ -360,7 +359,7 @@ public: template K> requires(IsSame>) [[nodiscard]] ConstIterator find(K const& value) const { - return find(Traits::hash(value), [&](auto& other) { return value == other; }); + return find(Traits::hash(value), [&](auto& other) { return Traits::equals(other, value); }); } template K, typename TUnaryPredicate> diff --git a/AK/Traits.h b/AK/Traits.h index b1d60f325f7..a797d4da966 100644 --- a/AK/Traits.h +++ b/AK/Traits.h @@ -20,6 +20,8 @@ struct GenericTraits { using ConstPeekType = T; static constexpr bool is_trivial() { return false; } static constexpr bool equals(const T& a, const T& b) { return a == b; } + template U> + static bool equals(U const& a, T const& b) { return a == b; } }; template