/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtConcurrent module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or (at your option) the GNU General ** Public license version 3 or any later version approved by the KDE Free ** Qt Foundation. The licenses are as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-2.0.html and ** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTCONCURRENT_MAP_H #define QTCONCURRENT_MAP_H #include #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC) #include #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_CLANG_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_CLANG_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 >::ResultType> 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 >::ResultType> 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_CLANG_QDOC template #else template >::ResultType, typename InitialValueType, 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_CLANG_QDOC template #else template >::ResultType, typename InitialValueType, 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_CLANG_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_CLANG_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 >::ResultType> 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 >::ResultType> 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_CLANG_QDOC template #else template >::ResultType, 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_CLANG_QDOC template #else template>::ResultType, 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_CLANG_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_CLANG_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 >::ResultType> 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> 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_CLANG_QDOC template #else template >::ResultType, typename InitialValueType, 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_CLANG_QDOC template #else template>::ResultType, typename InitialValueType, 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_CLANG_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_CLANG_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 >::ResultType> 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> 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_CLANG_QDOC template #else template >::ResultType, 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_CLANG_QDOC template #else template >::ResultType, 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