2020-12-23 18:35:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-23 18:35:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-22 14:49:34 +00:00
|
|
|
#include <AK/Concepts.h>
|
2020-12-23 18:35:01 +00:00
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2021-07-22 14:49:34 +00:00
|
|
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename TUnaryPredicate>
|
2022-06-26 16:21:14 +00:00
|
|
|
[[nodiscard]] constexpr TIterator find_if(TIterator first, TEndIterator last, TUnaryPredicate&& pred)
|
2020-12-23 18:35:01 +00:00
|
|
|
{
|
|
|
|
for (; first != last; ++first) {
|
|
|
|
if (pred(*first)) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:49:34 +00:00
|
|
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename T>
|
2022-06-26 16:21:14 +00:00
|
|
|
[[nodiscard]] constexpr TIterator find(TIterator first, TEndIterator last, T const& value)
|
2020-12-23 18:35:01 +00:00
|
|
|
{
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 14:38:11 +00:00
|
|
|
// FIXME: Use the iterator's trait type, and swap arguments in equals call.
|
2021-07-22 14:49:34 +00:00
|
|
|
return find_if(first, last, [&](auto const& v) { return Traits<T>::equals(value, v); });
|
2020-12-23 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 14:49:34 +00:00
|
|
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
[[nodiscard]] constexpr size_t find_index(TIterator first, TEndIterator last, T const& value)
|
|
|
|
requires(requires(TIterator it) { it.index(); })
|
2020-12-23 18:35:01 +00:00
|
|
|
{
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 14:38:11 +00:00
|
|
|
// FIXME: Use the iterator's trait type, and swap arguments in equals call.
|
2021-07-22 14:49:34 +00:00
|
|
|
return find_if(first, last, [&](auto const& v) { return Traits<T>::equals(value, v); }).index();
|
2020-12-23 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|