AK: Support using custom comparison operations for hash compatible keys

This commit is contained in:
Idan Horowitz 2022-01-29 20:01:35 +02:00
parent 4a99170cd2
commit 9b0d90a71d
3 changed files with 8 additions and 8 deletions

View file

@ -119,17 +119,16 @@ public:
return m_table.find(hash, predicate);
}
// FIXME: Use some sort of Traits to get the comparison operation
template<Concepts::HashCompatible<K> Key>
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] IteratorType find(Key const& value)
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] IteratorType find(Key const& key)
{
return m_table.find(Traits<Key>::hash(value), [&](auto& entry) { return value == entry.key; });
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(key, entry.key); });
}
template<Concepts::HashCompatible<K> Key>
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] ConstIteratorType find(Key const& value) const
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] ConstIteratorType find(Key const& key) const
{
return m_table.find(Traits<Key>::hash(value), [&](auto& entry) { return value == entry.key; });
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(key, entry.key); });
}
void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); }

View file

@ -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<Concepts::HashCompatible<T> K>
requires(IsSame<TraitsForT, Traits<T>>) [[nodiscard]] Iterator find(K const& value)
{
return find(Traits<K>::hash(value), [&](auto& other) { return value == other; });
return find(Traits<K>::hash(value), [&](auto& other) { return Traits<T>::equals(other, value); });
}
template<Concepts::HashCompatible<T> K, typename TUnaryPredicate>
@ -360,7 +359,7 @@ public:
template<Concepts::HashCompatible<T> K>
requires(IsSame<TraitsForT, Traits<T>>) [[nodiscard]] ConstIterator find(K const& value) const
{
return find(Traits<K>::hash(value), [&](auto& other) { return value == other; });
return find(Traits<K>::hash(value), [&](auto& other) { return Traits<T>::equals(other, value); });
}
template<Concepts::HashCompatible<T> K, typename TUnaryPredicate>

View file

@ -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<Concepts::HashCompatible<T> U>
static bool equals(U const& a, T const& b) { return a == b; }
};
template<typename T>