diff --git a/AK/Find.h b/AK/Find.h index fcdab746ba..7b99508669 100644 --- a/AK/Find.h +++ b/AK/Find.h @@ -26,6 +26,7 @@ template TIterator, typena template TIterator, typename T> [[nodiscard]] constexpr TIterator find(TIterator first, TEndIterator last, T const& value) { + // FIXME: Use the iterator's trait type, and swap arguments in equals call. return find_if(first, last, [&](auto const& v) { return Traits::equals(value, v); }); } @@ -33,6 +34,7 @@ template TIterator, typena [[nodiscard]] constexpr size_t find_index(TIterator first, TEndIterator last, T const& value) requires(requires(TIterator it) { it.index(); }) { + // FIXME: Use the iterator's trait type, and swap arguments in equals call. return find_if(first, last, [&](auto const& v) { return Traits::equals(value, v); }).index(); } diff --git a/AK/HashMap.h b/AK/HashMap.h index 5cd71c110d..b8c4f122ee 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -98,7 +98,7 @@ public: [[nodiscard]] IteratorType end() { return m_table.end(); } [[nodiscard]] IteratorType find(K const& key) { - return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); }); + return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); }); } template [[nodiscard]] IteratorType find(unsigned hash, TUnaryPredicate predicate) @@ -110,7 +110,7 @@ public: [[nodiscard]] ConstIteratorType end() const { return m_table.end(); } [[nodiscard]] ConstIteratorType find(K const& key) const { - return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); }); + return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); }); } template [[nodiscard]] ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const @@ -121,13 +121,13 @@ public: template Key> requires(IsSame>) [[nodiscard]] IteratorType find(Key const& key) { - return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(key, entry.key); }); + 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 { - return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(key, entry.key); }); + return m_table.find(Traits::hash(key), [&](auto& entry) { return Traits::equals(entry.key, key); }); } ErrorOr try_ensure_capacity(size_t capacity) { return m_table.try_ensure_capacity(capacity); } diff --git a/AK/HashTable.h b/AK/HashTable.h index 46388d80f6..56794d6047 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -317,7 +317,7 @@ public: [[nodiscard]] Iterator find(T const& value) { - return find(TraitsForT::hash(value), [&](auto& other) { return TraitsForT::equals(value, other); }); + return find(TraitsForT::hash(value), [&](auto& entry) { return TraitsForT::equals(entry, value); }); } template @@ -328,14 +328,14 @@ public: [[nodiscard]] ConstIterator find(T const& value) const { - return find(TraitsForT::hash(value), [&](auto& other) { return TraitsForT::equals(value, other); }); + return find(TraitsForT::hash(value), [&](auto& entry) { return TraitsForT::equals(entry, value); }); } // FIXME: Support for predicates, while guaranteeing that the predicate call // does not call a non trivial constructor each time invoked template K> requires(IsSame>) [[nodiscard]] Iterator find(K const& value) { - return find(Traits::hash(value), [&](auto& other) { return Traits::equals(other, value); }); + return find(Traits::hash(value), [&](auto& entry) { return Traits::equals(entry, value); }); } template K, typename TUnaryPredicate> @@ -347,7 +347,7 @@ public: template K> requires(IsSame>) [[nodiscard]] ConstIterator find(K const& value) const { - return find(Traits::hash(value), [&](auto& other) { return Traits::equals(other, value); }); + return find(Traits::hash(value), [&](auto& entry) { return Traits::equals(entry, value); }); } template K, typename TUnaryPredicate> diff --git a/AK/SinglyLinkedList.h b/AK/SinglyLinkedList.h index 488d1f93d5..f78ad50996 100644 --- a/AK/SinglyLinkedList.h +++ b/AK/SinglyLinkedList.h @@ -224,12 +224,12 @@ public: ConstIterator find(T const& value) const { - return find_if([&](auto& other) { return Traits::equals(value, other); }); + return find_if([&](auto& entry) { return Traits::equals(entry, value); }); } Iterator find(T const& value) { - return find_if([&](auto& other) { return Traits::equals(value, other); }); + return find_if([&](auto& entry) { return Traits::equals(entry, value); }); } template diff --git a/AK/Traits.h b/AK/Traits.h index 29334b41f7..d6e975f46a 100644 --- a/AK/Traits.h +++ b/AK/Traits.h @@ -22,7 +22,7 @@ struct GenericTraits { static constexpr bool is_trivially_serializable() { return false; } static constexpr bool equals(T const& a, T const& b) { return a == b; } template U> - static bool equals(U const& a, T const& b) { return a == b; } + static bool equals(T const& self, U const& other) { return self == other; } }; template diff --git a/Kernel/Library/KString.h b/Kernel/Library/KString.h index 7905936dbe..80928ef8b5 100644 --- a/Kernel/Library/KString.h +++ b/Kernel/Library/KString.h @@ -91,7 +91,7 @@ struct Traits> : public GenericTraits const& p) { return string_hash(p->characters(), p->length()); } static bool equals(NonnullOwnPtr const& a, NonnullOwnPtr const& b) { return a->view() == b->view(); } - static bool equals(StringView a, NonnullOwnPtr const& b) { return a == b->view(); } + static bool equals(NonnullOwnPtr const& a, StringView b) { return a->view() == b; } }; template<> @@ -113,11 +113,11 @@ struct Traits> : public GenericTraitsview() == b->view(); } - static bool equals(StringView a, OwnPtr const& b) + static bool equals(OwnPtr const& a, StringView b) { - if (!b) - return a.is_null(); - return a == b->view(); + if (!a) + return b.is_null(); + return a->view() == b; } }; diff --git a/Tests/AK/TestVector.cpp b/Tests/AK/TestVector.cpp index 6bf010b8b7..5e63dee85d 100644 --- a/Tests/AK/TestVector.cpp +++ b/Tests/AK/TestVector.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include TEST_CASE(construct) @@ -431,6 +432,13 @@ TEST_CASE(should_find_predicate_index) EXPECT(!v.find_first_index_if([](auto const v) { return v == 123; }).has_value()); } +TEST_CASE(should_find_using_a_hashcompatible_value) +{ + // Tests whether a hash-compatible value can be used to compare (Strings cannot be impliticly constructed from a StringView.) + Vector v { "hello!"_string }; + EXPECT(v.contains_slow("hello!"sv)); +} + TEST_CASE(should_contain_start) { // Tests whether value is found if at the start of the range.