diff --git a/AK/HashMap.h b/AK/HashMap.h index 32c31f06d09..8f25ac2c251 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -101,6 +101,8 @@ public: [[nodiscard]] IteratorType end() { return m_table.end(); } [[nodiscard]] IteratorType find(K const& key) { + if (m_table.is_empty()) + return m_table.end(); return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); }); } template @@ -113,6 +115,8 @@ public: [[nodiscard]] ConstIteratorType end() const { return m_table.end(); } [[nodiscard]] ConstIteratorType find(K const& key) const { + if (m_table.is_empty()) + return m_table.end(); return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); }); } template @@ -124,12 +128,16 @@ public: template Key> requires(IsSame>) [[nodiscard]] IteratorType find(Key const& key) { + if (m_table.is_empty()) + return m_table.end(); return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(entry.key, key); }); } template Key> requires(IsSame>) [[nodiscard]] ConstIteratorType find(Key const& key) const { + if (m_table.is_empty()) + return m_table.end(); return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(entry.key, key); }); } diff --git a/AK/HashTable.h b/AK/HashTable.h index 45bd3701246..0b8f679105f 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -378,6 +378,8 @@ public: [[nodiscard]] Iterator find(T const& value) { + if (is_empty()) + return end(); return find(TraitsForT::hash(value), [&](auto& entry) { return TraitsForT::equals(entry, value); }); } @@ -389,6 +391,8 @@ public: [[nodiscard]] ConstIterator find(T const& value) const { + if (is_empty()) + return end(); return find(TraitsForT::hash(value), [&](auto& entry) { return TraitsForT::equals(entry, value); }); } // FIXME: Support for predicates, while guaranteeing that the predicate call @@ -396,24 +400,32 @@ public: template K> requires(IsSame>) [[nodiscard]] Iterator find(K const& value) { + if (is_empty()) + return end(); return find(Traits::hash(value), [&](auto& entry) { return Traits::equals(entry, value); }); } template K, typename TUnaryPredicate> requires(IsSame>) [[nodiscard]] Iterator find(K const& value, TUnaryPredicate predicate) { + if (is_empty()) + return end(); return find(Traits::hash(value), move(predicate)); } template K> requires(IsSame>) [[nodiscard]] ConstIterator find(K const& value) const { + if (is_empty()) + return end(); return find(Traits::hash(value), [&](auto& entry) { return Traits::equals(entry, value); }); } template K, typename TUnaryPredicate> requires(IsSame>) [[nodiscard]] ConstIterator find(K const& value, TUnaryPredicate predicate) const { + if (is_empty()) + return end(); return find(Traits::hash(value), move(predicate)); }