summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-07-22 19:11:00 +0430
committerAndreas Kling <kling@serenityos.org>2021-07-22 22:56:20 +0200
commitd40d10aae72d1d6d04b818d2173e5c5f5a731384 (patch)
tree765ab8adb42259008759804e30af7c071869d55b /AK
parentf879e041339ee414ddb9514a0883baa66ce34f6d (diff)
downloadserenity-d40d10aae72d1d6d04b818d2173e5c5f5a731384.zip
AK: Implement {any,all}_of(IterableContainer&&, Predicate)
This is a generally nicer-to-use version of the existing {any,all}_of() that doesn't require the user to explicitly provide two iterators. As a bonus, it also allows arbitrary iterators (as opposed to the hard requirement of providing SimpleIterators in the iterator version).
Diffstat (limited to 'AK')
-rw-r--r--AK/AllOf.h11
-rw-r--r--AK/AnyOf.h11
2 files changed, 22 insertions, 0 deletions
diff --git a/AK/AllOf.h b/AK/AllOf.h
index 41a8095d1f..1248ddb471 100644
--- a/AK/AllOf.h
+++ b/AK/AllOf.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Concepts.h>
#include <AK/Iterator.h>
namespace AK {
@@ -24,6 +25,16 @@ constexpr bool all_of(
return true;
}
+template<IterableContainer Container>
+constexpr bool all_of(Container&& container, auto const& predicate)
+{
+ for (auto&& entry : container) {
+ if (!predicate(entry))
+ return false;
+ }
+ return true;
+}
+
}
using AK::all_of;
diff --git a/AK/AnyOf.h b/AK/AnyOf.h
index 201a875fda..034c1d57ea 100644
--- a/AK/AnyOf.h
+++ b/AK/AnyOf.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Concepts.h>
#include <AK/Find.h>
#include <AK/Iterator.h>
@@ -20,6 +21,16 @@ constexpr bool any_of(
return find_if(begin, end, predicate) != end;
}
+template<IterableContainer Container>
+constexpr bool any_of(Container&& container, auto const& predicate)
+{
+ for (auto&& entry : container) {
+ if (predicate(entry))
+ return true;
+ }
+ return false;
+}
+
}
using AK::any_of;