mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Support using custom comparison operations for hash compatible keys
This commit is contained in:
parent
4a99170cd2
commit
9b0d90a71d
Notes:
sideshowbarker
2024-07-17 20:01:10 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/9b0d90a71d7 Pull-request: https://github.com/SerenityOS/serenity/pull/12205 Reviewed-by: https://github.com/bgianfo ✅
3 changed files with 8 additions and 8 deletions
|
@ -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); }
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue