1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtCore module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QTCONCURRENT_FILTER_H
43 #define QTCONCURRENT_FILTER_H
45 #include <QtConcurrent/qtconcurrent_global.h>
47 #ifndef QT_NO_CONCURRENT
49 #include <QtConcurrent/qtconcurrentfilterkernel.h>
50 #include <QtConcurrent/qtconcurrentfunctionwrappers.h>
58 namespace QtConcurrent {
60 QFuture<void> filter(Sequence &sequence, FilterFunction filterFunction);
63 QFuture<T> filtered(const Sequence &sequence, FilterFunction filterFunction);
65 QFuture<T> filtered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction);
68 QFuture<T> filteredReduced(const Sequence &sequence,
69 FilterFunction filterFunction,
70 ReduceFunction reduceFunction,
71 QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
73 QFuture<T> filteredReduced(ConstIterator begin,
75 FilterFunction filterFunction,
76 ReduceFunction reduceFunction,
77 QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
79 void blockingFilter(Sequence &sequence, FilterFunction filterFunction);
81 template <typename Sequence>
82 Sequence blockingFiltered(const Sequence &sequence, FilterFunction filterFunction);
83 template <typename Sequence>
84 Sequence blockingFiltered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction);
87 T blockingFilteredReduced(const Sequence &sequence,
88 FilterFunction filterFunction,
89 ReduceFunction reduceFunction,
90 QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
92 T blockingFilteredReduced(ConstIterator begin,
94 FilterFunction filterFunction,
95 ReduceFunction reduceFunction,
96 QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
98 } // namespace QtConcurrent
102 namespace QtConcurrent {
104 template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
105 ThreadEngineStarter<void> filterInternal(Sequence &sequence, KeepFunctor keep, ReduceFunctor reduce)
107 typedef FilterKernel<Sequence, KeepFunctor, ReduceFunctor> KernelType;
108 return startThreadEngine(new KernelType(sequence, keep, reduce));
111 // filter() on sequences
112 template <typename Sequence, typename KeepFunctor>
113 QFuture<void> filter(Sequence &sequence, KeepFunctor keep)
115 return filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper());
118 // filteredReduced() on sequences
119 template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
120 QFuture<ResultType> filteredReduced(const Sequence &sequence,
122 ReduceFunctor reduce,
123 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
125 return startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
128 template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
129 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> filteredReduced(const Sequence &sequence,
131 ReduceFunctor reduce,
132 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
134 return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
136 QtPrivate::createFunctionWrapper(keep),
137 QtPrivate::createFunctionWrapper(reduce),
141 // filteredReduced() on iterators
142 template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
143 QFuture<ResultType> filteredReduced(Iterator begin,
146 ReduceFunctor reduce,
147 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
149 return startFilteredReduced<ResultType>(begin, end, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
152 template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
153 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> filteredReduced(Iterator begin,
156 ReduceFunctor reduce,
157 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
159 return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
161 QtPrivate::createFunctionWrapper(keep),
162 QtPrivate::createFunctionWrapper(reduce),
166 // filtered() on sequences
167 template <typename Sequence, typename KeepFunctor>
168 QFuture<typename Sequence::value_type> filtered(const Sequence &sequence, KeepFunctor keep)
170 return startFiltered(sequence, QtPrivate::createFunctionWrapper(keep));
173 // filtered() on iterators
174 template <typename Iterator, typename KeepFunctor>
175 QFuture<typename qValueType<Iterator>::value_type> filtered(Iterator begin, Iterator end, KeepFunctor keep)
177 return startFiltered(begin, end, QtPrivate::createFunctionWrapper(keep));
180 // blocking filter() on sequences
181 template <typename Sequence, typename KeepFunctor>
182 void blockingFilter(Sequence &sequence, KeepFunctor keep)
184 filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper()).startBlocking();
187 // blocking filteredReduced() on sequences
188 template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
189 ResultType blockingFilteredReduced(const Sequence &sequence,
191 ReduceFunctor reduce,
192 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
194 return startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options)
198 template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
199 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(const Sequence &sequence,
201 ReduceFunctor reduce,
202 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
204 return blockingFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
206 QtPrivate::createFunctionWrapper(keep),
207 QtPrivate::createFunctionWrapper(reduce),
211 // blocking filteredReduced() on iterators
212 template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
213 ResultType blockingFilteredReduced(Iterator begin,
216 ReduceFunctor reduce,
217 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
219 return startFilteredReduced<ResultType>
221 QtPrivate::createFunctionWrapper(keep),
222 QtPrivate::createFunctionWrapper(reduce),
227 template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
228 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(Iterator begin,
231 ReduceFunctor reduce,
232 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
234 return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
236 QtPrivate::createFunctionWrapper(keep),
237 QtPrivate::createFunctionWrapper(reduce),
242 // blocking filtered() on sequences
243 template <typename Sequence, typename KeepFunctor>
244 Sequence blockingFiltered(const Sequence &sequence, KeepFunctor keep)
246 return startFilteredReduced<Sequence>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper(), OrderedReduce).startBlocking();
249 // blocking filtered() on iterators
250 template <typename OutputSequence, typename Iterator, typename KeepFunctor>
251 OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor keep)
253 return startFilteredReduced<OutputSequence>(begin, end,
254 QtPrivate::createFunctionWrapper(keep),
255 QtPrivate::PushBackWrapper(),
256 OrderedReduce).startBlocking();
259 } // namespace QtConcurrent
266 #endif // QT_NO_CONCURRENT