mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:47:46 +00:00
AK: Allow hash-compatible key types in Hash[Table|Map] lookup
This will allow us to avoid some potentially expensive type conversion during lookup, like form String to StringView, which would allocate memory otherwise.
This commit is contained in:
parent
c8bee92fb9
commit
d50360f5dd
2 changed files with 79 additions and 0 deletions
46
AK/HashMap.h
46
AK/HashMap.h
|
@ -91,6 +91,19 @@ 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)
|
||||
{
|
||||
return m_table.find(Traits<Key>::hash(value), [&](auto& entry) { return value == entry.key; });
|
||||
}
|
||||
|
||||
template<Concepts::HashCompatible<K> Key>
|
||||
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] ConstIteratorType find(Key const& value) const
|
||||
{
|
||||
return m_table.find(Traits<Key>::hash(value), [&](auto& entry) { return value == entry.key; });
|
||||
}
|
||||
|
||||
void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); }
|
||||
ErrorOr<void> try_ensure_capacity(size_t capacity) { return m_table.try_ensure_capacity(capacity); }
|
||||
|
||||
|
@ -118,11 +131,44 @@ public:
|
|||
return (*it).value;
|
||||
}
|
||||
|
||||
template<Concepts::HashCompatible<K> Key>
|
||||
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename Traits<V>::PeekType> get(const Key& key) const requires(!IsPointer<typename Traits<V>::PeekType>)
|
||||
{
|
||||
auto it = find(key);
|
||||
if (it == end())
|
||||
return {};
|
||||
return (*it).value;
|
||||
}
|
||||
|
||||
template<Concepts::HashCompatible<K> Key>
|
||||
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename Traits<V>::ConstPeekType> get(const Key& key) const requires(IsPointer<typename Traits<V>::PeekType>)
|
||||
{
|
||||
auto it = find(key);
|
||||
if (it == end())
|
||||
return {};
|
||||
return (*it).value;
|
||||
}
|
||||
|
||||
template<Concepts::HashCompatible<K> Key>
|
||||
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename Traits<V>::PeekType> get(const Key& key) requires(!IsConst<typename Traits<V>::PeekType>)
|
||||
{
|
||||
auto it = find(key);
|
||||
if (it == end())
|
||||
return {};
|
||||
return (*it).value;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool contains(const K& key) const
|
||||
{
|
||||
return find(key) != end();
|
||||
}
|
||||
|
||||
template<Concepts::HashCompatible<K> Key>
|
||||
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] bool contains(Key const& value)
|
||||
{
|
||||
return find(value) != end();
|
||||
}
|
||||
|
||||
void remove(IteratorType it)
|
||||
{
|
||||
m_table.remove(it);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue