summaryrefslogtreecommitdiffstats
path: root/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp
diff options
context:
space:
mode:
authorKarsten Heimrich <karsten.heimrich@qt.io>2020-03-26 15:47:04 +0100
committerKarsten Heimrich <karsten.heimrich@qt.io>2020-04-14 19:06:27 +0200
commit786b48878f37edafd5eb928ed0f4d046ee1d6bec (patch)
tree7dffd263964b1375dd9814d4edcfc675d7489e84 /src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp
parent678b9f78a5af4513ed4e988de90148584a2ae90d (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/code/src_concurrent_qtconcurrentfilter.cpp')
-rw-r--r--src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp56
1 files changed, 54 insertions, 2 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]