ladybird/AK/AllOf.h
Ali Mohammad Pur d40d10aae7 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).
2021-07-22 22:56:20 +02:00

40 lines
787 B
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Concepts.h>
#include <AK/Iterator.h>
namespace AK {
template<typename Container, typename ValueType>
constexpr bool all_of(
const SimpleIterator<Container, ValueType>& begin,
const SimpleIterator<Container, ValueType>& end,
const auto& predicate)
{
for (auto iter = begin; iter != end; ++iter) {
if (!predicate(*iter)) {
return false;
}
}
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;