mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Rewrite {AnyOf,AllOf,Find}.h to use the IteratorPairWith concept
This makes it so these algorithms are usable with arbitrary iterators, as opposed to just instances of AK::SimpleIterator. This commit also makes the requirement of ::index() in find_index() explicit, as previously it was accepting any iterator.
This commit is contained in:
parent
2dc31c503e
commit
73fc2b3748
Notes:
sideshowbarker
2024-07-18 08:31:54 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/73fc2b3748d Pull-request: https://github.com/SerenityOS/serenity/pull/8933
3 changed files with 15 additions and 14 deletions
|
@ -11,10 +11,10 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
template<typename Container, typename ValueType>
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator>
|
||||||
constexpr bool all_of(
|
constexpr bool all_of(
|
||||||
SimpleIterator<Container, ValueType> const& begin,
|
TIterator const& begin,
|
||||||
SimpleIterator<Container, ValueType> const& end,
|
TEndIterator const& end,
|
||||||
auto const& predicate)
|
auto const& predicate)
|
||||||
{
|
{
|
||||||
for (auto iter = begin; iter != end; ++iter) {
|
for (auto iter = begin; iter != end; ++iter) {
|
||||||
|
|
|
@ -12,10 +12,10 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
template<typename Container, typename ValueType>
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator>
|
||||||
constexpr bool any_of(
|
constexpr bool any_of(
|
||||||
SimpleIterator<Container, ValueType> const& begin,
|
TIterator const& begin,
|
||||||
SimpleIterator<Container, ValueType> const& end,
|
TEndIterator const& end,
|
||||||
auto const& predicate)
|
auto const& predicate)
|
||||||
{
|
{
|
||||||
return find_if(begin, end, predicate) != end;
|
return find_if(begin, end, predicate) != end;
|
||||||
|
|
17
AK/Find.h
17
AK/Find.h
|
@ -6,13 +6,14 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Concepts.h>
|
||||||
#include <AK/Traits.h>
|
#include <AK/Traits.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
template<typename TIterator, typename TUnaryPredicate>
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename TUnaryPredicate>
|
||||||
constexpr TIterator find_if(TIterator first, TIterator last, TUnaryPredicate&& pred)
|
constexpr TIterator find_if(TIterator first, TEndIterator last, TUnaryPredicate&& pred)
|
||||||
{
|
{
|
||||||
for (; first != last; ++first) {
|
for (; first != last; ++first) {
|
||||||
if (pred(*first)) {
|
if (pred(*first)) {
|
||||||
|
@ -22,16 +23,16 @@ constexpr TIterator find_if(TIterator first, TIterator last, TUnaryPredicate&& p
|
||||||
return last;
|
return last;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TIterator, typename T>
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename T>
|
||||||
constexpr TIterator find(TIterator first, TIterator last, const T& value)
|
constexpr TIterator find(TIterator first, TEndIterator last, T const& value)
|
||||||
{
|
{
|
||||||
return find_if(first, last, [&](const auto& v) { return Traits<T>::equals(value, v); });
|
return find_if(first, last, [&](auto const& v) { return Traits<T>::equals(value, v); });
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TIterator, typename T>
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename T>
|
||||||
constexpr size_t find_index(TIterator first, TIterator last, const T& value)
|
constexpr size_t find_index(TIterator first, TEndIterator last, T const& value) requires(requires(TIterator it) { it.index(); })
|
||||||
{
|
{
|
||||||
return find_if(first, last, [&](const auto& v) { return Traits<T>::equals(value, v); }).index();
|
return find_if(first, last, [&](auto const& v) { return Traits<T>::equals(value, v); }).index();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue