<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qtconcurrentfilter.cpp --> <title>Concurrent Filter and Filter-Reduce | Qt Concurrent 5.12.2</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtconcurrent-index.html">Qt Concurrent</a></td><td >Concurrent Filter and Filter-Reduce</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtconcurrent-index.html">Qt 5.12.2 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#concurrent-filter">Concurrent Filter</a></li> <li class="level1"><a href="#concurrent-filter-reduce">Concurrent Filter-Reduce</a></li> <li class="level1"><a href="#additional-api-features">Additional API Features</a></li> <li class="level2"><a href="#using-iterators-instead-of-sequence">Using Iterators instead of Sequence</a></li> <li class="level2"><a href="#using-member-functions">Using Member Functions</a></li> <li class="level2"><a href="#using-function-objects">Using Function Objects</a></li> <li class="level2"><a href="#wrapping-functions-that-take-multiple-arguments">Wrapping Functions that Take Multiple Arguments</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Concurrent Filter and Filter-Reduce</h1> <span class="subtitle"></span> <!-- $$$qtconcurrentfilter.html-description --> <div class="descr"> <a name="details"></a> <p>The QtConcurrent::filter(), QtConcurrent::filtered() and QtConcurrent::filteredReduced() functions filter items in a sequence such as a <a href="../qtcore/qlist.html">QList</a> or a <a href="../qtcore/qvector.html">QVector</a> in parallel. QtConcurrent::filter() modifies a sequence in-place, QtConcurrent::filtered() returns a new sequence containing the filtered content, and QtConcurrent::filteredReduced() returns a single result.</p> <p>These functions are a part of the <a href="qtconcurrent-index.html">Qt Concurrent</a> framework.</p> <p>Each of the above functions have a blocking variant that returns the final result instead of a <a href="../qtcore/qfuture.html">QFuture</a>. You use them in the same way as the asynchronous variants.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="comment">// each call blocks until the entire operation is finished</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> lowerCaseStrings <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingFiltered(strings<span class="operator">,</span> allLowerCase); <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingFilter(strings<span class="operator">,</span> allLowerCase); <span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> dictionary <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingFilteredReduced(strings<span class="operator">,</span> allLowerCase<span class="operator">,</span> addToDictionary); </pre> <p>Note that the result types above are not <a href="../qtcore/qfuture.html">QFuture</a> objects, but real result types (in this case, <a href="../qtcore/qstringlist.html">QStringList</a> and <a href="../qtcore/qset.html">QSet</a><<a href="../qtcore/qstring.html">QString</a>>).</p> <a name="concurrent-filter"></a> <h2 id="concurrent-filter">Concurrent Filter</h2> <p>QtConcurrent::filtered() takes an input sequence and a filter function. This filter function is then called for each item in the sequence, and a new sequence containing the filtered values is returned.</p> <p>The filter function must be of the form:</p> <pre class="cpp"> bool function(<span class="keyword">const</span> T <span class="operator">&</span>t); </pre> <p>T must match the type stored in the sequence. The function returns <code>true</code> if the item should be kept, false if it should be discarded.</p> <p>This example shows how to keep strings that are all lower-case from a <a href="../qtcore/qstringlist.html">QStringList</a>:</p> <pre class="cpp"> bool allLowerCase(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>string) { <span class="keyword">return</span> string<span class="operator">.</span>lowered() <span class="operator">=</span><span class="operator">=</span> string; } <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> lowerCaseStrings <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filtered(strings<span class="operator">,</span> allLowerCase); </pre> <p>The results of the filter are made available through <a href="../qtcore/qfuture.html">QFuture</a>. See the <a href="../qtcore/qfuture.html">QFuture</a> and <a href="../qtcore/qfuturewatcher.html">QFutureWatcher</a> documentation for more information on how to use <a href="../qtcore/qfuture.html">QFuture</a> in your applications.</p> <p>If you want to modify a sequence in-place, use QtConcurrent::filter():</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filter(strings<span class="operator">,</span> allLowerCase); </pre> <p>Since the sequence is modified in place, QtConcurrent::filter() does not return any results via <a href="../qtcore/qfuture.html">QFuture</a>. However, you can still use <a href="../qtcore/qfuture.html">QFuture</a> and <a href="../qtcore/qfuturewatcher.html">QFutureWatcher</a> to monitor the status of the filter.</p> <a name="concurrent-filter-reduce"></a> <h2 id="concurrent-filter-reduce">Concurrent Filter-Reduce</h2> <p>QtConcurrent::filteredReduced() is similar to QtConcurrent::filtered(), but instead of returing a sequence with the filtered results, the results are combined into a single value using a reduce function.</p> <p>The reduce function must be of the form:</p> <pre class="cpp"> V function(T <span class="operator">&</span>result<span class="operator">,</span> <span class="keyword">const</span> U <span class="operator">&</span>intermediate) </pre> <p>T is the type of the final result, U is the type of items being filtered. Note that the return value and return type of the reduce function are not used.</p> <p>Call QtConcurrent::filteredReduced() like this:</p> <pre class="cpp"> <span class="type">void</span> addToDictionary(<span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> <span class="operator">&</span>dictionary<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>string) { dictionary<span class="operator">.</span>insert(string); } <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> <span class="operator">></span> dictionary <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filteredReduced(strings<span class="operator">,</span> allLowerCase<span class="operator">,</span> addToDictionary); </pre> <p>The reduce function will be called once for each result kept by the filter function, and should merge the <i>intermediate</i> into the <i>result</i> variable. QtConcurrent::filteredReduced() guarantees that only one thread will call reduce at a time, so using a mutex to lock the result variable is not necessary. The <a href="qtconcurrent.html#ReduceOption-enum">QtConcurrent::ReduceOptions</a> enum provides a way to control the order in which the reduction is done.</p> <a name="additional-api-features"></a> <h2 id="additional-api-features">Additional API Features</h2> <a name="using-iterators-instead-of-sequence"></a> <h3 >Using Iterators instead of Sequence</h3> <p>Each of the above functions has a variant that takes an iterator range instead of a sequence. You use them in the same way as the sequence variants:</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> lowerCaseStrings <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filtered(strings<span class="operator">.</span>constBegin()<span class="operator">,</span> strings<span class="operator">.</span>constEnd()<span class="operator">,</span> allLowerCase); <span class="comment">// filter in-place only works on non-const iterators</span> <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filter(strings<span class="operator">.</span>begin()<span class="operator">,</span> strings<span class="operator">.</span>end()<span class="operator">,</span> allLowerCase); <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> <span class="operator">></span> dictionary <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filteredReduced(strings<span class="operator">.</span>constBegin()<span class="operator">,</span> strings<span class="operator">.</span>constEnd()<span class="operator">,</span> allLowerCase<span class="operator">,</span> addToDictionary); </pre> <a name="using-member-functions"></a> <h3 >Using Member Functions</h3> <p>QtConcurrent::filter(), QtConcurrent::filtered(), and QtConcurrent::filteredReduced() accept pointers to member functions. The member function class type must match the type stored in the sequence:</p> <pre class="cpp"> <span class="comment">// keep only images with an alpha channel</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> alphaImages <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filter(images<span class="operator">,</span> <span class="operator">&</span><span class="type">QImage</span><span class="operator">::</span>hasAlphaChannel); <span class="comment">// retrieve gray scale images</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> grayscaleImages <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filtered(images<span class="operator">,</span> <span class="operator">&</span><span class="type">QImage</span><span class="operator">::</span>isGrayscale); <span class="comment">// create a set of all printable characters</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qchar.html">QChar</a></span><span class="operator">></span> characters <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qchar.html">QChar</a></span><span class="operator">></span> <span class="operator">></span> set <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filteredReduced(characters<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qchar.html">QChar</a></span><span class="operator">::</span>isPrint<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qchar.html">QChar</a></span><span class="operator">></span><span class="operator">::</span>insert); </pre> <p>Note that when using QtConcurrent::filteredReduced(), you can mix the use of normal and member functions freely:</p> <pre class="cpp"> <span class="comment">// can mix normal functions and member functions with QtConcurrent::filteredReduced()</span> <span class="comment">// create a dictionary of all lower cased strings</span> <span class="keyword">extern</span> bool allLowerCase(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>string); <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type">int</span><span class="operator">></span> <span class="operator">></span> averageWordLength <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filteredReduced(strings<span class="operator">,</span> allLowerCase<span class="operator">,</span> <span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span><span class="operator">::</span>insert); <span class="comment">// create a collage of all gray scale images</span> <span class="keyword">extern</span> <span class="type">void</span> addToCollage(<span class="type">QImage</span> <span class="operator">&</span>collage<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>grayscaleImage); <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filteredReduced(images<span class="operator">,</span> <span class="operator">&</span><span class="type">QImage</span><span class="operator">::</span>isGrayscale<span class="operator">,</span> addToCollage); </pre> <a name="using-function-objects"></a> <h3 >Using Function Objects</h3> <p>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:</p> <pre class="cpp"> <span class="keyword">struct</span> StartsWith { StartsWith(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>string) : m_string(string) { } <span class="keyword">typedef</span> bool result_type; bool <span class="keyword">operator</span>()(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>testString) { <span class="keyword">return</span> testString<span class="operator">.</span>startsWith(m_string); } <span class="type"><a href="../qtcore/qstring.html">QString</a></span> m_string; }; <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> fooString <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filtered(strings<span class="operator">,</span> StartsWith(QLatin1String(<span class="string">"Foo"</span>))); </pre> <p>For the reduce function, function objects are not directly supported. Function objects can, however, be used when the type of the reduction result is explicitly specified:</p> <pre class="cpp"> <span class="keyword">struct</span> StringTransform { <span class="type">void</span> <span class="keyword">operator</span>()(<span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>result<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>value); }; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> fooString <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filteredReduced<span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span>(strings<span class="operator">,</span> StartsWith(QLatin1String(<span class="string">"Foo"</span>))<span class="operator">,</span> StringTransform()); </pre> <a name="wrapping-functions-that-take-multiple-arguments"></a> <h3 >Wrapping Functions that Take Multiple Arguments</h3> <p>If you want to use a filter function takes more than one argument, you can use a lambda function or <code>std::bind()</code> to transform it onto a function that takes one argument.</p> <p>As an example, we use <a href="../qtcore/qstring.html#contains-1">QString::contains</a>():</p> <pre class="cpp"> bool <span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>contains(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> <span class="operator">&</span>regexp) <span class="keyword">const</span>; </pre> <p><a href="../qtcore/qstring.html#contains-1">QString::contains</a>() takes 2 arguments (including the "this" pointer) and can't be used with QtConcurrent::filtered() directly, because QtConcurrent::filtered() expects a function that takes one argument. To use <a href="../qtcore/qstring.html#contains-1">QString::contains</a>() with QtConcurrent::filtered() we have to provide a value for the <i>regexp</i> argument:</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>filtered(list<span class="operator">,</span> <span class="operator">[</span><span class="operator">]</span>(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>str) { <span class="keyword">return</span> str<span class="operator">.</span>contains(<span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span>(<span class="string">"^\\S+$"</span>)); <span class="comment">// matches strings without whitespace</span> }); </pre> </div> <!-- @@@qtconcurrentfilter.html --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>