From 786b48878f37edafd5eb928ed0f4d046ee1d6bec Mon Sep 17 00:00:00 2001 From: Karsten Heimrich Date: Thu, 26 Mar 2020 15:47:04 +0100 Subject: Improve Map|Map-Reduce and Filter|Filter-Reduce implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support lambda expressions * remove the need to specify result_type * use std::invoke to apply map|filter function * remove usage of FunctionWrapper* and createFunctionWrapper Task-number: QTBUG-33735 Task-number: QTBUG-82646 Change-Id: Ibcbe4278f0742c29182bd506081db0abb516f85f Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Sona Kurazyan --- .../code/src_concurrent_qtconcurrentfilter.cpp | 56 ++++- .../code/src_concurrent_qtconcurrentmap.cpp | 37 ++- src/concurrent/qtconcurrentcompilertest.h | 28 ++- src/concurrent/qtconcurrentfilter.cpp | 23 +- src/concurrent/qtconcurrentfilter.h | 79 +++--- src/concurrent/qtconcurrentfilterkernel.h | 20 +- src/concurrent/qtconcurrentfunctionwrappers.h | 272 ++++----------------- src/concurrent/qtconcurrentmap.cpp | 51 ++-- src/concurrent/qtconcurrentmap.h | 132 +++++----- src/concurrent/qtconcurrentmapkernel.h | 25 +- src/concurrent/qtconcurrentreducekernel.h | 2 +- 11 files changed, 336 insertions(+), 389 deletions(-) (limited to 'src') diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp index 3cc1fe836c..ef87a60080 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp @@ -158,8 +158,6 @@ struct StartsWith StartsWith(const QString &string) : m_string(string) { } - typedef bool result_type; - bool operator()(const QString &testString) { return testString.startsWith(m_string); @@ -183,3 +181,57 @@ QFuture fooString = StartsWith(QLatin1String("Foo")), StringTransform()); //! [14] + +//! [15] +// keep only even integers +QVector vector { 1, 2, 3, 4 }; +QtConcurrent::blockingFilter(vector, [](int n) { return (n & 1) == 0; }); + +// retrieve only even integers +QVector vector2 { 1, 2, 3, 4 }; +QFuture future = QtConcurrent::filtered(vector2, [](int x) { + return (x & 1) == 0; +}); +QVector results = future.results(); + +// add up all even integers +QVector vector3 { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced(vector3, + [](int x) { + return (x & 1) == 0; + }, + [](int &sum, int x) { + sum += x; + } +); +//! [15] + +//! [16] +void intSumReduce(int &sum, int x) +{ + sum += x; +} + +QVector vector { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced(vector, + [] (int x) { + return (x & 1) == 0; + }, + intSumReduce +); +//! [16] + +//! [17] +bool keepEvenIntegers(int x) +{ + return (x & 1) == 0; +} + +QVector vector { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced(vector, + keepEvenIntegers, + [](int &sum, int x) { + sum += x; + } +); +//! [17] diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp index fc574302d2..dd3e0103bb 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp @@ -130,7 +130,8 @@ QFuture squeezedStrings = QtConcurrent::map(strings, &QString::squeeze); // Swap the rgb values of all pixels on a list of images. QList images = ...; -QFuture bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped); +QFuture bgrImages = QtConcurrent::mapped(images, + static_cast(&QImage::rgbSwapped)); // Create a set of the lengths of all strings in a list. QStringList strings = ...; @@ -197,3 +198,37 @@ struct Scaled QList images = ...; QFuture thumbnails = QtConcurrent::mapped(images, Scaled(100)); //! [14] + +//! [15] +QVector vector { 1, 2, 3, 4 }; +QtConcurrent::blockingMap(vector, [](int &x) { x *= 2; }); + +int size = 100; +QVector images = ...; + +QVector thumbnails = QtConcurrent::mapped(images, + [&size](const QImage &image) { + return image.scaled(size, size); + } + ).results(); +//! [15] + +//! [16] +QVector collage = QtConcurrent::mappedReduced(images, + [&size](const QImage &image) { + return image.scaled(size, size); + }, + addToCollage + ).results(); +//! [16] + +//! [17] +QVector collage = QtConcurrent::mappedReduced(images, + [&size](const QImage &image) { + return image.scaled(size, size); + }, + [](QImage &result, const QImage &value) { + // do some transformation + } + ).results(); +//! [17] diff --git a/src/concurrent/qtconcurrentcompilertest.h b/src/concurrent/qtconcurrentcompilertest.h index 72cf1670a0..8292d5c504 100644 --- a/src/concurrent/qtconcurrentcompilertest.h +++ b/src/concurrent/qtconcurrentcompilertest.h @@ -48,16 +48,26 @@ QT_BEGIN_NAMESPACE namespace QtPrivate { -template -class HasResultType { - typedef char Yes; - typedef void *No; - template static Yes test(int, const typename U::result_type * = nullptr); - template static No test(double); -public: - enum { Value = (sizeof(test(0)) == sizeof(Yes)) }; -}; + template + struct IsIterable : std::false_type {}; + template + struct IsIterable())), + decltype(std::end(std::declval()))>> + : std::true_type + { }; + template + inline constexpr bool IsIterableValue = IsIterable::value; + + template + struct IsDereferenceable : std::false_type {}; + template + struct IsDereferenceable())>> + : std::true_type + { }; + + template + inline constexpr bool IsDereferenceableValue = IsDereferenceable::value; } QT_END_NAMESPACE diff --git a/src/concurrent/qtconcurrentfilter.cpp b/src/concurrent/qtconcurrentfilter.cpp index bac8ac59fc..789188911e 100644 --- a/src/concurrent/qtconcurrentfilter.cpp +++ b/src/concurrent/qtconcurrentfilter.cpp @@ -144,8 +144,7 @@ QtConcurrent::filter(), QtConcurrent::filtered(), and QtConcurrent::filteredReduced() accept function objects for the filter function. These function objects can be used to - add state to a function call. The result_type typedef must define the - result type of the function call operator: + add state to a function call: \snippet code/src_concurrent_qtconcurrentfilter.cpp 13 @@ -155,6 +154,26 @@ \snippet code/src_concurrent_qtconcurrentfilter.cpp 14 + \section2 Using Lambda Expressions + + QtConcurrent::filter(), QtConcurrent::filtered(), and + QtConcurrent::filteredReduced() accept lambda expressions for the filter and + reduce function: + + \snippet code/src_concurrent_qtconcurrentfilter.cpp 15 + + When using QtConcurrent::filteredReduced() or + QtConcurrent::blockingFilteredReduced(), you can mix the use of normal + functions, member functions and lambda expressions freely. + + \snippet code/src_concurrent_qtconcurrentfilter.cpp 16 + + For the reduce function, lambda expressions are not directly supported. + Lambda expressions can, however, be used when the type of the reduction + result is explicitly specified: + + \snippet code/src_concurrent_qtconcurrentfilter.cpp 17 + \section2 Wrapping Functions that Take Multiple Arguments If you want to use a filter function takes more than one argument, you can diff --git a/src/concurrent/qtconcurrentfilter.h b/src/concurrent/qtconcurrentfilter.h index 4c7d1ee7e5..df4c9ed384 100644 --- a/src/concurrent/qtconcurrentfilter.h +++ b/src/concurrent/qtconcurrentfilter.h @@ -63,7 +63,7 @@ ThreadEngineStarter filterInternal(Sequence &sequence, KeepFunctor keep, R template QFuture filter(Sequence &sequence, KeepFunctor keep) { - return filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper()); + return filterInternal(sequence, keep, QtPrivate::PushBackWrapper()); } // filteredReduced() on sequences @@ -73,7 +73,7 @@ QFuture filteredReduced(const Sequence &sequence, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options); + return startFilteredReduced(sequence, keep, reduce, options); } template filteredReduced(const Sequence &sequence, KeepFunctor keep, | SequentialReduce)) { return startFilteredReduced( - sequence, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + sequence, keep, + reduce, ResultType(std::forward(initialValue)), options); } @@ -99,8 +99,8 @@ QFuture::ResultType> filtere { return startFilteredReduced::ResultType> (sequence, - QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + keep, + reduce, options); } @@ -114,8 +114,8 @@ QFuture filteredReduced(const Sequence &sequence, KeepFunctor keep, | SequentialReduce)) { return startFilteredReduced( - sequence, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + sequence, keep, + reduce, ResultType(std::forward(initialValue)), options); } #endif @@ -128,7 +128,7 @@ QFuture filteredReduced(Iterator begin, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(begin, end, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options); + return startFilteredReduced(begin, end, keep, reduce, options); } template filteredReduced(Iterator begin, Iterator end, KeepFunctor ke | SequentialReduce)) { return startFilteredReduced( - begin, end, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + begin, end, keep, + reduce, ResultType(std::forward(initialValue)), options); } @@ -155,8 +155,8 @@ QFuture::ResultType> filtere { return startFilteredReduced::ResultType> (begin, end, - QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + keep, + reduce, options); } @@ -170,8 +170,8 @@ QFuture filteredReduced(Iterator begin, Iterator end, KeepFunctor ke | SequentialReduce)) { return startFilteredReduced( - begin, end, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + begin, end, keep, + reduce, ResultType(std::forward(initialValue)), options); } #endif @@ -180,21 +180,21 @@ QFuture filteredReduced(Iterator begin, Iterator end, KeepFunctor ke template QFuture filtered(const Sequence &sequence, KeepFunctor keep) { - return startFiltered(sequence, QtPrivate::createFunctionWrapper(keep)); + return startFiltered(sequence, keep); } // filtered() on iterators template QFuture::value_type> filtered(Iterator begin, Iterator end, KeepFunctor keep) { - return startFiltered(begin, end, QtPrivate::createFunctionWrapper(keep)); + return startFiltered(begin, end, keep); } // blocking filter() on sequences template void blockingFilter(Sequence &sequence, KeepFunctor keep) { - filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper()).startBlocking(); + filterInternal(sequence, keep, QtPrivate::PushBackWrapper()).startBlocking(); } // blocking filteredReduced() on sequences @@ -204,7 +204,7 @@ ResultType blockingFilteredReduced(const Sequence &sequence, ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options) + return startFilteredReduced(sequence, keep, reduce, options) .startBlocking(); } @@ -217,8 +217,8 @@ ResultType blockingFilteredReduced(const Sequence &sequence, KeepFunctor keep, R | SequentialReduce)) { return startFilteredReduced( - sequence, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + sequence, keep, + reduce, ResultType(std::forward(initialValue)), options) .startBlocking(); } @@ -230,11 +230,11 @@ typename QtPrivate::ReduceResultType::ResultType blockingFiltered ReduceFunctor reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return blockingFilteredReduced::ResultType> + return startFilteredReduced::ResultType> (sequence, - QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), - options); + keep, + reduce, + options).startBlocking(); } template ( - sequence, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), - ResultType(std::forward(initialValue)), options); + return startFilteredReduced( + sequence, keep, + reduce, + ResultType(std::forward(initialValue)), options) + .startBlocking(); } #endif @@ -263,8 +264,8 @@ ResultType blockingFilteredReduced(Iterator begin, { return startFilteredReduced (begin, end, - QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + keep, + reduce, options) .startBlocking(); } @@ -278,8 +279,8 @@ ResultType blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor kee | SequentialReduce)) { return startFilteredReduced( - begin, end, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + begin, end, keep, + reduce, ResultType(std::forward(initialValue)), options) .startBlocking(); } @@ -294,8 +295,8 @@ typename QtPrivate::ReduceResultType::ResultType blockingFiltered { return startFilteredReduced::ResultType> (begin, end, - QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + keep, + reduce, options) .startBlocking(); } @@ -310,8 +311,8 @@ ResultType blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor kee | SequentialReduce)) { return startFilteredReduced( - begin, end, QtPrivate::createFunctionWrapper(keep), - QtPrivate::createFunctionWrapper(reduce), + begin, end, keep, + reduce, ResultType(std::forward(initialValue)), options) .startBlocking(); } @@ -321,7 +322,7 @@ ResultType blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor kee template Sequence blockingFiltered(const Sequence &sequence, KeepFunctor keep) { - return startFilteredReduced(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper(), OrderedReduce).startBlocking(); + return startFilteredReduced(sequence, keep, QtPrivate::PushBackWrapper(), OrderedReduce).startBlocking(); } // blocking filtered() on iterators @@ -329,7 +330,7 @@ template OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor keep) { return startFilteredReduced(begin, end, - QtPrivate::createFunctionWrapper(keep), + keep, QtPrivate::PushBackWrapper(), OrderedReduce).startBlocking(); } diff --git a/src/concurrent/qtconcurrentfilterkernel.h b/src/concurrent/qtconcurrentfilterkernel.h index 8ec551eeb2..babd173ff8 100644 --- a/src/concurrent/qtconcurrentfilterkernel.h +++ b/src/concurrent/qtconcurrentfilterkernel.h @@ -78,7 +78,7 @@ class FilterKernel : public IterateKernel Reducer; typedef IterateKernel IterateKernelType; - typedef typename ReduceFunctor::result_type T; + typedef void T; Sequence reducedResult; Sequence &sequence; @@ -101,11 +101,11 @@ public: results.begin = index; results.end = index + 1; - if (keep(*it)) - results.vector.append(*it); + if (std::invoke(keep, *it)) + results.vector.append(*it); - reducer.runReduce(reduce, reducedResult, results); - return false; + reducer.runReduce(reduce, reducedResult, results); + return false; } bool runIterations(typename Sequence::const_iterator sequenceBeginIterator, int begin, int end, T *) override @@ -119,7 +119,7 @@ public: typename Sequence::const_iterator it = sequenceBeginIterator; std::advance(it, begin); for (int i = begin; i < end; ++i) { - if (keep(*it)) + if (std::invoke(keep, *it)) results.vector.append(*it); std::advance(it, 1); } @@ -189,7 +189,7 @@ public: results.begin = index; results.end = index + 1; - if (keep(*it)) + if (std::invoke(keep, *it)) results.vector.append(*it); reducer.runReduce(reduce, reducedResult, results); @@ -206,7 +206,7 @@ public: Iterator it = sequenceBeginIterator; std::advance(it, begin); for (int i = begin; i < end; ++i) { - if (keep(*it)) + if (std::invoke(keep, *it)) results.vector.append(*it); std::advance(it, 1); } @@ -264,7 +264,7 @@ public: bool runIteration(Iterator it, int index, T *) override { - if (keep(*it)) + if (std::invoke(keep, *it)) this->reportResult(&(*it), index); else this->reportResult(nullptr, index); @@ -282,7 +282,7 @@ public: Iterator it = sequenceBeginIterator; std::advance(it, begin); for (int i = begin; i < end; ++i) { - if (keep(*it)) + if (std::invoke(keep, *it)) results.vector.append(*it); std::advance(it, 1); } diff --git a/src/concurrent/qtconcurrentfunctionwrappers.h b/src/concurrent/qtconcurrentfunctionwrappers.h index 4731de77cc..34bbe89c86 100644 --- a/src/concurrent/qtconcurrentfunctionwrappers.h +++ b/src/concurrent/qtconcurrentfunctionwrappers.h @@ -43,165 +43,64 @@ #include #include +#include + #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC) QT_BEGIN_NAMESPACE -namespace QtConcurrent { +namespace QtPrivate { -template -class FunctionWrapper1 +struct PushBackWrapper { -public: - typedef T (*FunctionPointerType)(U u); - typedef T result_type; - inline FunctionWrapper1(FunctionPointerType _functionPointer) - :functionPointer(_functionPointer) { } - - inline T operator()(U u) + template + inline void operator()(C &c, const U &u) const { - return functionPointer(u); + return c.push_back(u); } -private: - FunctionPointerType functionPointer; -}; - -template -class MemberFunctionWrapper -{ -public: - typedef T (C::*FunctionPointerType)(); - typedef T result_type; - inline MemberFunctionWrapper(FunctionPointerType _functionPointer) - :functionPointer(_functionPointer) { } - - inline T operator()(C &c) + template + inline void operator()(C &c, U &&u) const { - return (c.*functionPointer)(); + return c.push_back(u); } -private: - FunctionPointerType functionPointer; }; -template -class MemberFunctionWrapper1 -{ -public: - typedef T (C::*FunctionPointerType)(U); - typedef T result_type; - - inline MemberFunctionWrapper1(FunctionPointerType _functionPointer) - : functionPointer(_functionPointer) - { } - - inline T operator()(C &c, U u) - { - return (c.*functionPointer)(u); - } - -private: - FunctionPointerType functionPointer; -}; +// -- MapResultType -template -class ConstMemberFunctionWrapper +template +struct Argument { -public: - typedef T (C::*FunctionPointerType)() const; - typedef T result_type; - inline ConstMemberFunctionWrapper(FunctionPointerType _functionPointer) - :functionPointer(_functionPointer) { } - - inline T operator()(const C &c) const - { - return (c.*functionPointer)(); - } -private: - FunctionPointerType functionPointer; + using Type = void; }; -} // namespace QtConcurrent. - -namespace QtPrivate { - -template -const T& createFunctionWrapper(const T& t) +template +struct Argument>::type> { - return t; -} - -template -QtConcurrent::FunctionWrapper1 createFunctionWrapper(T (*func)(U)) -{ - return QtConcurrent::FunctionWrapper1(func); -} - -template -QtConcurrent::MemberFunctionWrapper createFunctionWrapper(T (C::*func)()) -{ - return QtConcurrent::MemberFunctionWrapper(func); -} - -template -QtConcurrent::MemberFunctionWrapper1 createFunctionWrapper(T (C::*func)(U)) -{ - return QtConcurrent::MemberFunctionWrapper1(func); -} - -template -QtConcurrent::ConstMemberFunctionWrapper createFunctionWrapper(T (C::*func)() const) -{ - return QtConcurrent::ConstMemberFunctionWrapper(func); -} - -#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510 -template -QtConcurrent::FunctionWrapper1 createFunctionWrapper(T (*func)(U) noexcept) -{ - return QtConcurrent::FunctionWrapper1(func); -} + using Type = std::decay_t()))>; +}; -template -QtConcurrent::MemberFunctionWrapper createFunctionWrapper(T (C::*func)() noexcept) +template +struct Argument>::type> { - return QtConcurrent::MemberFunctionWrapper(func); -} + using Type = std::decay_t())>; +}; -template -QtConcurrent::MemberFunctionWrapper1 createFunctionWrapper(T (C::*func)(U) noexcept) -{ - return QtConcurrent::MemberFunctionWrapper1(func); -} +template +using ArgumentType = typename Argument::Type; -template -QtConcurrent::ConstMemberFunctionWrapper createFunctionWrapper(T (C::*func)() const noexcept) +template +struct MapResult { - return QtConcurrent::ConstMemberFunctionWrapper(func); -} -#endif - -struct PushBackWrapper -{ - typedef void result_type; - - template - inline void operator()(C &c, const U &u) const - { - return c.push_back(u); - } - - template - inline void operator()(C &c, U &&u) const - { - return c.push_back(u); - } + static_assert(std::is_invocable_v, ArgumentType>, + "It's not possible to invoke the function with passed argument."); + using Type = std::invoke_result_t, ArgumentType>; }; -template ::Value> -struct LazyResultType { typedef typename Functor::result_type Type; }; -template -struct LazyResultType { typedef void Type; }; +template +using MapResultType = typename MapResult::Type; + +// -- ReduceResultType template struct ReduceResultType; @@ -218,121 +117,52 @@ struct ReduceResultType typedef C ResultType; }; -#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510 -template -struct ReduceResultType -{ - typedef U ResultType; -}; - -template -struct ReduceResultType -{ - typedef C ResultType; -}; -#endif - -template -struct MapResultType -{ - typedef typename LazyResultType::Type ResultType; -}; - template -struct MapResultType +struct ReduceResultType> { typedef U ResultType; }; -template -struct MapResultType +template +struct ReduceResultType { - typedef T ResultType; + using ResultType = typename std::tuple_element<0, std::tuple>::type; }; #if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510 template -struct MapResultType +struct ReduceResultType { typedef U ResultType; }; -template -struct MapResultType +template +struct ReduceResultType { - typedef T ResultType; + typedef C ResultType; }; #endif -#ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS - -template