1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore 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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTCONCURRENT_MAP_H
43 #define QTCONCURRENT_MAP_H
44 
45 #include <QtCore/qglobal.h>
46 
47 #ifndef QT_NO_CONCURRENT
48 
49 #include <QtCore/qtconcurrentmapkernel.h>
50 #include <QtCore/qtconcurrentreducekernel.h>
51 #include <QtCore/qtconcurrentfunctionwrappers.h>
52 #include <QtCore/qstringlist.h>
53 
54 QT_BEGIN_HEADER
55 QT_BEGIN_NAMESPACE
56 
QT_MODULE(Core)57 QT_MODULE(Core)
58 
59 #ifdef qdoc
60 
61 namespace QtConcurrent {
62 
63     QFuture<void> map(Sequence &sequence, MapFunction function);
64     QFuture<void> map(Iterator begin, Iterator end, MapFunction function);
65 
66     template <typename T>
67     QFuture<T> mapped(const Sequence &sequence, MapFunction function);
68     template <typename T>
69     QFuture<T> mapped(ConstIterator begin, ConstIterator end, MapFunction function);
70 
71     template <typename T>
72     QFuture<T> mappedReduced(const Sequence &sequence,
73                              MapFunction function,
74                              ReduceFunction function,
75                              QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
76     template <typename T>
77     QFuture<T> mappedReduced(ConstIterator begin,
78                              ConstIterator end,
79                              MapFunction function,
80                              ReduceFunction function,
81                              QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
82 
83     void blockingMap(Sequence &sequence, MapFunction function);
84     void blockingMap(Iterator begin, Iterator end, MapFunction function);
85 
86     template <typename T>
87     T blockingMapped(const Sequence &sequence, MapFunction function);
88     template <typename T>
89     T blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function);
90 
91     template <typename T>
92     T blockingMappedReduced(const Sequence &sequence,
93                             MapFunction function,
94                             ReduceFunction function,
95                             QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
96     template <typename T>
97     T blockingMappedReduced(ConstIterator begin,
98                             ConstIterator end,
99                             MapFunction function,
100                             ReduceFunction function,
101                             QtConcurrent::ReduceOptions options = UnorderedReduce | SequentialReduce);
102 
103 } // namespace QtConcurrent
104 
105 #else
106 
107 namespace QtConcurrent {
108 
109 // map() on sequences
110 template <typename Sequence, typename MapFunctor>
111 QFuture<void> map(Sequence &sequence, MapFunctor map)
112 {
113     return startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map));
114 }
115 
116 // map() on iterators
117 template <typename Iterator, typename MapFunctor>
118 QFuture<void> map(Iterator begin, Iterator end, MapFunctor map)
119 {
120     return startMap(begin, end, QtPrivate::createFunctionWrapper(map));
121 }
122 
123 // mappedReduced() for sequences.
124 template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
125 QFuture<ResultType> mappedReduced(const Sequence &sequence,
126                                   MapFunctor map,
127                                   ReduceFunctor reduce,
128                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
129 {
130     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
131         (sequence,
132          QtPrivate::createFunctionWrapper(map),
133          QtPrivate::createFunctionWrapper(reduce),
134          options);
135 }
136 
137 template <typename Sequence, typename MapFunctor, typename ReduceFunctor>
138 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(const Sequence &sequence,
139                                   MapFunctor map,
140                                   ReduceFunctor reduce,
141                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
142 {
143     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
144         (sequence,
145          QtPrivate::createFunctionWrapper(map),
146          QtPrivate::createFunctionWrapper(reduce),
147          options);
148 }
149 
150 // mappedReduced() for iterators
151 template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
152 QFuture<ResultType> mappedReduced(Iterator begin,
153                                   Iterator end,
154                                   MapFunctor map,
155                                   ReduceFunctor reduce,
156                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
157 {
158     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
159         (begin, end,
160          QtPrivate::createFunctionWrapper(map),
161          QtPrivate::createFunctionWrapper(reduce),
162          options);
163 }
164 
165 template <typename Iterator, typename MapFunctor, typename ReduceFunctor>
166 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(Iterator begin,
167                                   Iterator end,
168                                   MapFunctor map,
169                                   ReduceFunctor reduce,
170                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
171 {
172     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
173         (begin, end,
174          QtPrivate::createFunctionWrapper(map),
175          QtPrivate::createFunctionWrapper(reduce),
176          options);
177 }
178 
179 // mapped() for sequences
180 template <typename Sequence, typename MapFunctor>
181 QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(const Sequence &sequence, MapFunctor map)
182 {
183     return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(sequence, QtPrivate::createFunctionWrapper(map));
184 }
185 
186 // mapped() for iterator ranges.
187 template <typename Iterator, typename MapFunctor>
188 QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(Iterator begin, Iterator end, MapFunctor map)
189 {
190     return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(begin, end, QtPrivate::createFunctionWrapper(map));
191 }
192 
193 // blockingMap() for sequences
194 template <typename Sequence, typename MapFunctor>
195 void blockingMap(Sequence &sequence, MapFunctor map)
196 {
197     startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map)).startBlocking();
198 }
199 
200 // blockingMap() for iterator ranges
201 template <typename Iterator, typename MapFunctor>
202 void blockingMap(Iterator begin, Iterator end, MapFunctor map)
203 {
204     startMap(begin, end, QtPrivate::createFunctionWrapper(map)).startBlocking();
205 }
206 
207 // blockingMappedReduced() for sequences
208 template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
209 ResultType blockingMappedReduced(const Sequence &sequence,
210                                  MapFunctor map,
211                                  ReduceFunctor reduce,
212                                  ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
213 {
214     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
215         (sequence,
216          QtPrivate::createFunctionWrapper(map),
217          QtPrivate::createFunctionWrapper(reduce),
218          options)
219         .startBlocking();
220 }
221 
222 template <typename MapFunctor, typename ReduceFunctor, typename Sequence>
223 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(const Sequence &sequence,
224                                  MapFunctor map,
225                                  ReduceFunctor reduce,
226                                  ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
227 {
228     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
229         (sequence,
230          QtPrivate::createFunctionWrapper(map),
231          QtPrivate::createFunctionWrapper(reduce),
232          options)
233         .startBlocking();
234 }
235 
236 // blockingMappedReduced() for iterator ranges
237 template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
238 ResultType blockingMappedReduced(Iterator begin,
239                                  Iterator end,
240                                  MapFunctor map,
241                                  ReduceFunctor reduce,
242                                  QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce))
243 {
244     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
245         (begin, end,
246          QtPrivate::createFunctionWrapper(map),
247          QtPrivate::createFunctionWrapper(reduce),
248          options)
249         .startBlocking();
250 }
251 
252 template <typename Iterator, typename MapFunctor, typename ReduceFunctor>
253 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(Iterator begin,
254                                  Iterator end,
255                                  MapFunctor map,
256                                  ReduceFunctor reduce,
257                                  QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce))
258 {
259     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
260         (begin, end,
261          QtPrivate::createFunctionWrapper(map),
262          QtPrivate::createFunctionWrapper(reduce),
263          options)
264         .startBlocking();
265 }
266 
267 // mapped() for sequences with a different putput sequence type.
268 template <typename OutputSequence, typename InputSequence, typename MapFunctor>
269 OutputSequence blockingMapped(const InputSequence &sequence, MapFunctor map)
270 {
271     return blockingMappedReduced<OutputSequence>
272         (sequence,
273          QtPrivate::createFunctionWrapper(map),
274          QtPrivate::PushBackWrapper(),
275          QtConcurrent::OrderedReduce);
276 }
277 
278 template <typename MapFunctor, typename InputSequence>
279 typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType blockingMapped(const InputSequence &sequence, MapFunctor map)
280 {
281     typedef typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType OutputSequence;
282     return blockingMappedReduced<OutputSequence>
283         (sequence,
284          QtPrivate::createFunctionWrapper(map),
285          QtPrivate::PushBackWrapper(),
286          QtConcurrent::OrderedReduce);
287 }
288 
289 // mapped()  for iterator ranges
290 template <typename Sequence, typename Iterator, typename MapFunctor>
291 Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor map)
292 {
293     return blockingMappedReduced<Sequence>
294         (begin, end,
295          QtPrivate::createFunctionWrapper(map),
296          QtPrivate::PushBackWrapper(),
297          QtConcurrent::OrderedReduce);
298 }
299 
300 template <typename Iterator, typename MapFunctor>
301 typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType blockingMapped(Iterator begin, Iterator end, MapFunctor map)
302 {
303     typedef typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType OutputSequence;
304     return blockingMappedReduced<OutputSequence>
305         (begin, end,
306          QtPrivate::createFunctionWrapper(map),
307          QtPrivate::PushBackWrapper(),
308          QtConcurrent::OrderedReduce);
309 }
310 
311 } // namespace QtConcurrent
312 
313 #endif // qdoc
314 
315 QT_END_NAMESPACE
316 QT_END_HEADER
317 
318 #endif // QT_NO_CONCURRENT
319 
320 #endif
321