diff --git a/src/utils/algorithm.h b/src/utils/algorithm.h index e669bf9d7..50fcfb62a 100644 --- a/src/utils/algorithm.h +++ b/src/utils/algorithm.h @@ -1,82 +1,92 @@ /* utils/algorithm.h This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include #include namespace Kleo { template ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value) { const ForwardIterator it = std::lower_bound(first, last, value); return (it == last || value < *it) ? last : it; } template ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value, Compare comp) { const ForwardIterator it = std::lower_bound(first, last, value, comp); return (it == last || comp(value, *it)) ? last : it; } template Container transformInPlace(Container &&c, UnaryOperation op) { std::transform(std::begin(c), std::end(c), std::begin(c), op); return std::move(c); } +/** Convenience helper for checking if the predicate @p p returns @c true + * for all elements in the range @p range. Returns @c true if the range is empty. + * Use ranges::all_of() instead if you can use C++20. + */ +template +bool all_of(const InputRange &range, UnaryPredicate p) +{ + return std::all_of(std::begin(range), std::end(range), p); +} + /** Convenience helper for checking if a @p range contains at least one element * for which predicate @p p returns @c true. Returns @c false if @p range is * empty. * Use ranges::any_of() instead if you can use C++20. */ template bool any_of(const InputRange &range, UnaryPredicate p) { return std::any_of(std::begin(range), std::end(range), p); } /** Convenience helper for checking if a @p container contains an element * with key equivalent to @p key. This is mainly meant to be used for the * associative standard containers until we can use their corresponding * member function in C++20. */ template bool contains(const Container &container, const Key &key) { return std::find(std::begin(container), std::end(container), key) != std::end(container); } /** Convenience helper for checking if a @p container contains an element * for which predicate @p p returns @c true. */ template bool contains_if(const Container &container, UnaryPredicate p) { return std::find_if(std::begin(container), std::end(container), p) != std::end(container); } /** * Convenience helper for removing elements from a vector @p v for which * predicate @p p returns @c true. * Use std::erase_if() instead if you can use C++20. */ template void erase_if(Vector &v, UnaryPredicate p) { v.erase(std::remove_if(std::begin(v), std::end(v), p), std::end(v)); } }