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 QtConcurrent module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTCONCURRENT_FILTER_H
41#define QTCONCURRENT_FILTER_H
42
43#include <QtConcurrent/qtconcurrent_global.h>
44
45#if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC)
46
47#include <QtConcurrent/qtconcurrentfilterkernel.h>
48#include <QtConcurrent/qtconcurrentfunctionwrappers.h>
49
50QT_BEGIN_NAMESPACE
51
52namespace QtConcurrent {
53
54//! [QtConcurrent-1]
55template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
56ThreadEngineStarter<void> filterInternal(Sequence &sequence, KeepFunctor keep, ReduceFunctor reduce)
57{
58 typedef FilterKernel<Sequence, KeepFunctor, ReduceFunctor> KernelType;
59 return startThreadEngine(new KernelType(sequence, keep, reduce));
60}
61
62// filter() on sequences
63template <typename Sequence, typename KeepFunctor>
64QFuture<void> filter(Sequence &sequence, KeepFunctor keep)
65{
66 return filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper());
67}
68
69// filteredReduced() on sequences
70template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
71QFuture<ResultType> filteredReduced(const Sequence &sequence,
72 KeepFunctor keep,
73 ReduceFunctor reduce,
74 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
75{
76 return startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
77}
78
79#ifndef Q_CLANG_QDOC
80template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
81QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> filteredReduced(const Sequence &sequence,
82 KeepFunctor keep,
83 ReduceFunctor reduce,
84 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
85{
86 return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
87 (sequence,
88 QtPrivate::createFunctionWrapper(keep),
89 QtPrivate::createFunctionWrapper(reduce),
90 options);
91}
92#endif
93
94// filteredReduced() on iterators
95template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
96QFuture<ResultType> filteredReduced(Iterator begin,
97 Iterator end,
98 KeepFunctor keep,
99 ReduceFunctor reduce,
100 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
101{
102 return startFilteredReduced<ResultType>(begin, end, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
103}
104
105#ifndef Q_CLANG_QDOC
106template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
107QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> filteredReduced(Iterator begin,
108 Iterator end,
109 KeepFunctor keep,
110 ReduceFunctor reduce,
111 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
112{
113 return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
114 (begin, end,
115 QtPrivate::createFunctionWrapper(keep),
116 QtPrivate::createFunctionWrapper(reduce),
117 options);
118}
119#endif
120
121// filtered() on sequences
122template <typename Sequence, typename KeepFunctor>
123QFuture<typename Sequence::value_type> filtered(const Sequence &sequence, KeepFunctor keep)
124{
125 return startFiltered(sequence, QtPrivate::createFunctionWrapper(keep));
126}
127
128// filtered() on iterators
129template <typename Iterator, typename KeepFunctor>
130QFuture<typename qValueType<Iterator>::value_type> filtered(Iterator begin, Iterator end, KeepFunctor keep)
131{
132 return startFiltered(begin, end, QtPrivate::createFunctionWrapper(keep));
133}
134
135// blocking filter() on sequences
136template <typename Sequence, typename KeepFunctor>
137void blockingFilter(Sequence &sequence, KeepFunctor keep)
138{
139 QFuture<void> future =
140 filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper());
141 future.waitForFinished();
142}
143
144// blocking filteredReduced() on sequences
145template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
146ResultType blockingFilteredReduced(const Sequence &sequence,
147 KeepFunctor keep,
148 ReduceFunctor reduce,
149 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
150{
151 QFuture<ResultType> future =
152 startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
153 return future.result();
154}
155
156#ifndef Q_CLANG_QDOC
157template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
158typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(const Sequence &sequence,
159 KeepFunctor keep,
160 ReduceFunctor reduce,
161 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
162{
163 return blockingFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
164 (sequence,
165 QtPrivate::createFunctionWrapper(keep),
166 QtPrivate::createFunctionWrapper(reduce),
167 options);
168}
169#endif
170
171// blocking filteredReduced() on iterators
172template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
173ResultType blockingFilteredReduced(Iterator begin,
174 Iterator end,
175 KeepFunctor keep,
176 ReduceFunctor reduce,
177 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
178{
179 QFuture<ResultType> future = startFilteredReduced<ResultType>(begin, end,
180 QtPrivate::createFunctionWrapper(keep),
181 QtPrivate::createFunctionWrapper(reduce),
182 options);
183 return future.result();
184}
185
186#ifndef Q_CLANG_QDOC
187template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
188typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(Iterator begin,
189 Iterator end,
190 KeepFunctor keep,
191 ReduceFunctor reduce,
192 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
193{
194 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> future =
195 startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
196 (begin, end,
197 QtPrivate::createFunctionWrapper(keep),
198 QtPrivate::createFunctionWrapper(reduce),
199 options);
200 return future.result();
201}
202#endif
203
204// blocking filtered() on sequences
205template <typename Sequence, typename KeepFunctor>
206Sequence blockingFiltered(const Sequence &sequence, KeepFunctor keep)
207{
208 QFuture<Sequence> future =
209 startFilteredReduced<Sequence>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper(), OrderedReduce);
210 return future.result();
211}
212
213// blocking filtered() on iterators
214template <typename OutputSequence, typename Iterator, typename KeepFunctor>
215OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor keep)
216{
217 QFuture<OutputSequence> future = startFilteredReduced<OutputSequence>(begin, end,
218 QtPrivate::createFunctionWrapper(keep),
219 QtPrivate::PushBackWrapper(),
220 OrderedReduce);
221 return future.result();
222}
223
224} // namespace QtConcurrent
225
226QT_END_NAMESPACE
227
228#endif // QT_NO_CONCURRENT
229
230#endif
231

source code of qtbase/src/concurrent/qtconcurrentfilter.h