Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
q20algorithm.h
Go to the documentation of this file.
1// Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4#ifndef Q20ALGORITHM_H
5#define Q20ALGORITHM_H
6
7#include <QtCore/qglobal.h>
8
9#include <algorithm>
10#include <QtCore/q20functional.h>
11
12//
13// W A R N I N G
14// -------------
15//
16// This file is not part of the Qt API. Types and functions defined in this
17// file can reliably be replaced by their std counterparts, once available.
18// You may use these definitions in your own code, but be aware that we
19// will remove them once Qt depends on the C++ version that supports
20// them in namespace std. There will be NO deprecation warning, the
21// definitions will JUST go away.
22//
23// If you can't agree to these terms, don't use these definitions!
24//
25// We mean it.
26//
27
28QT_BEGIN_NAMESPACE
29
30namespace q20 {
31// like std::<algorithm> (ie. not ranged, but constexpr)
32#ifdef __cpp_lib_constexpr_algorithms
33using std::copy;
34using std::copy_if;
35using std::copy_n;
36using std::fill;
37using std::fill_n;
38using std::is_sorted_until;
39using std::is_sorted;
40using std::transform;
41#else
42template <typename InputIterator, typename OutputIterator>
43constexpr OutputIterator
44copy(InputIterator first, InputIterator last, OutputIterator dest)
45{
46 while (first != last) {
47 *dest = *first;
48 ++first;
49 ++dest;
50 }
51 return dest;
52}
53
54template <typename InputIterator, typename OutputIterator, typename UnaryPredicate>
55constexpr OutputIterator
56copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred)
57{
58 while (first != last) {
59 if (pred(*first)) {
60 *dest = *first;
61 ++dest;
62 }
63 ++first;
64 }
65 return dest;
66}
67
68template <typename InputIterator, typename Size, typename OutputIterator>
69constexpr OutputIterator
70copy_n(InputIterator first, Size n, OutputIterator dest)
71{
72 while (n > Size{0}) {
73 *dest = *first;
74 ++first;
75 ++dest;
76 --n;
77 }
78 return dest;
79}
80
81template <typename ForwardIterator, typename Value>
82constexpr void
83fill(ForwardIterator first, ForwardIterator last, const Value &value)
84{
85 while (first != last) {
86 *first = value;
87 ++first;
88 }
89}
90
91template <typename OutputIterator, typename Size, typename Value>
92constexpr OutputIterator
93fill_n(OutputIterator first, Size n, const Value &value)
94{
95 while (n > Size{0}) {
96 *first = value;
97 ++first;
98 --n;
99 }
100 return first;
101}
102
103template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
104constexpr ForwardIterator
105is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
106{
107 if (first == last)
108 return first;
109 auto prev = first;
110 while (++first != last) {
111 if (p(*first, *prev))
112 return first;
113 prev = first;
114 }
115 return first;
116}
117
118template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
119constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
120{
121 return q20::is_sorted_until(first, last, p) == last;
122}
123
124template <typename InputIterator, typename OutputIterator, typename UnaryFunction>
125constexpr OutputIterator
126transform(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction op)
127{
128 while (first != last) {
129 *dest = op(*first);
130 ++first;
131 ++dest;
132 }
133 return dest;
134}
135
136// binary transform missing on purpose (no users)
137
138#endif
139}
140
141namespace q20::ranges {
142// like std::ranges::{any,all,none}_of, just unconstrained, so no range-overload
143#ifdef __cpp_lib_ranges
144using std::ranges::any_of;
145using std::ranges::all_of;
146using std::ranges::none_of;
147#else
148[[maybe_unused]] inline constexpr struct { // Niebloid
149 template <typename InputIterator, typename Sentinel,
150 typename Predicate, typename Projection = q20::identity>
151 [[maybe_unused]] constexpr bool operator()(InputIterator first, Sentinel last, Predicate pred, Projection proj = {}) const
152 {
153 while (first != last) {
154 if (std::invoke(pred, std::invoke(proj, *first)))
155 return true;
156 ++first;
157 }
158 return false;
159 }
160} any_of;
161[[maybe_unused]] inline constexpr struct { // Niebloid
162 template <typename InputIterator, typename Sentinel,
163 typename Predicate, typename Projection = q20::identity>
164 [[maybe_unused]] constexpr bool operator()(InputIterator first, Sentinel last, Predicate pred, Projection proj = {}) const
165 {
166 while (first != last) {
167 if (!std::invoke(pred, std::invoke(proj, *first)))
168 return false;
169 ++first;
170 }
171 return true;
172 }
173} all_of;
174[[maybe_unused]] inline constexpr struct { // Niebloid
175 template <typename InputIterator, typename Sentinel,
176 typename Predicate, typename Projection = q20::identity>
177 [[maybe_unused]] constexpr bool operator()(InputIterator first, Sentinel last, Predicate pred, Projection proj = {}) const
178 {
179 while (first != last) {
180 if (std::invoke(pred, std::invoke(proj, *first)))
181 return false;
182 ++first;
183 }
184 return true;
185 }
186} none_of;
187#endif // __cpp_lib_ranges
188} // namespace q20::ranges
189
190QT_END_NAMESPACE
191
192#endif /* Q20ALGORITHM_H */
\inmodule QtCore
Definition qfile.h:71
Q_AUTOTEST_EXPORT bool unload(UnloadFlag flag=UnloadSys)
Definition qlibrary.cpp:556
QtPluginInstanceFunction loadPlugin()
Definition qlibrary.cpp:583
void setLoadHints(QLibrary::LoadHints lh)
Definition qlibrary.cpp:487
void updatePluginState()
Definition qlibrary.cpp:724
QFunctionPointer resolve(const char *)
Definition qlibrary.cpp:480
QObject * pluginInstance()
Definition qlibrary.cpp:501
static void cleanup()
Definition qlibrary.cpp:340
static void releaseLibrary(QLibraryPrivate *lib)
Definition qlibrary.cpp:429
static QLibraryPrivate * findOrCreate(const QString &fileName, const QString &version, QLibrary::LoadHints loadHints)
Definition qlibrary.cpp:395
\inmodule QtCore \reentrant
Definition qlibrary.h:18
\inmodule QtCore
Definition qmutex.h:346
constexpr void fill(ForwardIterator first, ForwardIterator last, const Value &value)
constexpr OutputIterator transform(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction op)
constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p={})
constexpr OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred)
constexpr OutputIterator copy(InputIterator first, InputIterator last, OutputIterator dest)
constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p={})
constexpr OutputIterator fill_n(OutputIterator first, Size n, const Value &value)
constexpr OutputIterator copy_n(InputIterator first, Size n, OutputIterator dest)
Q_TRACE_POINT(qtcore, QLibraryPrivate_load_exit, bool success)
static void installCoverageTool(QLibraryPrivate *libPrivate)
Definition qlibrary.cpp:282
static constexpr bool PluginMustMatchQtDebug
Definition qlibrary.cpp:46
bool qt_debug_component()
Q_TRACE_POINT(qtcore, QLibraryPrivate_load_entry, const QString &fileName)
static bool qt_get_metadata(QLibraryPrivate *priv, QString *errMsg)
Definition qlibrary.cpp:680
static Q_CONSTINIT bool qt_library_data_once
Definition qlibrary.cpp:333
static QLibraryScanResult findPatternUnloaded(const QString &library, QLibraryPrivate *lib)
Definition qlibrary.cpp:216
static constexpr bool QtBuildIsDebug
Definition qlibrary.cpp:56
static void qlibraryCleanup()
Definition qlibrary.cpp:378
QT_REQUIRE_CONFIG(library)