QtBase  v6.3.1
src_concurrent_qtconcurrentfilter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 bool function(const T &t);
54 
55 
57 bool allLowerCase(const QString &string)
58 {
59  return string.lowered() == string;
60 }
61 
65 
66 
68 QStringList strings = ...;
71 
72 
74 V function(T &result, const U &intermediate)
76 
77 
79 void addToDictionary(QSet<QString> &dictionary, const QString &string)
80 {
81  dictionary.insert(string);
82 }
83 
84 QStringList strings = ...;
87 
88 
90 QStringList strings = ...;
92 
93 // filter in-place only works on non-const iterators
95 
96 QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);
98 
99 
101 QStringList strings = ...;
102 
103 // each call blocks until the entire operation is finished
105 
106 
108 
111 
112 
114 // keep only images with an alpha channel
117 
118 // retrieve gray scale images
119 QList<QImage> images = ...;
121 
122 // create a set of all printable characters
125  qOverload<const QChar&>(&QSet<QChar>::insert));
127 
128 
130 // can mix normal functions and member functions with QtConcurrent::filteredReduced()
131 
132 // create a dictionary of all lower cased strings
133 extern bool allLowerCase(const QString &string);
134 QStringList strings = ...;
136  qOverload<const QString&>(&QSet<QString>::insert));
137 
138 // create a collage of all gray scale images
139 extern void addToCollage(QImage &collage, const QImage &grayscaleImage);
140 QList<QImage> images = ...;
143 
144 
146 bool QString::contains(const QRegularExpression &regexp) const;
148 
149 
151 QStringList strings = ...;
153  return str.contains(QRegularExpression("^\\S+$")); // matches strings without whitespace
154 });
156 
159 {
160  StartsWith(const QString &string)
161  : m_string(string) { }
162 
163  bool operator()(const QString &testString)
164  {
165  return testString.startsWith(m_string);
166  }
167 
169 };
170 
171 QList<QString> strings = ...;
174 
177 {
179 };
180 
184 
186 // keep only even integers
187 QList<int> list { 1, 2, 3, 4 };
188 QtConcurrent::blockingFilter(list, [](int n) { return (n & 1) == 0; });
189 
190 // retrieve only even integers
191 QList<int> list2 { 1, 2, 3, 4 };
193  return (x & 1) == 0;
194 });
196 
197 // add up all even integers
198 QList<int> list3 { 1, 2, 3, 4 };
200  [](int x) {
201  return (x & 1) == 0;
202  },
203  [](int &sum, int x) {
204  sum += x;
205  }
206 );
208 
210 void intSumReduce(int &sum, int x)
211 {
212  sum += x;
213 }
214 
215 QList<int> list { 1, 2, 3, 4 };
217  [] (int x) {
218  return (x & 1) == 0;
219  },
221 );
223 
226 {
227  return (x & 1) == 0;
228 }
229 
230 QList<int> list { 1, 2, 3, 4 };
233  [](int &sum, int x) {
234  sum += x;
235  }
236 );
bool isPrint() const noexcept
Definition: qchar.h:496
QList< T > results() const
Definition: qfuture.h:150
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
bool hasAlphaChannel() const
Definition: qimage.cpp:4539
bool isGrayscale() const
Definition: qimage.cpp:2903
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QRegularExpression class provides pattern matching using regular expressions.
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:5092
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:1353
The QStringList class provides a list of strings.
QString str
[2]
auto V(a, v))) struct
Definition: hb-algs.hh:317
StdString::StartsWithMatcher StartsWith(std::string const &str, CaseSensitive::Choice caseSensitivity=CaseSensitive::Yes)
QFuture< typename std::decay_t< Sequence >::value_type > filtered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&keep)
void blockingFilter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&keep)
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&keep)
QFuture< ResultType > filteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&keep, ReduceFunctor &&reduce, ReduceOptions options=ReduceOptions(UnorderedReduce|SequentialReduce))
ResultType blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&keep, ReduceFunctor &&reduce, ReduceOptions options=ReduceOptions(UnorderedReduce|SequentialReduce))
std::decay_t< Sequence > blockingFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&keep)
EGLOutputLayerEXT EGLint EGLAttrib value
GLint GLint GLint GLint GLint x
[0]
GLsizei const GLchar ** strings
[1]
GLfloat n
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
GLsizei const GLchar *const * string
[0]
Definition: qopenglext.h:694
bool keepEvenIntegers(int x)
[16]
QList< QImage > images
[6]
QFuture< QString > fooString
QFuture< QImage > collage
[15]
QFuture< QImage > grayscaleImages
QList< int > results
void addToCollage(QImage &collage, const QImage &grayscaleImage)
QFuture< QString > lowerCaseStrings
QFuture< int > sum
QFuture< QSet< QString > > lowerCase
bool allLowerCase(const QString &string)
[0]
void intSumReduce(int &sum, int x)
[15]
QFuture< QSet< QString > > dictionary
QFuture< void > alphaImages
QFuture< void > future
[5]
QList< int > list
[14]
QFuture< QSet< QChar > > set
[10]
QList< QChar > characters
V const QString & string
bool operator()(const QString &testString)
StartsWith(const QString &string)
void operator()(QString &result, const QString &value)
Definition: main.cpp:38