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_MAP_H
41 #define QTCONCURRENT_MAP_H
42 
43 #include <QtConcurrent/qtconcurrent_global.h>
44 
45 #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC)
46 
47 #include <QtConcurrent/qtconcurrentmapkernel.h>
48 #include <QtConcurrent/qtconcurrentreducekernel.h>
49 #include <QtConcurrent/qtconcurrentfunctionwrappers.h>
50 #include <QtCore/qstringlist.h>
51 
52 QT_BEGIN_NAMESPACE
53 
54 
55 
56 namespace QtConcurrent {
57 
58 // map() on sequences
59 template <typename Sequence, typename MapFunctor>
map(Sequence & sequence,MapFunctor map)60 QFuture<void> map(Sequence &sequence, MapFunctor map)
61 {
62     return startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map));
63 }
64 
65 // map() on iterators
66 template <typename Iterator, typename MapFunctor>
map(Iterator begin,Iterator end,MapFunctor map)67 QFuture<void> map(Iterator begin, Iterator end, MapFunctor map)
68 {
69     return startMap(begin, end, QtPrivate::createFunctionWrapper(map));
70 }
71 
72 // mappedReduced() for sequences.
73 template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
74 QFuture<ResultType> mappedReduced(const Sequence &sequence,
75                                   MapFunctor map,
76                                   ReduceFunctor reduce,
77                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
78 {
79     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
80         (sequence,
81          QtPrivate::createFunctionWrapper(map),
82          QtPrivate::createFunctionWrapper(reduce),
83          options);
84 }
85 
86 template <typename Sequence, typename MapFunctor, typename ReduceFunctor>
87 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(const Sequence &sequence,
88                                   MapFunctor map,
89                                   ReduceFunctor reduce,
90                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
91 {
92     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
93         (sequence,
94          QtPrivate::createFunctionWrapper(map),
95          QtPrivate::createFunctionWrapper(reduce),
96          options);
97 }
98 
99 // mappedReduced() for iterators
100 template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
101 QFuture<ResultType> mappedReduced(Iterator begin,
102                                   Iterator end,
103                                   MapFunctor map,
104                                   ReduceFunctor reduce,
105                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
106 {
107     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
108         (begin, end,
109          QtPrivate::createFunctionWrapper(map),
110          QtPrivate::createFunctionWrapper(reduce),
111          options);
112 }
113 
114 template <typename Iterator, typename MapFunctor, typename ReduceFunctor>
115 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(Iterator begin,
116                                   Iterator end,
117                                   MapFunctor map,
118                                   ReduceFunctor reduce,
119                                   ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
120 {
121     return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
122         (begin, end,
123          QtPrivate::createFunctionWrapper(map),
124          QtPrivate::createFunctionWrapper(reduce),
125          options);
126 }
127 
128 // mapped() for sequences
129 template <typename Sequence, typename MapFunctor>
mapped(const Sequence & sequence,MapFunctor map)130 QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(const Sequence &sequence, MapFunctor map)
131 {
132     return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(sequence, QtPrivate::createFunctionWrapper(map));
133 }
134 
135 // mapped() for iterator ranges.
136 template <typename Iterator, typename MapFunctor>
mapped(Iterator begin,Iterator end,MapFunctor map)137 QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(Iterator begin, Iterator end, MapFunctor map)
138 {
139     return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(begin, end, QtPrivate::createFunctionWrapper(map));
140 }
141 
142 // blockingMap() for sequences
143 template <typename Sequence, typename MapFunctor>
blockingMap(Sequence & sequence,MapFunctor map)144 void blockingMap(Sequence &sequence, MapFunctor map)
145 {
146     startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map)).startBlocking();
147 }
148 
149 // blockingMap() for iterator ranges
150 template <typename Iterator, typename MapFunctor>
blockingMap(Iterator begin,Iterator end,MapFunctor map)151 void blockingMap(Iterator begin, Iterator end, MapFunctor map)
152 {
153     startMap(begin, end, QtPrivate::createFunctionWrapper(map)).startBlocking();
154 }
155 
156 // blockingMappedReduced() for sequences
157 template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
158 ResultType blockingMappedReduced(const Sequence &sequence,
159                                  MapFunctor map,
160                                  ReduceFunctor reduce,
161                                  ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
162 {
163     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
164         (sequence,
165          QtPrivate::createFunctionWrapper(map),
166          QtPrivate::createFunctionWrapper(reduce),
167          options)
168         .startBlocking();
169 }
170 
171 template <typename MapFunctor, typename ReduceFunctor, typename Sequence>
172 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(const Sequence &sequence,
173                                  MapFunctor map,
174                                  ReduceFunctor reduce,
175                                  ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
176 {
177     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
178         (sequence,
179          QtPrivate::createFunctionWrapper(map),
180          QtPrivate::createFunctionWrapper(reduce),
181          options)
182         .startBlocking();
183 }
184 
185 // blockingMappedReduced() for iterator ranges
186 template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
187 ResultType blockingMappedReduced(Iterator begin,
188                                  Iterator end,
189                                  MapFunctor map,
190                                  ReduceFunctor reduce,
191                                  QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce))
192 {
193     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType>
194         (begin, end,
195          QtPrivate::createFunctionWrapper(map),
196          QtPrivate::createFunctionWrapper(reduce),
197          options)
198         .startBlocking();
199 }
200 
201 template <typename Iterator, typename MapFunctor, typename ReduceFunctor>
202 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(Iterator begin,
203                                  Iterator end,
204                                  MapFunctor map,
205                                  ReduceFunctor reduce,
206                                  QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce))
207 {
208     return QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
209         (begin, end,
210          QtPrivate::createFunctionWrapper(map),
211          QtPrivate::createFunctionWrapper(reduce),
212          options)
213         .startBlocking();
214 }
215 
216 // mapped() for sequences with a different putput sequence type.
217 template <typename OutputSequence, typename InputSequence, typename MapFunctor>
blockingMapped(const InputSequence & sequence,MapFunctor map)218 OutputSequence blockingMapped(const InputSequence &sequence, MapFunctor map)
219 {
220     return blockingMappedReduced<OutputSequence>
221         (sequence,
222          QtPrivate::createFunctionWrapper(map),
223          QtPrivate::PushBackWrapper(),
224          QtConcurrent::OrderedReduce);
225 }
226 
227 template <typename MapFunctor, typename InputSequence>
blockingMapped(const InputSequence & sequence,MapFunctor map)228 typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType blockingMapped(const InputSequence &sequence, MapFunctor map)
229 {
230     typedef typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType OutputSequence;
231     return blockingMappedReduced<OutputSequence>
232         (sequence,
233          QtPrivate::createFunctionWrapper(map),
234          QtPrivate::PushBackWrapper(),
235          QtConcurrent::OrderedReduce);
236 }
237 
238 // mapped()  for iterator ranges
239 template <typename Sequence, typename Iterator, typename MapFunctor>
blockingMapped(Iterator begin,Iterator end,MapFunctor map)240 Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor map)
241 {
242     return blockingMappedReduced<Sequence>
243         (begin, end,
244          QtPrivate::createFunctionWrapper(map),
245          QtPrivate::PushBackWrapper(),
246          QtConcurrent::OrderedReduce);
247 }
248 
249 template <typename Iterator, typename MapFunctor>
blockingMapped(Iterator begin,Iterator end,MapFunctor map)250 typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType blockingMapped(Iterator begin, Iterator end, MapFunctor map)
251 {
252     typedef typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType OutputSequence;
253     return blockingMappedReduced<OutputSequence>
254         (begin, end,
255          QtPrivate::createFunctionWrapper(map),
256          QtPrivate::PushBackWrapper(),
257          QtConcurrent::OrderedReduce);
258 }
259 
260 } // namespace QtConcurrent
261 
262 
263 QT_END_NAMESPACE
264 
265 #endif // QT_NO_CONCURRENT
266 
267 #endif
268