diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2020-02-26 18:06:49 +0100 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2020-03-04 17:40:45 +0100 |
commit | d57adfe5f375121c3e233465526ee5d9df03e1e9 (patch) | |
tree | ecf9892b6e7a954f145ef74743caa932033b5253 /src/concurrent/qtconcurrentfilterkernel.h | |
parent | d50b22e75df0e0fcecf987813b873e717a225e66 (diff) |
QtConcurrent: filter- and map-reduce with initial value
It takes any type which is implictly covertible to the result type
and then converts it in the outer-layers. Then it passes it into the
deeper layers and initiales the result value.
One drive-by fix with a missing letter in the documentation.
[ChangeLog][QtConcurrent] QtConcurrent::mappedReduce and
QtConcurrent::filteredReduced, as well as their blocking variants,
now optionally take an initial value.
Fixes: QTBUG-73240
Change-Id: I7a80d96693cfa3374847c75c75b3167664609c1a
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/concurrent/qtconcurrentfilterkernel.h')
-rw-r--r-- | src/concurrent/qtconcurrentfilterkernel.h | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/src/concurrent/qtconcurrentfilterkernel.h b/src/concurrent/qtconcurrentfilterkernel.h index e921a3d51a..8ec551eeb2 100644 --- a/src/concurrent/qtconcurrentfilterkernel.h +++ b/src/concurrent/qtconcurrentfilterkernel.h @@ -173,14 +173,15 @@ public: : IterateKernelType(begin, end), reducedResult(), keep(_keep), reduce(_reduce), reducer(reduceOption) { } -#if 0 - FilteredReducedKernel(ReducedResultType initialValue, - KeepFunctor keep, - ReduceFunctor reduce, - ReduceOption reduceOption) - : reducedResult(initialValue), keep(keep), reduce(reduce), reducer(reduceOption) - { } -#endif + FilteredReducedKernel(Iterator begin, Iterator end, KeepFunctor _keep, ReduceFunctor _reduce, + ReducedResultType &&initialValue, ReduceOptions reduceOption) + : IterateKernelType(begin, end), + reducedResult(std::forward<ReducedResultType>(initialValue)), + keep(_keep), + reduce(_reduce), + reducer(reduceOption) + { + } bool runIteration(Iterator it, int index, ReducedResultType *) override { @@ -337,6 +338,33 @@ inline ThreadEngineStarter<ResultType> startFilteredReduced(Iterator begin, Iter return startThreadEngine(new FilteredReduceType(begin, end, mapFunctor, reduceFunctor, options)); } +// Repeat the two functions above, but now with an initial value! +//! [QtConcurrent-6] +template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> +inline ThreadEngineStarter<ResultType> startFilteredReduced(const Sequence & sequence, + MapFunctor mapFunctor, ReduceFunctor reduceFunctor, + ResultType &&initialValue, + ReduceOptions options) +{ + typedef typename Sequence::const_iterator Iterator; + typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type > Reducer; + typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> FilteredReduceType; + typedef SequenceHolder2<Sequence, FilteredReduceType, MapFunctor, ReduceFunctor> SequenceHolderType; + return startThreadEngine(new SequenceHolderType(sequence, mapFunctor, reduceFunctor, std::forward<ResultType>(initialValue), options)); +} + +//! [QtConcurrent-7] +template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor> +inline ThreadEngineStarter<ResultType> startFilteredReduced(Iterator begin, Iterator end, + MapFunctor mapFunctor, ReduceFunctor reduceFunctor, + ResultType &&initialValue, + ReduceOptions options) +{ + typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type> Reducer; + typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> FilteredReduceType; + return startThreadEngine(new FilteredReduceType(begin, end, mapFunctor, reduceFunctor, std::forward<ResultType>(initialValue), options)); +} + } // namespace QtConcurrent |