/**************************************************************************** ** ** 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(), map); } template QFuture map(Sequence &sequence, MapFunctor map) { return startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), map); } // map() on iterators template QFuture map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) { return startMap(pool, begin, end, map); } template QFuture map(Iterator begin, Iterator end, MapFunctor map) { return startMap(QThreadPool::globalInstance(), begin, end, map); } // mappedReduced() for sequences. template QFuture mappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, sequence, map, reduce, options); } template QFuture mappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, options); } template , int> = 0> QFuture mappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, sequence, map, reduce, ResultType(std::forward(initialValue)), options); } template , int> = 0> QFuture mappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, ResultType(std::forward(initialValue)), options); } template ::ResultType> QFuture mappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, sequence, map, reduce, options); } template ::ResultType> QFuture mappedReduced( const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, options); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture mappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (pool, sequence, map, reduce, ResultType(std::forward(initialValue)), options); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture mappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), sequence, map, 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, map, 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, map, reduce, options); } template , int> = 0> 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, map, reduce, ResultType(std::forward(initialValue)), options); } template , int> = 0> QFuture mappedReduced(Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), begin, end, map, 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, map, 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, map, reduce, options); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> 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, map, reduce, ResultType(std::forward(initialValue)), options); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture mappedReduced(Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> (QThreadPool::globalInstance(), begin, end, map, reduce, ResultType(std::forward(initialValue)), options); } // mapped() for sequences template QFuture> mapped( QThreadPool *pool, const Sequence &sequence, MapFunctor map) { return startMapped>(pool, sequence, map); } template QFuture> mapped( const Sequence &sequence, MapFunctor map) { return startMapped> (QThreadPool::globalInstance(), sequence, map); } // mapped() for iterator ranges. template QFuture> mapped( QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) { return startMapped>(pool, begin, end, map); } template QFuture> mapped( Iterator begin, Iterator end, MapFunctor map) { return startMapped> (QThreadPool::globalInstance(), begin, end, map); } // blockingMap() for sequences template void blockingMap(QThreadPool *pool, Sequence &sequence, MapFunctor map) { QFuture future = startMap(pool, sequence.begin(), sequence.end(), map); future.waitForFinished(); } template void blockingMap(Sequence &sequence, MapFunctor map) { QFuture future = startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), 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, map); future.waitForFinished(); } // blockingMappedReduced() for sequences template ResultType blockingMappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, sequence, map, reduce, options); return future.result(); } template ResultType blockingMappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, options); return future.result(); } template , int> = 0> ResultType blockingMappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, sequence, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } template , int> = 0> ResultType blockingMappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } template ::ResultType> ResultType blockingMappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, sequence, map, reduce, options); return future.result(); } template ::ResultType> ResultType blockingMappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, options); return future.result(); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingMappedReduced(QThreadPool *pool, const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, sequence, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingMappedReduced(const Sequence &sequence, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), sequence, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } // blockingMappedReduced() for iterator ranges template ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, begin, end, map, reduce, options); return future.result(); } template ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), begin, end, map, reduce, options); return future.result(); } template , int> = 0> ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, begin, end, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } template , int> = 0> ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), begin, end, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } template ::ResultType> ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, begin, end, map, reduce, options); return future.result(); } template ::ResultType> ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), begin, end, map, reduce, options); return future.result(); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (pool, begin, end, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } template ::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingMappedReduced(Iterator begin, Iterator end, MapFunctor map, ReduceFunctor reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = QtConcurrent::startMappedReduced , ResultType> (QThreadPool::globalInstance(), begin, end, map, reduce, ResultType(std::forward(initialValue)), options); return future.result(); } // mapped() for sequences with a different putput sequence type. template OutputSequence blockingMapped(QThreadPool *pool, const InputSequence &sequence, MapFunctor map) { return blockingMappedReduced(pool, sequence, map, QtPrivate::PushBackWrapper(), OrderedReduce); } template OutputSequence blockingMapped(const InputSequence &sequence, MapFunctor map) { return blockingMappedReduced(QThreadPool::globalInstance(), sequence, map, QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(QThreadPool *pool, const InputSequence &sequence, MapFunctor map) { using OutputSequence = typename QtPrivate::MapSequenceResultType::ResultType; return blockingMappedReduced(pool, sequence, map, QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(const InputSequence &sequence, MapFunctor map) { using OutputSequence = typename QtPrivate::MapSequenceResultType::ResultType; return blockingMappedReduced(QThreadPool::globalInstance(), sequence, map, QtPrivate::PushBackWrapper(), OrderedReduce); } // mapped() for iterator ranges template Sequence blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) { return blockingMappedReduced(pool, begin, end, map, QtPrivate::PushBackWrapper(), OrderedReduce); } template Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor map) { return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, map, QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) { using OutputSequence = QtPrivate::MapResultType; return blockingMappedReduced(pool, begin, end, map, QtPrivate::PushBackWrapper(), OrderedReduce); } template auto blockingMapped(Iterator begin, Iterator end, MapFunctor map) { using OutputSequence = QtPrivate::MapResultType; return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, map, QtPrivate::PushBackWrapper(), OrderedReduce); } } // namespace QtConcurrent QT_END_NAMESPACE #endif // QT_NO_CONCURRENT #endif