1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTCONCURRENT_MAP_H
5#define QTCONCURRENT_MAP_H
6
7#if 0
8#pragma qt_class(QtConcurrentMap)
9#endif
10
11#include <QtConcurrent/qtconcurrent_global.h>
12
13#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
14
15#include <QtConcurrent/qtconcurrentmapkernel.h>
16#include <QtConcurrent/qtconcurrentreducekernel.h>
17#include <QtConcurrent/qtconcurrentfunctionwrappers.h>
18
19QT_BEGIN_NAMESPACE
20
21
22
23namespace QtConcurrent {
24
25// map() on sequences
26template <typename Sequence, typename MapFunctor>
27QFuture<void> map(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map)
28{
29 return startMap(pool, sequence.begin(), sequence.end(), std::forward<MapFunctor>(map));
30}
31
32template <typename Sequence, typename MapFunctor>
33QFuture<void> map(Sequence &&sequence, MapFunctor &&map)
34{
35 return startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(),
36 std::forward<MapFunctor>(map));
37}
38
39// map() on iterators
40template <typename Iterator, typename MapFunctor>
41QFuture<void> map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map)
42{
43 return startMap(pool, begin, end, std::forward<MapFunctor>(map));
44}
45
46template <typename Iterator, typename MapFunctor>
47QFuture<void> map(Iterator begin, Iterator end, MapFunctor &&map)
48{
49 return startMap(QThreadPool::globalInstance(), begin, end, std::forward<MapFunctor>(map));
50}
51
52// mappedReduced() for sequences.
53template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
54QFuture<ResultType> mappedReduced(QThreadPool *pool,
55 Sequence &&sequence,
56 MapFunctor &&map,
57 ReduceFunctor &&reduce,
58 ReduceOptions options = ReduceOptions(UnorderedReduce
59 | SequentialReduce))
60{
61 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
62 (pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
63 std::forward<ReduceFunctor>(reduce), options);
64}
65
66template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
67QFuture<ResultType> mappedReduced(Sequence &&sequence,
68 MapFunctor &&map,
69 ReduceFunctor &&reduce,
70 ReduceOptions options = ReduceOptions(UnorderedReduce
71 | SequentialReduce))
72{
73 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
74 (QThreadPool::globalInstance(), std::forward<Sequence>(sequence),
75 std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce), options);
76}
77
78#ifdef Q_QDOC
79template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
80 typename InitialValueType>
81#else
82template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
83 typename InitialValueType,
84 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
85 int> = 0>
86#endif
87QFuture<ResultType> mappedReduced(QThreadPool *pool,
88 Sequence &&sequence,
89 MapFunctor &&map,
90 ReduceFunctor &&reduce,
91 InitialValueType &&initialValue,
92 ReduceOptions options = ReduceOptions(UnorderedReduce
93 | SequentialReduce))
94{
95 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>(
96 pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
97 std::forward<ReduceFunctor>(reduce),
98 ResultType(std::forward<InitialValueType>(initialValue)), options);
99}
100#ifdef Q_QDOC
101template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
102 typename InitialValueType>
103#else
104template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
105 typename InitialValueType,
106 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
107 int> = 0>
108#endif
109QFuture<ResultType> mappedReduced(Sequence &&sequence,
110 MapFunctor &&map,
111 ReduceFunctor &&reduce,
112 InitialValueType &&initialValue,
113 ReduceOptions options = ReduceOptions(UnorderedReduce
114 | SequentialReduce))
115{
116 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
117 (QThreadPool::globalInstance(), std::forward<Sequence>(sequence),
118 std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
119 ResultType(std::forward<InitialValueType>(initialValue)), options);
120}
121
122template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
123 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
124 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
125QFuture<ResultType> mappedReduced(QThreadPool *pool,
126 Sequence &&sequence,
127 MapFunctor &&map,
128 ReduceFunctor &&reduce,
129 ReduceOptions options = ReduceOptions(UnorderedReduce
130 | SequentialReduce))
131{
132 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
133 (pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
134 std::forward<ReduceFunctor>(reduce), options);
135}
136
137template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
138 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
139 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
140QFuture<ResultType> mappedReduced(Sequence &&sequence,
141 MapFunctor &&map,
142 ReduceFunctor &&reduce,
143 ReduceOptions options = ReduceOptions(UnorderedReduce
144 | SequentialReduce))
145{
146 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
147 (QThreadPool::globalInstance(), std::forward<Sequence>(sequence),
148 std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce), options);
149}
150
151#ifdef Q_QDOC
152template <typename Sequence, typename MapFunctor, typename ReduceFunctor, typename ResultType,
153 typename InitialValueType>
154#else
155template <typename Sequence, typename MapFunctor, typename ReduceFunctor, typename InitialValueType,
156 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
157 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
158 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
159 int> = 0>
160#endif
161QFuture<ResultType> mappedReduced(QThreadPool *pool,
162 Sequence &&sequence,
163 MapFunctor &&map,
164 ReduceFunctor &&reduce,
165 InitialValueType &&initialValue,
166 ReduceOptions options = ReduceOptions(UnorderedReduce
167 | SequentialReduce))
168{
169 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>(
170 pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
171 std::forward<ReduceFunctor>(reduce),
172 ResultType(std::forward<InitialValueType>(initialValue)), options);
173}
174
175#ifdef Q_QDOC
176template <typename Sequence, typename MapFunctor, typename ReduceFunctor, typename ResultType,
177 typename InitialValueType>
178#else
179template <typename Sequence, typename MapFunctor, typename ReduceFunctor, typename InitialValueType,
180 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
181 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
182 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
183 int> = 0>
184#endif
185QFuture<ResultType> mappedReduced(Sequence &&sequence,
186 MapFunctor &&map,
187 ReduceFunctor &&reduce,
188 InitialValueType &&initialValue,
189 ReduceOptions options = ReduceOptions(UnorderedReduce
190 | SequentialReduce))
191{
192 return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
193 (QThreadPool::globalInstance(), std::forward<Sequence>(sequence),
194 std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
195 ResultType(std::forward<InitialValueType>(initialValue)), options);
196}
197
198// mappedReduced() for iterators
199template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
200QFuture<ResultType> mappedReduced(QThreadPool *pool,
201 Iterator begin,
202 Iterator end,
203 MapFunctor &&map,
204 ReduceFunctor &&reduce,
205 ReduceOptions options = ReduceOptions(UnorderedReduce
206 | SequentialReduce))
207{
208 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
209 (pool, begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
210 options);
211}
212
213template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
214QFuture<ResultType> mappedReduced(Iterator begin,
215 Iterator end,
216 MapFunctor &&map,
217 ReduceFunctor &&reduce,
218 ReduceOptions options = ReduceOptions(UnorderedReduce
219 | SequentialReduce))
220{
221 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
222 (QThreadPool::globalInstance(), begin, end, std::forward<MapFunctor>(map),
223 std::forward<ReduceFunctor>(reduce), options);
224}
225
226#ifdef Q_QDOC
227template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
228 typename InitialValueType>
229#else
230template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
231 typename InitialValueType,
232 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
233 int> = 0>
234#endif
235QFuture<ResultType> mappedReduced(QThreadPool *pool,
236 Iterator begin,
237 Iterator end,
238 MapFunctor &&map,
239 ReduceFunctor &&reduce,
240 InitialValueType &&initialValue,
241 ReduceOptions options = ReduceOptions(UnorderedReduce
242 | SequentialReduce))
243{
244 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
245 (pool, begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
246 ResultType(std::forward<InitialValueType>(initialValue)), options);
247}
248
249#ifdef Q_QDOC
250template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
251 typename InitialValueType>
252#else
253template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
254 typename InitialValueType,
255 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
256 int> = 0>
257#endif
258QFuture<ResultType> mappedReduced(Iterator begin,
259 Iterator end,
260 MapFunctor &&map,
261 ReduceFunctor &&reduce,
262 InitialValueType &&initialValue,
263 ReduceOptions options = ReduceOptions(UnorderedReduce
264 | SequentialReduce))
265{
266 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
267 (QThreadPool::globalInstance(), begin, end, std::forward<MapFunctor>(map),
268 std::forward<ReduceFunctor>(reduce),
269 ResultType(std::forward<InitialValueType>(initialValue)), options);
270}
271
272template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
273 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
274QFuture<ResultType> mappedReduced(QThreadPool *pool,
275 Iterator begin,
276 Iterator end,
277 MapFunctor &&map,
278 ReduceFunctor &&reduce,
279 ReduceOptions options = ReduceOptions(UnorderedReduce
280 | SequentialReduce))
281{
282 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>(
283 pool, begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
284 options);
285}
286
287template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
288 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
289QFuture<ResultType> mappedReduced(Iterator begin,
290 Iterator end,
291 MapFunctor &&map,
292 ReduceFunctor &&reduce,
293 ReduceOptions options = ReduceOptions(UnorderedReduce
294 | SequentialReduce))
295{
296 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
297 (QThreadPool::globalInstance(), begin, end, std::forward<MapFunctor>(map),
298 std::forward<ReduceFunctor>(reduce), options);
299}
300
301#ifdef Q_QDOC
302template <typename Iterator, typename MapFunctor, typename ReduceFunctor, typename ResultType,
303 typename InitialValueType>
304#else
305template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
306 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
307 typename InitialValueType,
308 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
309 int> = 0>
310#endif
311QFuture<ResultType> mappedReduced(QThreadPool *pool,
312 Iterator begin,
313 Iterator end,
314 MapFunctor &&map,
315 ReduceFunctor &&reduce,
316 InitialValueType &&initialValue,
317 ReduceOptions options = ReduceOptions(UnorderedReduce
318 | SequentialReduce))
319{
320 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
321 (pool, begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
322 ResultType(std::forward<InitialValueType>(initialValue)), options);
323}
324
325#ifdef Q_QDOC
326template <typename Iterator, typename MapFunctor, typename ReduceFunctor, typename ResultType,
327 typename InitialValueType>
328#else
329template<typename Iterator, typename MapFunctor, typename ReduceFunctor,
330 std::enable_if_t<QtPrivate::isIterator_v<Iterator>, int> = 0,
331 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
332 typename InitialValueType,
333 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
334 int> = 0>
335#endif
336QFuture<ResultType> mappedReduced(Iterator begin,
337 Iterator end,
338 MapFunctor &&map,
339 ReduceFunctor &&reduce,
340 InitialValueType &&initialValue,
341 ReduceOptions options = ReduceOptions(UnorderedReduce
342 | SequentialReduce))
343{
344 return startMappedReduced<QtPrivate::MapResultType<Iterator, MapFunctor>, ResultType>
345 (QThreadPool::globalInstance(), begin, end, std::forward<MapFunctor>(map),
346 std::forward<ReduceFunctor>(reduce),
347 ResultType(std::forward<InitialValueType>(initialValue)), options);
348}
349
350// mapped() for sequences
351template <typename Sequence, typename MapFunctor>
352QFuture<QtPrivate::MapResultType<Sequence, MapFunctor>> mapped(
353 QThreadPool *pool,
354 Sequence &&sequence,
355 MapFunctor &&map)
356{
357 return startMapped<QtPrivate::MapResultType<Sequence, MapFunctor>>(
358 pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map));
359}
360
361template <typename Sequence, typename MapFunctor>
362QFuture<QtPrivate::MapResultType<Sequence, MapFunctor>> mapped(
363 Sequence &&sequence,
364 MapFunctor &&map)
365{
366 return startMapped<QtPrivate::MapResultType<Sequence, MapFunctor>>
367 (QThreadPool::globalInstance(), std::forward<Sequence>(sequence),
368 std::forward<MapFunctor>(map));
369}
370
371// mapped() for iterator ranges.
372template <typename Iterator, typename MapFunctor>
373QFuture<QtPrivate::MapResultType<Iterator, MapFunctor>> mapped(
374 QThreadPool *pool,
375 Iterator begin,
376 Iterator end,
377 MapFunctor &&map)
378{
379 return startMapped<QtPrivate::MapResultType<Iterator, MapFunctor>>(
380 pool, begin, end, std::forward<MapFunctor>(map));
381}
382
383template <typename Iterator, typename MapFunctor>
384QFuture<QtPrivate::MapResultType<Iterator, MapFunctor>> mapped(
385 Iterator begin,
386 Iterator end,
387 MapFunctor &&map)
388{
389 return startMapped<QtPrivate::MapResultType<Iterator, MapFunctor>>
390 (QThreadPool::globalInstance(), begin, end, std::forward<MapFunctor>(map));
391}
392
393// blockingMap() for sequences
394template <typename Sequence, typename MapFunctor>
395void blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor map)
396{
397 QFuture<void> future =
398 startMap(pool, sequence.begin(), sequence.end(), std::forward<MapFunctor>(map));
399 future.waitForFinished();
400}
401
402template <typename Sequence, typename MapFunctor>
403void blockingMap(Sequence &&sequence, MapFunctor &&map)
404{
405 QFuture<void> future = startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(),
406 std::forward<MapFunctor>(map));
407 future.waitForFinished();
408}
409
410// blockingMap() for iterator ranges
411template <typename Iterator, typename MapFunctor>
412void blockingMap(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map)
413{
414 QFuture<void> future = startMap(pool, begin, end, map);
415 future.waitForFinished();
416}
417
418template <typename Iterator, typename MapFunctor>
419void blockingMap(Iterator begin, Iterator end, MapFunctor &&map)
420{
421 QFuture<void> future = startMap(QThreadPool::globalInstance(), begin, end,
422 std::forward<MapFunctor>(map));
423 future.waitForFinished();
424}
425
426// blockingMappedReduced() for sequences
427template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
428ResultType blockingMappedReduced(QThreadPool *pool,
429 Sequence &&sequence,
430 MapFunctor &&map,
431 ReduceFunctor &&reduce,
432 ReduceOptions options = ReduceOptions(UnorderedReduce
433 | SequentialReduce))
434{
435 QFuture<ResultType> future =
436 mappedReduced<ResultType>(pool, std::forward<Sequence>(sequence),
437 std::forward<MapFunctor>(map),
438 std::forward<ReduceFunctor>(reduce), options);
439 return future.takeResult();
440}
441
442template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
443ResultType blockingMappedReduced(Sequence &&sequence,
444 MapFunctor &&map,
445 ReduceFunctor &&reduce,
446 ReduceOptions options = ReduceOptions(UnorderedReduce
447 | SequentialReduce))
448{
449 QFuture<ResultType> future =
450 mappedReduced<ResultType>(std::forward<Sequence>(sequence),
451 std::forward<MapFunctor>(map),
452 std::forward<ReduceFunctor>(reduce), options);
453 return future.takeResult();
454}
455
456#ifdef Q_QDOC
457template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
458 typename InitialValueType>
459#else
460template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
461 typename InitialValueType,
462 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
463 int> = 0>
464#endif
465ResultType blockingMappedReduced(QThreadPool *pool,
466 Sequence &&sequence,
467 MapFunctor &&map,
468 ReduceFunctor &&reduce,
469 InitialValueType &&initialValue,
470 ReduceOptions options = ReduceOptions(UnorderedReduce
471 | SequentialReduce))
472{
473 QFuture<ResultType> future = mappedReduced<ResultType>(
474 pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
475 std::forward<ReduceFunctor>(reduce),
476 ResultType(std::forward<InitialValueType>(initialValue)), options);
477 return future.takeResult();
478}
479
480#ifdef Q_QDOC
481template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
482 typename InitialValueType>
483#else
484template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
485 typename InitialValueType,
486 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
487 int> = 0>
488#endif
489ResultType blockingMappedReduced(Sequence &&sequence,
490 MapFunctor &&map,
491 ReduceFunctor &&reduce,
492 InitialValueType &&initialValue,
493 ReduceOptions options = ReduceOptions(UnorderedReduce
494 | SequentialReduce))
495{
496 QFuture<ResultType> future = mappedReduced<ResultType>(
497 std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
498 std::forward<ReduceFunctor>(reduce),
499 ResultType(std::forward<InitialValueType>(initialValue)), options);
500 return future.takeResult();
501}
502
503template <typename MapFunctor, typename ReduceFunctor, typename Sequence,
504 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
505 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
506ResultType blockingMappedReduced(QThreadPool *pool,
507 Sequence &&sequence,
508 MapFunctor &&map,
509 ReduceFunctor &&reduce,
510 ReduceOptions options = ReduceOptions(UnorderedReduce
511 | SequentialReduce))
512{
513 QFuture<ResultType> future =
514 mappedReduced<ResultType>(pool, std::forward<Sequence>(sequence),
515 std::forward<MapFunctor>(map),
516 std::forward<ReduceFunctor>(reduce), options);
517 return future.takeResult();
518}
519
520template <typename MapFunctor, typename ReduceFunctor, typename Sequence,
521 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
522 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
523ResultType blockingMappedReduced(Sequence &&sequence,
524 MapFunctor &&map,
525 ReduceFunctor &&reduce,
526 ReduceOptions options = ReduceOptions(UnorderedReduce
527 | SequentialReduce))
528{
529 QFuture<ResultType> future =
530 mappedReduced<ResultType>(std::forward<Sequence>(sequence),
531 std::forward<MapFunctor>(map),
532 std::forward<ReduceFunctor>(reduce), options);
533 return future.takeResult();
534}
535
536#ifdef Q_QDOC
537template <typename MapFunctor, typename ReduceFunctor, typename Sequence, typename ResultType,
538 typename InitialValueType>
539#else
540template <typename MapFunctor, typename ReduceFunctor, typename Sequence, typename InitialValueType,
541 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
542 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
543 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
544 int> = 0>
545#endif
546ResultType blockingMappedReduced(QThreadPool *pool,
547 Sequence &&sequence,
548 MapFunctor &&map,
549 ReduceFunctor &&reduce,
550 InitialValueType &&initialValue,
551 ReduceOptions options = ReduceOptions(UnorderedReduce
552 | SequentialReduce))
553{
554 QFuture<ResultType> future = mappedReduced<ResultType>(
555 pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
556 std::forward<ReduceFunctor>(reduce),
557 ResultType(std::forward<InitialValueType>(initialValue)), options);
558 return future.takeResult();
559}
560
561#ifdef Q_QDOC
562template <typename MapFunctor, typename ReduceFunctor, typename Sequence, typename ResultType,
563 typename InitialValueType>
564#else
565template<typename MapFunctor, typename ReduceFunctor, typename Sequence, typename InitialValueType,
566 std::enable_if_t<QtPrivate::isInvocable<MapFunctor, Sequence>::value, int> = 0,
567 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
568 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
569 int> = 0>
570#endif
571ResultType blockingMappedReduced(Sequence &&sequence,
572 MapFunctor &&map,
573 ReduceFunctor &&reduce,
574 InitialValueType &&initialValue,
575 ReduceOptions options = ReduceOptions(UnorderedReduce
576 | SequentialReduce))
577{
578 QFuture<ResultType> future = mappedReduced<ResultType>(
579 std::forward<Sequence>(sequence), std::forward<MapFunctor>(map),
580 std::forward<ReduceFunctor>(reduce),
581 ResultType(std::forward<InitialValueType>(initialValue)), options);
582 return future.takeResult();
583}
584
585// blockingMappedReduced() for iterator ranges
586template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
587ResultType blockingMappedReduced(QThreadPool *pool,
588 Iterator begin,
589 Iterator end,
590 MapFunctor &&map,
591 ReduceFunctor &&reduce,
592 ReduceOptions options = ReduceOptions(UnorderedReduce
593 | SequentialReduce))
594{
595 QFuture<ResultType> future =
596 mappedReduced<ResultType>(pool, begin, end, std::forward<MapFunctor>(map),
597 std::forward<ReduceFunctor>(reduce), options);
598 return future.takeResult();
599}
600
601template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
602ResultType blockingMappedReduced(Iterator begin,
603 Iterator end,
604 MapFunctor &&map,
605 ReduceFunctor &&reduce,
606 ReduceOptions options = ReduceOptions(UnorderedReduce
607 | SequentialReduce))
608{
609 QFuture<ResultType> future =
610 mappedReduced<ResultType>(begin, end, std::forward<MapFunctor>(map),
611 std::forward<ReduceFunctor>(reduce), options);
612 return future.takeResult();
613}
614
615#ifdef Q_QDOC
616template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
617 typename InitialValueType>
618#else
619template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
620 typename InitialValueType,
621 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
622 int> = 0>
623#endif
624ResultType blockingMappedReduced(QThreadPool *pool,
625 Iterator begin,
626 Iterator end,
627 MapFunctor &&map,
628 ReduceFunctor &&reduce,
629 InitialValueType &&initialValue,
630 ReduceOptions options = ReduceOptions(UnorderedReduce
631 | SequentialReduce))
632{
633 QFuture<ResultType> future = mappedReduced<ResultType>(
634 pool, begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
635 ResultType(std::forward<InitialValueType>(initialValue)),
636 options);
637 return future.takeResult();
638}
639
640#ifdef Q_QDOC
641template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
642 typename InitialValueType>
643#else
644template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor,
645 typename InitialValueType,
646 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
647 int> = 0>
648#endif
649ResultType blockingMappedReduced(Iterator begin,
650 Iterator end,
651 MapFunctor &&map,
652 ReduceFunctor &&reduce,
653 InitialValueType &&initialValue,
654 ReduceOptions options = ReduceOptions(UnorderedReduce
655 | SequentialReduce))
656{
657 QFuture<ResultType> future = mappedReduced<ResultType>(
658 begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
659 ResultType(std::forward<InitialValueType>(initialValue)),
660 options);
661 return future.takeResult();
662}
663
664template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
665 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
666ResultType blockingMappedReduced(QThreadPool *pool,
667 Iterator begin,
668 Iterator end,
669 MapFunctor &&map,
670 ReduceFunctor &&reduce,
671 ReduceOptions options = ReduceOptions(UnorderedReduce
672 | SequentialReduce))
673{
674 QFuture<ResultType> future =
675 mappedReduced<ResultType>(pool, begin, end, std::forward<MapFunctor>(map),
676 std::forward<ReduceFunctor>(reduce), options);
677 return future.takeResult();
678}
679
680template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
681 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type>
682ResultType blockingMappedReduced(Iterator begin,
683 Iterator end,
684 MapFunctor &&map,
685 ReduceFunctor &&reduce,
686 ReduceOptions options = ReduceOptions(UnorderedReduce
687 | SequentialReduce))
688{
689 QFuture<ResultType> future =
690 mappedReduced<ResultType>(begin, end, std::forward<MapFunctor>(map),
691 std::forward<ReduceFunctor>(reduce), options);
692 return future.takeResult();
693}
694
695#ifdef Q_QDOC
696template <typename Iterator, typename MapFunctor, typename ReduceFunctor, typename ResultType,
697 typename InitialValueType>
698#else
699template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
700 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
701 typename InitialValueType,
702 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
703 int> = 0>
704#endif
705ResultType blockingMappedReduced(QThreadPool *pool,
706 Iterator begin,
707 Iterator end,
708 MapFunctor &&map,
709 ReduceFunctor &&reduce,
710 InitialValueType &&initialValue,
711 ReduceOptions options = ReduceOptions(UnorderedReduce
712 | SequentialReduce))
713{
714 QFuture<ResultType> future = mappedReduced<ResultType>(
715 pool, begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
716 ResultType(std::forward<InitialValueType>(initialValue)), options);
717 return future.takeResult();
718}
719
720#ifdef Q_QDOC
721template <typename Iterator, typename MapFunctor, typename ReduceFunctor, typename ResultType,
722 typename InitialValueType>
723#else
724template <typename Iterator, typename MapFunctor, typename ReduceFunctor,
725 std::enable_if_t<QtPrivate::isIterator_v<Iterator>, int> = 0,
726 typename ResultType = typename QtPrivate::ReduceResultTypeHelper<ReduceFunctor>::type,
727 typename InitialValueType,
728 std::enable_if_t<QtPrivate::isInitialValueCompatible_v<InitialValueType, ResultType>,
729 int> = 0>
730#endif
731ResultType blockingMappedReduced(Iterator begin,
732 Iterator end,
733 MapFunctor &&map,
734 ReduceFunctor &&reduce,
735 InitialValueType &&initialValue,
736 ReduceOptions options = ReduceOptions(UnorderedReduce
737 | SequentialReduce))
738{
739 QFuture<ResultType> future = mappedReduced<ResultType>(
740 begin, end, std::forward<MapFunctor>(map), std::forward<ReduceFunctor>(reduce),
741 ResultType(std::forward<InitialValueType>(initialValue)), options);
742 return future.takeResult();
743}
744
745// mapped() for sequences with a different putput sequence type.
746template <typename OutputSequence, typename InputSequence, typename MapFunctor>
747OutputSequence blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map)
748{
749 return blockingMappedReduced<OutputSequence>(pool, std::forward<InputSequence>(sequence),
750 std::forward<MapFunctor>(map),
751 QtPrivate::PushBackWrapper(), OrderedReduce);
752}
753
754template <typename OutputSequence, typename InputSequence, typename MapFunctor>
755OutputSequence blockingMapped(InputSequence &&sequence, MapFunctor &&map)
756{
757 return blockingMappedReduced<OutputSequence>(
758 QThreadPool::globalInstance(), std::forward<InputSequence>(sequence),
759 std::forward<MapFunctor>(map), QtPrivate::PushBackWrapper(), OrderedReduce);
760}
761
762template <typename MapFunctor, typename InputSequence>
763auto blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map)
764{
765 using OutputSequence = typename QtPrivate::MapSequenceResultType<std::decay_t<InputSequence>,
766 MapFunctor>::ResultType;
767 return blockingMappedReduced<OutputSequence>(pool, std::forward<InputSequence>(sequence),
768 std::forward<MapFunctor>(map),
769 QtPrivate::PushBackWrapper(), OrderedReduce);
770}
771
772template <typename MapFunctor, typename InputSequence>
773auto blockingMapped(InputSequence &&sequence, MapFunctor &&map)
774{
775 using OutputSequence = typename QtPrivate::MapSequenceResultType<std::decay_t<InputSequence>,
776 MapFunctor>::ResultType;
777 return blockingMappedReduced<OutputSequence>(QThreadPool::globalInstance(),
778 std::forward<InputSequence>(sequence),
779 std::forward<MapFunctor>(map),
780 QtPrivate::PushBackWrapper(), OrderedReduce);
781}
782
783// mapped() for iterator ranges
784template <typename Sequence, typename Iterator, typename MapFunctor>
785Sequence blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map)
786{
787 return blockingMappedReduced<Sequence>(pool, begin, end, std::forward<MapFunctor>(map),
788 QtPrivate::PushBackWrapper(), OrderedReduce);
789}
790
791template <typename Sequence, typename Iterator, typename MapFunctor>
792Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor &&map)
793{
794 return blockingMappedReduced<Sequence>(QThreadPool::globalInstance(), begin, end,
795 std::forward<MapFunctor>(map),
796 QtPrivate::PushBackWrapper(), OrderedReduce);
797}
798
799template <typename Iterator, typename MapFunctor>
800auto blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map)
801{
802 using OutputSequence = QtPrivate::MapResultType<Iterator, MapFunctor>;
803 return blockingMappedReduced<OutputSequence>(pool, begin, end, std::forward<MapFunctor>(map),
804 QtPrivate::PushBackWrapper(), OrderedReduce);
805}
806
807template <typename Iterator, typename MapFunctor>
808auto blockingMapped(Iterator begin, Iterator end, MapFunctor &&map)
809{
810 using OutputSequence = QtPrivate::MapResultType<Iterator, MapFunctor>;
811 return blockingMappedReduced<OutputSequence>(QThreadPool::globalInstance(), begin, end,
812 std::forward<MapFunctor>(map),
813 QtPrivate::PushBackWrapper(), OrderedReduce);
814}
815
816} // namespace QtConcurrent
817
818
819QT_END_NAMESPACE
820
821#endif // QT_NO_CONCURRENT
822
823#endif
824

source code of qtbase/src/concurrent/qtconcurrentmap.h