1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QTCONCURRENT_MAP_H
43 #define QTCONCURRENT_MAP_H
45 #include <QtConcurrent/qtconcurrent_global.h>
47 #ifndef QT_NO_CONCURRENT
49 #include <QtConcurrent/qtconcurrentmapkernel.h>
50 #include <QtConcurrent/qtconcurrentreducekernel.h>
51 #include <QtConcurrent/qtconcurrentfunctionwrappers.h>
52 #include <QtCore/qstringlist.h>
60 namespace QtConcurrent {
62 QFuture<void> map(Sequence &sequence, MapFunction function);
63 QFuture<void> map(Iterator begin, Iterator end, MapFunction function);
66 QFuture<T> mapped(const Sequence &sequence, MapFunction function);
68 QFuture<T> mapped(ConstIterator begin, ConstIterator end, MapFunction function);
71 QFuture<T> mappedReduced(const Sequence &sequence,
73 ReduceFunction function,
74 QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
76 QFuture<T> mappedReduced(ConstIterator begin,
79 ReduceFunction function,
80 QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
82 void blockingMap(Sequence &sequence, MapFunction function);
83 void blockingMap(Iterator begin, Iterator end, MapFunction function);
86 T blockingMapped(const Sequence &sequence, MapFunction function);
88 T blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function);
91 T blockingMappedReduced(const Sequence &sequence,
93 ReduceFunction function,
94 QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
96 T blockingMappedReduced(ConstIterator begin,
99 ReduceFunction function,
100 QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
102 } // namespace QtConcurrent
106 namespace QtConcurrent {
108 // map() on sequences
109 template <typename Sequence, typename MapFunctor>
110 QFuture<void> map(Sequence &sequence, MapFunctor map)
112 return startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map));
115 // map() on iterators
116 template <typename Iterator, typename MapFunctor>
117 QFuture<void> map(Iterator begin, Iterator end, MapFunctor map)
119 return startMap(begin, end, QtPrivate::createFunctionWrapper(map));
122 // mappedReduced() for sequences.
123 template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
124 QFuture<ResultType> mappedReduced(const Sequence &sequence,
126 ReduceFunctor reduce,
127 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
129 return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
131 QtPrivate::createFunctionWrapper(map),
132 QtPrivate::createFunctionWrapper(reduce),
136 template <typename Sequence, typename MapFunctor, typename ReduceFunctor>
137 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(const Sequence &sequence,
139 ReduceFunctor reduce,
140 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
142 return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
144 QtPrivate::createFunctionWrapper(map),
145 QtPrivate::createFunctionWrapper(reduce),
149 // mappedReduced() for iterators
150 template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
151 QFuture<ResultType> mappedReduced(Iterator begin,
154 ReduceFunctor reduce,
155 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
157 return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
159 QtPrivate::createFunctionWrapper(map),
160 QtPrivate::createFunctionWrapper(reduce),
164 template <typename Iterator, typename MapFunctor, typename ReduceFunctor>
165 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(Iterator begin,
168 ReduceFunctor reduce,
169 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
171 return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
173 QtPrivate::createFunctionWrapper(map),
174 QtPrivate::createFunctionWrapper(reduce),
178 // mapped() for sequences
179 template <typename Sequence, typename MapFunctor>
180 QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(const Sequence &sequence, MapFunctor map)
182 return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(sequence, QtPrivate::createFunctionWrapper(map));
185 // mapped() for iterator ranges.
186 template <typename Iterator, typename MapFunctor>
187 QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(Iterator begin, Iterator end, MapFunctor map)
189 return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(begin, end, QtPrivate::createFunctionWrapper(map));
192 // blockingMap() for sequences
193 template <typename Sequence, typename MapFunctor>
194 void blockingMap(Sequence &sequence, MapFunctor map)
196 startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map)).startBlocking();
199 // blockingMap() for iterator ranges
200 template <typename Iterator, typename MapFunctor>
201 void blockingMap(Iterator begin, Iterator end, MapFunctor map)
203 startMap(begin, end, QtPrivate::createFunctionWrapper(map)).startBlocking();
206 // blockingMappedReduced() for sequences
207 template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
208 ResultType blockingMappedReduced(const Sequence &sequence,
210 ReduceFunctor reduce,
211 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
213 return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
215 QtPrivate::createFunctionWrapper(map),
216 QtPrivate::createFunctionWrapper(reduce),
221 template <typename MapFunctor, typename ReduceFunctor, typename Sequence>
222 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(const Sequence &sequence,
224 ReduceFunctor reduce,
225 ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
227 return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
229 QtPrivate::createFunctionWrapper(map),
230 QtPrivate::createFunctionWrapper(reduce),
235 // blockingMappedReduced() for iterator ranges
236 template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
237 ResultType blockingMappedReduced(Iterator begin,
240 ReduceFunctor reduce,
241 QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce))
243 return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
245 QtPrivate::createFunctionWrapper(map),
246 QtPrivate::createFunctionWrapper(reduce),
251 template <typename Iterator, typename MapFunctor, typename ReduceFunctor>
252 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(Iterator begin,
255 ReduceFunctor reduce,
256 QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce))
258 return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
260 QtPrivate::createFunctionWrapper(map),
261 QtPrivate::createFunctionWrapper(reduce),
266 // mapped() for sequences with a different putput sequence type.
267 template <typename OutputSequence, typename InputSequence, typename MapFunctor>
268 OutputSequence blockingMapped(const InputSequence &sequence, MapFunctor map)
270 return blockingMappedReduced<OutputSequence>
272 QtPrivate::createFunctionWrapper(map),
273 QtPrivate::PushBackWrapper(),
274 QtConcurrent::OrderedReduce);
277 template <typename MapFunctor, typename InputSequence>
278 typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType blockingMapped(const InputSequence &sequence, MapFunctor map)
280 typedef typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType OutputSequence;
281 return blockingMappedReduced<OutputSequence>
283 QtPrivate::createFunctionWrapper(map),
284 QtPrivate::PushBackWrapper(),
285 QtConcurrent::OrderedReduce);
288 // mapped() for iterator ranges
289 template <typename Sequence, typename Iterator, typename MapFunctor>
290 Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor map)
292 return blockingMappedReduced<Sequence>
294 QtPrivate::createFunctionWrapper(map),
295 QtPrivate::PushBackWrapper(),
296 QtConcurrent::OrderedReduce);
299 template <typename Iterator, typename MapFunctor>
300 typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType blockingMapped(Iterator begin, Iterator end, MapFunctor map)
302 typedef typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType OutputSequence;
303 return blockingMappedReduced<OutputSequence>
305 QtPrivate::createFunctionWrapper(map),
306 QtPrivate::PushBackWrapper(),
307 QtConcurrent::OrderedReduce);
310 } // namespace QtConcurrent
317 #endif // QT_NO_CONCURRENT