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 filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper()).startBlocking();
140}
141
142// blocking filteredReduced() on sequences
143template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
144ResultType blockingFilteredReduced(const Sequence &sequence,
145 KeepFunctor keep,
146 ReduceFunctor reduce,
147 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
148{
149 return startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options)
150 .startBlocking();
151}
152
153#ifndef Q_CLANG_QDOC
154template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
155typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(const Sequence &sequence,
156 KeepFunctor keep,
157 ReduceFunctor reduce,
158 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
159{
160 return blockingFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
161 (sequence,
162 QtPrivate::createFunctionWrapper(keep),
163 QtPrivate::createFunctionWrapper(reduce),
164 options);
165}
166#endif
167
168// blocking filteredReduced() on iterators
169template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
170ResultType blockingFilteredReduced(Iterator begin,
171 Iterator end,
172 KeepFunctor keep,
173 ReduceFunctor reduce,
174 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
175{
176 return startFilteredReduced<ResultType>
177 (begin, end,
178 QtPrivate::createFunctionWrapper(keep),
179 QtPrivate::createFunctionWrapper(reduce),
180 options)
181 .startBlocking();
182}
183
184#ifndef Q_CLANG_QDOC
185template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
186typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(Iterator begin,
187 Iterator end,
188 KeepFunctor keep,
189 ReduceFunctor reduce,
190 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
191{
192 return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
193 (begin, end,
194 QtPrivate::createFunctionWrapper(keep),
195 QtPrivate::createFunctionWrapper(reduce),
196 options)
197 .startBlocking();
198}
199#endif
200
201// blocking filtered() on sequences
202template <typename Sequence, typename KeepFunctor>
203Sequence blockingFiltered(const Sequence &sequence, KeepFunctor keep)
204{
205 return startFilteredReduced<Sequence>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper(), OrderedReduce).startBlocking();
206}
207
208// blocking filtered() on iterators
209template <typename OutputSequence, typename Iterator, typename KeepFunctor>
210OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor keep)
211{
212 return startFilteredReduced<OutputSequence>(begin, end,
213 QtPrivate::createFunctionWrapper(keep),
214 QtPrivate::PushBackWrapper(),
215 OrderedReduce).startBlocking();
216}
217
218} // namespace QtConcurrent
219
220QT_END_NAMESPACE
221
222#endif // QT_NO_CONCURRENT
223
224#endif
225