From d57adfe5f375121c3e233465526ee5d9df03e1e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 26 Feb 2020 18:06:49 +0100 Subject: 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 Reviewed-by: Timur Pocheptsov --- src/concurrent/qtconcurrentfilterkernel.h | 44 +++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'src/concurrent/qtconcurrentfilterkernel.h') 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(initialValue)), + keep(_keep), + reduce(_reduce), + reducer(reduceOption) + { + } bool runIteration(Iterator it, int index, ReducedResultType *) override { @@ -337,6 +338,33 @@ inline ThreadEngineStarter 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 +inline ThreadEngineStarter startFilteredReduced(const Sequence & sequence, + MapFunctor mapFunctor, ReduceFunctor reduceFunctor, + ResultType &&initialValue, + ReduceOptions options) +{ + typedef typename Sequence::const_iterator Iterator; + typedef ReduceKernel::value_type > Reducer; + typedef FilteredReducedKernel FilteredReduceType; + typedef SequenceHolder2 SequenceHolderType; + return startThreadEngine(new SequenceHolderType(sequence, mapFunctor, reduceFunctor, std::forward(initialValue), options)); +} + +//! [QtConcurrent-7] +template +inline ThreadEngineStarter startFilteredReduced(Iterator begin, Iterator end, + MapFunctor mapFunctor, ReduceFunctor reduceFunctor, + ResultType &&initialValue, + ReduceOptions options) +{ + typedef ReduceKernel::value_type> Reducer; + typedef FilteredReducedKernel FilteredReduceType; + return startThreadEngine(new FilteredReduceType(begin, end, mapFunctor, reduceFunctor, std::forward(initialValue), options)); +} + } // namespace QtConcurrent -- cgit v1.2.3