diff --git a/AK/HashMap.h b/AK/HashMap.h index 89c83a77e18d55e8a341dfb20e818e91b780a755..2fde4b9b7b6541c79be410bc0ba43386e6c0daae 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 8ecf295819132049efa76dc3d784d36a26222414..6b797b4fcbcd80f719e9033f2daa42d2649a6396 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 b1d60f325f7d2089c0f747563cb54ac83c4f6822..a797d4da9661b646dae34592ac58138597aeb433 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