From 73fc2b3748dbf13a05b1ea60e0fa97529df6283d Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 22 Jul 2021 19:19:34 +0430 Subject: [PATCH] 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. --- AK/AllOf.h | 6 +++--- AK/AnyOf.h | 6 +++--- AK/Find.h | 17 +++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/AK/AllOf.h b/AK/AllOf.h index 96a612decb2..a551046b977 100644 --- a/AK/AllOf.h +++ b/AK/AllOf.h @@ -11,10 +11,10 @@ namespace AK { -template +template TIterator> constexpr bool all_of( - SimpleIterator const& begin, - SimpleIterator const& end, + TIterator const& begin, + TEndIterator const& end, auto const& predicate) { for (auto iter = begin; iter != end; ++iter) { diff --git a/AK/AnyOf.h b/AK/AnyOf.h index 97bacefd462..f651f3eec62 100644 --- a/AK/AnyOf.h +++ b/AK/AnyOf.h @@ -12,10 +12,10 @@ namespace AK { -template +template TIterator> constexpr bool any_of( - SimpleIterator const& begin, - SimpleIterator const& end, + TIterator const& begin, + TEndIterator const& end, auto const& predicate) { return find_if(begin, end, predicate) != end; diff --git a/AK/Find.h b/AK/Find.h index f0f3359172c..7fe7f666bb4 100644 --- a/AK/Find.h +++ b/AK/Find.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace AK { -template -constexpr TIterator find_if(TIterator first, TIterator last, TUnaryPredicate&& pred) +template TIterator, typename TUnaryPredicate> +constexpr TIterator find_if(TIterator first, TEndIterator last, TUnaryPredicate&& pred) { for (; first != last; ++first) { if (pred(*first)) { @@ -22,16 +23,16 @@ constexpr TIterator find_if(TIterator first, TIterator last, TUnaryPredicate&& p return last; } -template -constexpr TIterator find(TIterator first, TIterator last, const T& value) +template TIterator, typename T> +constexpr TIterator find(TIterator first, TEndIterator last, T const& value) { - return find_if(first, last, [&](const auto& v) { return Traits::equals(value, v); }); + return find_if(first, last, [&](auto const& v) { return Traits::equals(value, v); }); } -template -constexpr size_t find_index(TIterator first, TIterator last, const T& value) +template TIterator, typename T> +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::equals(value, v); }).index(); + return find_if(first, last, [&](auto const& v) { return Traits::equals(value, v); }).index(); } }