From d50360f5dd97505e057c1d0dc92717a745576feb Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Sun, 7 Nov 2021 14:52:20 +0100 Subject: 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. --- AK/HashMap.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'AK/HashMap.h') diff --git a/AK/HashMap.h b/AK/HashMap.h index 5303bdf32d..78a307e3ee 100644 --- a/AK/HashMap.h +++ b/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 Key> + requires(IsSame>) [[nodiscard]] IteratorType find(Key const& value) + { + return m_table.find(Traits::hash(value), [&](auto& entry) { return value == entry.key; }); + } + + template Key> + requires(IsSame>) [[nodiscard]] ConstIteratorType find(Key const& value) const + { + return m_table.find(Traits::hash(value), [&](auto& entry) { return value == entry.key; }); + } + void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); } ErrorOr try_ensure_capacity(size_t capacity) { return m_table.try_ensure_capacity(capacity); } @@ -118,11 +131,44 @@ public: return (*it).value; } + template Key> + requires(IsSame>) Optional::PeekType> get(const Key& key) const requires(!IsPointer::PeekType>) + { + auto it = find(key); + if (it == end()) + return {}; + return (*it).value; + } + + template Key> + requires(IsSame>) Optional::ConstPeekType> get(const Key& key) const requires(IsPointer::PeekType>) + { + auto it = find(key); + if (it == end()) + return {}; + return (*it).value; + } + + template Key> + requires(IsSame>) Optional::PeekType> get(const Key& key) requires(!IsConst::PeekType>) + { + auto it = find(key); + if (it == end()) + return {}; + return (*it).value; + } + [[nodiscard]] bool contains(const K& key) const { return find(key) != end(); } + template Key> + requires(IsSame>) [[nodiscard]] bool contains(Key const& value) + { + return find(value) != end(); + } + void remove(IteratorType it) { m_table.remove(it); -- cgit v1.2.3