mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
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).
This commit is contained in:
parent
f879e04133
commit
d40d10aae7
Notes:
sideshowbarker
2024-07-18 08:32:01 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/d40d10aae72 Pull-request: https://github.com/SerenityOS/serenity/pull/8933
4 changed files with 101 additions and 0 deletions
11
AK/AllOf.h
11
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;
|
||||
|
|
11
AK/AnyOf.h
11
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;
|
||||
|
|
|
@ -19,3 +19,41 @@ TEST_CASE(should_determine_if_predicate_applies_to_all_elements_in_container)
|
|||
EXPECT(all_of(a.begin(), a.end(), [](auto elem) { return elem == 0; }));
|
||||
EXPECT(!all_of(a.begin(), a.end(), [](auto elem) { return elem == 1; }));
|
||||
}
|
||||
|
||||
TEST_CASE(container_form)
|
||||
{
|
||||
constexpr Array a { 10, 20, 30 };
|
||||
static_assert(all_of(a, [](auto elem) { return elem > 0; }));
|
||||
static_assert(!all_of(a, [](auto elem) { return elem > 10; }));
|
||||
EXPECT(all_of(a, [](auto elem) { return elem > 0; }));
|
||||
EXPECT(!all_of(a, [](auto elem) { return elem > 10; }));
|
||||
|
||||
Vector b { 10, 20, 30 };
|
||||
EXPECT(all_of(b, [](auto elem) { return elem > 0; }));
|
||||
EXPECT(!all_of(b, [](auto elem) { return elem > 10; }));
|
||||
|
||||
struct ArbitraryIterable {
|
||||
struct ArbitraryIterator {
|
||||
ArbitraryIterator(int v)
|
||||
: value(v)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(ArbitraryIterator const&) const = default;
|
||||
int operator*() const { return value; }
|
||||
ArbitraryIterator& operator++()
|
||||
{
|
||||
++value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int value;
|
||||
};
|
||||
ArbitraryIterator begin() const { return 0; }
|
||||
ArbitraryIterator end() const { return 20; }
|
||||
};
|
||||
|
||||
ArbitraryIterable c;
|
||||
EXPECT(all_of(c, [](auto elem) { return elem < 20; }));
|
||||
EXPECT(!all_of(c, [](auto elem) { return elem > 10; }));
|
||||
}
|
||||
|
|
|
@ -21,3 +21,44 @@ TEST_CASE(should_determine_if_predicate_applies_to_any_element_in_container)
|
|||
EXPECT(any_of(a.begin(), a.end(), [](auto elem) { return elem == 1; }));
|
||||
EXPECT(!any_of(a.begin(), a.end(), [](auto elem) { return elem == 2; }));
|
||||
}
|
||||
|
||||
TEST_CASE(container_form)
|
||||
{
|
||||
constexpr Array a { 10, 20, 30 };
|
||||
static_assert(any_of(a, [](auto elem) { return elem == 10; }));
|
||||
static_assert(any_of(a, [](auto elem) { return elem == 20; }));
|
||||
static_assert(!any_of(a, [](auto elem) { return elem == 40; }));
|
||||
|
||||
EXPECT(any_of(a, [](auto elem) { return elem == 10; }));
|
||||
EXPECT(any_of(a, [](auto elem) { return elem == 20; }));
|
||||
EXPECT(!any_of(a, [](auto elem) { return elem == 40; }));
|
||||
|
||||
Vector b { 10, 20, 30 };
|
||||
EXPECT(any_of(b, [](auto elem) { return elem > 10; }));
|
||||
EXPECT(!any_of(b, [](auto elem) { return elem > 40; }));
|
||||
|
||||
struct ArbitraryIterable {
|
||||
struct ArbitraryIterator {
|
||||
ArbitraryIterator(int v)
|
||||
: value(v)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(ArbitraryIterator const&) const = default;
|
||||
int operator*() const { return value; }
|
||||
ArbitraryIterator& operator++()
|
||||
{
|
||||
++value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int value;
|
||||
};
|
||||
ArbitraryIterator begin() const { return 0; }
|
||||
ArbitraryIterator end() const { return 20; }
|
||||
};
|
||||
|
||||
ArbitraryIterable c;
|
||||
EXPECT(any_of(c, [](auto elem) { return elem < 20; }));
|
||||
EXPECT(!any_of(c, [](auto elem) { return elem > 31; }));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue