Libkleo

algorithm.h
1 /*
2  utils/algorithm.h
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  SPDX-FileCopyrightText: 2021 g10 Code GmbH
6  SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
7 
8  SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 
11 #pragma once
12 
13 #include <algorithm>
14 #include <iterator>
15 
16 namespace Kleo
17 {
18 
19 template<typename ForwardIterator, typename T>
20 ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value)
21 {
22  const ForwardIterator it = std::lower_bound(first, last, value);
23  return (it == last || value < *it) ? last : it;
24 }
25 
26 template<typename ForwardIterator, typename T, typename Compare>
27 ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value, Compare comp)
28 {
29  const ForwardIterator it = std::lower_bound(first, last, value, comp);
30  return (it == last || comp(value, *it)) ? last : it;
31 }
32 
33 template<typename Container, typename UnaryOperation>
34 Container transformInPlace(Container &&c, UnaryOperation op)
35 {
36  std::transform(std::begin(c), std::end(c), std::begin(c), op);
37  return std::move(c);
38 }
39 
40 /** Convenience helper for checking if the predicate @p p returns @c true
41  * for all elements in the range @p range. Returns @c true if the range is empty.
42  * Use ranges::all_of() instead if you can use C++20.
43  */
44 template<typename InputRange, typename UnaryPredicate>
45 bool all_of(const InputRange &range, UnaryPredicate p)
46 {
47  return std::all_of(std::begin(range), std::end(range), p);
48 }
49 
50 /** Convenience helper for checking if a @p range contains at least one element
51  * for which predicate @p p returns @c true. Returns @c false if @p range is
52  * empty.
53  * Use ranges::any_of() instead if you can use C++20.
54  */
55 template<typename InputRange, typename UnaryPredicate>
56 bool any_of(const InputRange &range, UnaryPredicate p)
57 {
58  return std::any_of(std::begin(range), std::end(range), p);
59 }
60 
61 /** Convenience helper for counting the number of elements in the range @p range
62  * for which the predicate @p p returns @c true.
63  * Use ranges::count_if() instead if you can use C++20.
64  */
65 template<typename InputRange, typename UnaryPredicate>
66 auto count_if(const InputRange &range, UnaryPredicate p)
67 {
68  return std::count_if(std::begin(range), std::end(range), p);
69 }
70 
71 /** Convenience helper for finding the first element in the range @p range
72  * for which predicate @p p returns @c true.
73  * Use ranges::find_if() instead if you can use C++20.
74  */
75 template<typename InputRange, typename UnaryPredicate>
76 auto find_if(const InputRange &range, UnaryPredicate p)
77 {
78  return std::find_if(std::begin(range), std::end(range), p);
79 }
80 
81 /** Convenience helper for applying the function @p f on all elements of the
82  * range @p range.
83  * Use ranges::for_each() instead if you can use C++20.
84  */
85 template<typename InputRange, typename UnaryFunction>
86 UnaryFunction for_each(const InputRange &range, UnaryFunction f)
87 {
88  return std::for_each(std::begin(range), std::end(range), f);
89 }
90 
91 /** Convenience helper for checking if a @p container contains an element
92  * with key equivalent to @p key. This is mainly meant to be used for the
93  * associative standard containers until we can use their corresponding
94  * member function in C++20.
95  */
96 template<typename Container, typename Key>
97 bool contains(const Container &container, const Key &key)
98 {
99  return std::find(std::begin(container), std::end(container), key) != std::end(container);
100 }
101 
102 /** Convenience helper for checking if a range @p range contains an element
103  * for which predicate @p p returns @c true.
104  */
105 template<typename InputRange, typename UnaryPredicate>
106 bool contains_if(const InputRange &range, UnaryPredicate p)
107 {
108  return Kleo::find_if(range, p) != std::end(range);
109 }
110 
111 /**
112  * Convenience helper for copying elements of @p range.
113  * Use std::ranges::copy_if() instead if you can use C++20.
114  */
115 template<typename InputRange, typename OutputIterator, typename UnaryPredicate>
116 OutputIterator copy(InputRange &&range, OutputIterator result)
117 {
118  return std::copy(std::begin(range), std::end(range), result);
119 }
120 
121 /**
122  * Convenience helper for copying elements of @p range for which predicate @p p
123  * returns @c true.
124  * Use std::ranges::copy_if() instead if you can use C++20.
125  */
126 template<typename InputRange, typename OutputIterator, typename UnaryPredicate>
127 OutputIterator copy_if(InputRange &&range, OutputIterator result, UnaryPredicate p)
128 {
129  return std::copy_if(std::begin(range), std::end(range), result, p);
130 }
131 
132 /**
133  * Convenience helper for transforming the elements of @p range.
134  * Use std::ranges::transform() instead if you can use C++20.
135  */
136 template<typename InputRange, typename OutputIterator, typename UnaryOperation>
137 OutputIterator transform(InputRange &&range, OutputIterator result, UnaryOperation op)
138 {
139  return std::transform(std::begin(range), std::end(range), result, op);
140 }
141 
142 /**
143  * Convenience helper for removing elements from a vector @p v for which
144  * predicate @p p returns @c true.
145  * Use std::erase_if() instead if you can use C++20.
146  */
147 template<typename Vector, typename UnaryPredicate>
148 void erase_if(Vector &v, UnaryPredicate p)
149 {
150  v.erase(std::remove_if(std::begin(v), std::end(v), p), std::end(v));
151 }
152 
153 }
QAction * copy(const QObject *recvr, const char *slot, QObject *parent)
KDOCTOOLS_EXPORT QString transform(const QString &file, const QString &stylesheet, const QList< const char * > &params=QList< const char * >())
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:56:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.