// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QTCONCURRENT_MAP_H #define QTCONCURRENT_MAP_H #if 0 #pragma qt_class(QtConcurrentMap) #endif #include #if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC) #include #include #include QT_BEGIN_NAMESPACE namespace QtConcurrent { // map() on sequences template QFuture map(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map) { return startMap(pool, sequence.begin(), sequence.end(), std::forward(map)); } template QFuture map(Sequence &&sequence, MapFunctor &&map) { return startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), std::forward(map)); } // map() on iterators template QFuture map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { return startMap(pool, begin, end, std::forward(map)); } template QFuture map(Iterator begin, Iterator end, MapFunctor &&map) { return startMap(QThreadPool::globalInstance(), begin, end, std::forward(map)); } // mappedReduced() for sequences. template QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, std::forward(sequence), std::forward(map), std::forward(reduce), options); } template QFuture mappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), std::forward(sequence), std::forward(map), std::forward(reduce), options); } #ifdef Q_QDOC template #else template , int> = 0> #endif QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType>( pool, std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } #ifdef Q_QDOC template #else template , int> = 0> #endif QFuture mappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type> QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, std::forward(sequence), std::forward(map), std::forward(reduce), options); } template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type> QFuture mappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), std::forward(sequence), std::forward(map), std::forward(reduce), options); } #ifdef Q_QDOC template #else template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType>( pool, std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } #ifdef Q_QDOC template #else template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } // mappedReduced() for iterators template QFuture mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, begin, end, std::forward(map), std::forward(reduce), options); } template QFuture mappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), begin, end, std::forward(map), std::forward(reduce), options); } #ifdef Q_QDOC template #else template , int> = 0> #endif QFuture mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } #ifdef Q_QDOC template #else template , int> = 0> #endif QFuture mappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } template ::type> QFuture mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType>( pool, begin, end, std::forward(map), std::forward(reduce), options); } template ::type> QFuture mappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), begin, end, std::forward(map), std::forward(reduce), options); } #ifdef Q_QDOC template #else template ::type, typename InitialValueType, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } #ifdef Q_QDOC template #else template, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type, typename InitialValueType, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } // mapped() for sequences template QFuture> mapped( QThreadPool *pool, Sequence &&sequence, MapFunctor &&map) { return startMapped>( pool, std::forward(sequence), std::forward(map)); } template QFuture> mapped( Sequence &&sequence, MapFunctor &&map) { return startMapped> (QThreadPool::globalInstance(), std::forward(sequence), std::forward(map)); } // mapped() for iterator ranges. template QFuture> mapped( QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { return startMapped>( pool, begin, end, std::forward(map)); } template QFuture> mapped( Iterator begin, Iterator end, MapFunctor &&map) { return startMapped> (QThreadPool::globalInstance(), begin, end, std::forward(map)); } // blockingMap() for sequences template void blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor map) { QFuture future = startMap(pool, sequence.begin(), sequence.end(), std::forward(map)); future.waitForFinished(); } template void blockingMap(Sequence &&sequence, MapFunctor &&map) { QFuture future = startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), std::forward(map)); future.waitForFinished(); } // blockingMap() for iterator ranges template void blockingMap(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { QFuture future = startMap(pool, begin, end, map); future.waitForFinished(); } template void blockingMap(Iterator begin, Iterator end, MapFunctor &&map) { QFuture future = startMap(QThreadPool::globalInstance(), begin, end, std::forward(map)); future.waitForFinished(); } // blockingMappedReduced() for sequences template ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(pool, std::forward(sequence), std::forward(map), std::forward(reduce), options); return future.takeResult(); } template ResultType blockingMappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(std::forward(sequence), std::forward(map), std::forward(reduce), options); return future.takeResult(); } #ifdef Q_QDOC template #else template , int> = 0> #endif ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( pool, std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } #ifdef Q_QDOC template #else template , int> = 0> #endif ResultType blockingMappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type> ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(pool, std::forward(sequence), std::forward(map), std::forward(reduce), options); return future.takeResult(); } template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type> ResultType blockingMappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(std::forward(sequence), std::forward(map), std::forward(reduce), options); return future.takeResult(); } #ifdef Q_QDOC template #else template ::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( pool, std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } #ifdef Q_QDOC template #else template::value, int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( std::forward(sequence), std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } // blockingMappedReduced() for iterator ranges template ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(pool, begin, end, std::forward(map), std::forward(reduce), options); return future.takeResult(); } template ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(begin, end, std::forward(map), std::forward(reduce), options); return future.takeResult(); } #ifdef Q_QDOC template #else template , int> = 0> #endif ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( pool, begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } #ifdef Q_QDOC template #else template , int> = 0> #endif ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } template ::type> ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(pool, begin, end, std::forward(map), std::forward(reduce), options); return future.takeResult(); } template ::type> ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced(begin, end, std::forward(map), std::forward(reduce), options); return future.takeResult(); } #ifdef Q_QDOC template #else template ::type, typename InitialValueType, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( pool, begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } #ifdef Q_QDOC template #else template , int> = 0, typename ResultType = typename QtPrivate::ReduceResultTypeHelper::type, typename InitialValueType, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor &&map, ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } // mapped() for sequences with a different putput sequence type. template OutputSequence blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map) { return blockingMappedReduced(pool, std::forward(sequence), std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template OutputSequence blockingMapped(InputSequence &&sequence, MapFunctor &&map) { return blockingMappedReduced( QThreadPool::globalInstance(), std::forward(sequence), std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map) { using OutputSequence = typename QtPrivate::MapSequenceResultType, MapFunctor>::ResultType; return blockingMappedReduced(pool, std::forward(sequence), std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(InputSequence &&sequence, MapFunctor &&map) { using OutputSequence = typename QtPrivate::MapSequenceResultType, MapFunctor>::ResultType; return blockingMappedReduced(QThreadPool::globalInstance(), std::forward(sequence), std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } // mapped() for iterator ranges template Sequence blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { return blockingMappedReduced(pool, begin, end, std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor &&map) { return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { using OutputSequence = QtPrivate::MapResultType; return blockingMappedReduced(pool, begin, end, std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(Iterator begin, Iterator end, MapFunctor &&map) { using OutputSequence = QtPrivate::MapResultType; return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } } // namespace QtConcurrent QT_END_NAMESPACE #endif // QT_NO_CONCURRENT #endif