diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2020-03-26 15:47:04 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@qt.io> | 2020-04-14 19:06:27 +0200 |
commit | 786b48878f37edafd5eb928ed0f4d046ee1d6bec (patch) | |
tree | 7dffd263964b1375dd9814d4edcfc675d7489e84 /src/concurrent/doc/snippets | |
parent | 678b9f78a5af4513ed4e988de90148584a2ae90d (diff) |
Improve Map|Map-Reduce and Filter|Filter-Reduce implementation
* 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 <marten.nordheim@qt.io>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src/concurrent/doc/snippets')
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp | 56 | ||||
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp | 37 |
2 files changed, 90 insertions, 3 deletions
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<QString> fooString = StartsWith(QLatin1String("Foo")), StringTransform()); //! [14] + +//! [15] +// keep only even integers +QVector<int> vector { 1, 2, 3, 4 }; +QtConcurrent::blockingFilter(vector, [](int n) { return (n & 1) == 0; }); + +// retrieve only even integers +QVector<int> vector2 { 1, 2, 3, 4 }; +QFuture<int> future = QtConcurrent::filtered(vector2, [](int x) { + return (x & 1) == 0; +}); +QVector<int> results = future.results(); + +// add up all even integers +QVector<int> vector3 { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced<int>(vector3, + [](int x) { + return (x & 1) == 0; + }, + [](int &sum, int x) { + sum += x; + } +); +//! [15] + +//! [16] +void intSumReduce(int &sum, int x) +{ + sum += x; +} + +QVector<int> 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<int> vector { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced<int>(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<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze); // Swap the rgb values of all pixels on a list of images. QList<QImage> images = ...; -QFuture<QImage> bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped); +QFuture<QImage> bgrImages = QtConcurrent::mapped(images, + static_cast<QImage (QImage::*)() const &>(&QImage::rgbSwapped)); // Create a set of the lengths of all strings in a list. QStringList strings = ...; @@ -197,3 +198,37 @@ struct Scaled QList<QImage> images = ...; QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100)); //! [14] + +//! [15] +QVector<int> vector { 1, 2, 3, 4 }; +QtConcurrent::blockingMap(vector, [](int &x) { x *= 2; }); + +int size = 100; +QVector<QImage> images = ...; + +QVector<QImage> thumbnails = QtConcurrent::mapped(images, + [&size](const QImage &image) { + return image.scaled(size, size); + } + ).results(); +//! [15] + +//! [16] +QVector<QImage> collage = QtConcurrent::mappedReduced(images, + [&size](const QImage &image) { + return image.scaled(size, size); + }, + addToCollage + ).results(); +//! [16] + +//! [17] +QVector<QImage> collage = QtConcurrent::mappedReduced<QImage>(images, + [&size](const QImage &image) { + return image.scaled(size, size); + }, + [](QImage &result, const QImage &value) { + // do some transformation + } + ).results(); +//! [17] |