summaryrefslogtreecommitdiffstats
path: root/src/concurrent/qtconcurrentmap.h
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2020-09-24 11:07:32 +0200
committerSona Kurazyan <sona.kurazyan@qt.io>2020-09-30 15:28:01 +0200
commit2e27b98cffcd96d4e1d8a0b5836a36dc9929f676 (patch)
tree5276d28f7a834fa59e653a9cb887f55b72891fe3 /src/concurrent/qtconcurrentmap.h
parent5c7307775d32ae0a54acad8ae7a1c9d3cff7d45e (diff)
Use universal references in QtConcurrent
Changed QtConcurrent algorithms to take the passed sequences as universal references, where it makes sense. In addition to avoiding to create extra copies when passing rvalues, this change allows passing temporary container adaptors to QtConcurrent::map (e.g. see the example in the ticket and the new test-cases). Task-number: QTBUG-83170 Change-Id: Ia7c0833f4ec1d860294fa5214cd53934b65ff084 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/concurrent/qtconcurrentmap.h')
-rw-r--r--src/concurrent/qtconcurrentmap.h127
1 files changed, 65 insertions, 62 deletions
diff --git a/src/concurrent/qtconcurrentmap.h b/src/concurrent/qtconcurrentmap.h
index e217c9af3a5..e7c188fb8c1 100644
--- a/src/concurrent/qtconcurrentmap.h
+++ b/src/concurrent/qtconcurrentmap.h
@@ -57,13 +57,13 @@ namespace QtConcurrent {
// map() on sequences
template <typename Sequence, typename MapFunctor>
-QFuture<void> map(QThreadPool *pool, Sequence &sequence, MapFunctor map)
+QFuture<void> map(QThreadPool *pool, Sequence &&sequence, MapFunctor map)
{
return startMap(pool, sequence.begin(), sequence.end(), map);
}
template <typename Sequence, typename MapFunctor>
-QFuture<void> map(Sequence &sequence, MapFunctor map)
+QFuture<void> map(Sequence &&sequence, MapFunctor map)
{
return startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), map);
}
@@ -84,47 +84,47 @@ QFuture<void> map(Iterator begin, Iterator end, MapFunctor map)
// mappedReduced() for sequences.
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
QFuture<ResultType> mappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
| SequentialReduce))
{
return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, options);
+ (pool, std::forward<Sequence>(sequence), map, reduce, options);
}
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
-QFuture<ResultType> mappedReduced(const Sequence &sequence,
+QFuture<ResultType> mappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
| SequentialReduce))
{
return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce, options);
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce, options);
}
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
QFuture<ResultType> mappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
ReduceOptions options = ReduceOptions(UnorderedReduce
| SequentialReduce))
{
- return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, ResultType(std::forward<InitialValueType>(initialValue)),
- options);
+ return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>(
+ pool, std::forward<Sequence>(sequence), map, reduce,
+ ResultType(std::forward<InitialValueType>(initialValue)), options);
}
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
-QFuture<ResultType> mappedReduced(const Sequence &sequence,
+QFuture<ResultType> mappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
@@ -132,34 +132,34 @@ QFuture<ResultType> mappedReduced(const Sequence &sequence,
| SequentialReduce))
{
return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce,
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce,
ResultType(std::forward<InitialValueType>(initialValue)), options);
}
template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename ResultType = typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
QFuture<ResultType> mappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
| SequentialReduce))
{
return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, options);
+ (pool, std::forward<Sequence>(sequence), map, reduce, options);
}
template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename ResultType = typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
QFuture<ResultType> mappedReduced(
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
| SequentialReduce))
{
return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce, options);
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce, options);
}
template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
@@ -167,23 +167,23 @@ template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
QFuture<ResultType> mappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
ReduceOptions options = ReduceOptions(UnorderedReduce
| SequentialReduce))
{
- return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, ResultType(std::forward<InitialValueType>(initialValue)),
- options);
+ return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>(
+ pool, std::forward<Sequence>(sequence), map, reduce,
+ ResultType(std::forward<InitialValueType>(initialValue)), options);
}
template <typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename ResultType = typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
-QFuture<ResultType> mappedReduced(const Sequence &sequence,
+QFuture<ResultType> mappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
@@ -191,7 +191,7 @@ QFuture<ResultType> mappedReduced(const Sequence &sequence,
| SequentialReduce))
{
return startMappedReduced<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce,
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce,
ResultType(std::forward<InitialValueType>(initialValue)), options);
}
@@ -320,19 +320,20 @@ QFuture<ResultType> mappedReduced(Iterator begin,
template <typename Sequence, typename MapFunctor>
QFuture<QtPrivate::MapResultType<Sequence, MapFunctor>> mapped(
QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map)
{
- return startMapped<QtPrivate::MapResultType<Sequence, MapFunctor>>(pool, sequence, map);
+ return startMapped<QtPrivate::MapResultType<Sequence, MapFunctor>>(
+ pool, std::forward<Sequence>(sequence), map);
}
template <typename Sequence, typename MapFunctor>
QFuture<QtPrivate::MapResultType<Sequence, MapFunctor>> mapped(
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map)
{
return startMapped<QtPrivate::MapResultType<Sequence, MapFunctor>>
- (QThreadPool::globalInstance(), sequence, map);
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map);
}
// mapped() for iterator ranges.
@@ -358,14 +359,14 @@ QFuture<QtPrivate::MapResultType<Iterator, MapFunctor>> mapped(
// blockingMap() for sequences
template <typename Sequence, typename MapFunctor>
-void blockingMap(QThreadPool *pool, Sequence &sequence, MapFunctor map)
+void blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor map)
{
QFuture<void> future = startMap(pool, sequence.begin(), sequence.end(), map);
future.waitForFinished();
}
template <typename Sequence, typename MapFunctor>
-void blockingMap(Sequence &sequence, MapFunctor map)
+void blockingMap(Sequence &&sequence, MapFunctor map)
{
QFuture<void> future = startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), map);
future.waitForFinished();
@@ -389,7 +390,7 @@ void blockingMap(Iterator begin, Iterator end, MapFunctor map)
// blockingMappedReduced() for sequences
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
ResultType blockingMappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
@@ -397,12 +398,12 @@ ResultType blockingMappedReduced(QThreadPool *pool,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, options);
+ (pool, std::forward<Sequence>(sequence), map, reduce, options);
return future.result();
}
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
-ResultType blockingMappedReduced(const Sequence &sequence,
+ResultType blockingMappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
@@ -410,7 +411,7 @@ ResultType blockingMappedReduced(const Sequence &sequence,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce, options);
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce, options);
return future.result();
}
@@ -418,7 +419,7 @@ template <typename ResultType, typename Sequence, typename MapFunctor, typename
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
ResultType blockingMappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
@@ -426,16 +427,16 @@ ResultType blockingMappedReduced(QThreadPool *pool,
| SequentialReduce))
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
- <QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, ResultType(std::forward<InitialValueType>(initialValue)),
- options);
+ <QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
+ (pool, std::forward<Sequence>(sequence), map, reduce,
+ ResultType(std::forward<InitialValueType>(initialValue)), options);
return future.result();
}
template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
-ResultType blockingMappedReduced(const Sequence &sequence,
+ResultType blockingMappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
@@ -444,7 +445,7 @@ ResultType blockingMappedReduced(const Sequence &sequence,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce,
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce,
ResultType(std::forward<InitialValueType>(initialValue)), options);
return future.result();
}
@@ -452,7 +453,7 @@ ResultType blockingMappedReduced(const Sequence &sequence,
template <typename MapFunctor, typename ReduceFunctor, typename Sequence,
typename ResultType = typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
ResultType blockingMappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
@@ -460,13 +461,13 @@ ResultType blockingMappedReduced(QThreadPool *pool,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, options);
+ (pool, std::forward<Sequence>(sequence), map, reduce, options);
return future.result();
}
template <typename MapFunctor, typename ReduceFunctor, typename Sequence,
typename ResultType = typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
-ResultType blockingMappedReduced(const Sequence &sequence,
+ResultType blockingMappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
ReduceOptions options = ReduceOptions(UnorderedReduce
@@ -474,7 +475,7 @@ ResultType blockingMappedReduced(const Sequence &sequence,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce, options);
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce, options);
return future.result();
}
@@ -483,7 +484,7 @@ template <typename MapFunctor, typename ReduceFunctor, typename Sequence,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
ResultType blockingMappedReduced(QThreadPool *pool,
- const Sequence &sequence,
+ Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
@@ -492,8 +493,8 @@ ResultType blockingMappedReduced(QThreadPool *pool,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (pool, sequence, map, reduce, ResultType(std::forward<InitialValueType>(initialValue)),
- options);
+ (pool, std::forward<Sequence>(sequence), map, reduce,
+ ResultType(std::forward<InitialValueType>(initialValue)), options);
return future.result();
}
@@ -501,7 +502,7 @@ template <typename MapFunctor, typename ReduceFunctor, typename Sequence,
typename ResultType = typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType,
typename InitialValueType,
std::enable_if_t<std::is_convertible_v<InitialValueType, ResultType>, int> = 0>
-ResultType blockingMappedReduced(const Sequence &sequence,
+ResultType blockingMappedReduced(Sequence &&sequence,
MapFunctor map,
ReduceFunctor reduce,
InitialValueType &&initialValue,
@@ -510,7 +511,7 @@ ResultType blockingMappedReduced(const Sequence &sequence,
{
QFuture<ResultType> future = QtConcurrent::startMappedReduced
<QtPrivate::MapResultType<Sequence, MapFunctor>, ResultType>
- (QThreadPool::globalInstance(), sequence, map, reduce,
+ (QThreadPool::globalInstance(), std::forward<Sequence>(sequence), map, reduce,
ResultType(std::forward<InitialValueType>(initialValue)), options);
return future.result();
}
@@ -654,35 +655,37 @@ ResultType blockingMappedReduced(Iterator begin,
// mapped() for sequences with a different putput sequence type.
template <typename OutputSequence, typename InputSequence, typename MapFunctor>
-OutputSequence blockingMapped(QThreadPool *pool, const InputSequence &sequence, MapFunctor map)
+OutputSequence blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor map)
{
- return blockingMappedReduced<OutputSequence>(pool, sequence, map,
+ return blockingMappedReduced<OutputSequence>(pool, std::forward<InputSequence>(sequence), map,
QtPrivate::PushBackWrapper(), OrderedReduce);
}
template <typename OutputSequence, typename InputSequence, typename MapFunctor>
-OutputSequence blockingMapped(const InputSequence &sequence, MapFunctor map)
+OutputSequence blockingMapped(InputSequence &&sequence, MapFunctor map)
{
- return blockingMappedReduced<OutputSequence>(QThreadPool::globalInstance(), sequence, map,
- QtPrivate::PushBackWrapper(), OrderedReduce);
+ return blockingMappedReduced<OutputSequence>(QThreadPool::globalInstance(),
+ std::forward<InputSequence>(sequence), map,
+ QtPrivate::PushBackWrapper(), OrderedReduce);
}
template <typename MapFunctor, typename InputSequence>
-auto blockingMapped(QThreadPool *pool, const InputSequence &sequence, MapFunctor map)
+auto blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor map)
{
- using OutputSequence = typename QtPrivate::MapSequenceResultType<InputSequence,
- MapFunctor>::ResultType;
- return blockingMappedReduced<OutputSequence>(pool, sequence, map, QtPrivate::PushBackWrapper(),
- OrderedReduce);
+ using OutputSequence = typename QtPrivate::MapSequenceResultType<std::decay_t<InputSequence>,
+ MapFunctor>::ResultType;
+ return blockingMappedReduced<OutputSequence>(pool, std::forward<InputSequence>(sequence), map,
+ QtPrivate::PushBackWrapper(), OrderedReduce);
}
template <typename MapFunctor, typename InputSequence>
-auto blockingMapped(const InputSequence &sequence, MapFunctor map)
+auto blockingMapped(InputSequence &&sequence, MapFunctor map)
{
- using OutputSequence = typename QtPrivate::MapSequenceResultType<InputSequence,
- MapFunctor>::ResultType;
- return blockingMappedReduced<OutputSequence>(QThreadPool::globalInstance(), sequence, map,
- QtPrivate::PushBackWrapper(), OrderedReduce);
+ using OutputSequence = typename QtPrivate::MapSequenceResultType<std::decay_t<InputSequence>,
+ MapFunctor>::ResultType;
+ return blockingMappedReduced<OutputSequence>(QThreadPool::globalInstance(),
+ std::forward<InputSequence>(sequence), map,
+ QtPrivate::PushBackWrapper(), OrderedReduce);
}
// mapped() for iterator ranges