<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qtconcurrentmap.cpp --> <title>Concurrent Map and Map-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 Map and Map-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-map">Concurrent Map</a></li> <li class="level1"><a href="#concurrent-map-reduce">Concurrent Map-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="#blocking-variants">Blocking Variants</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 Map and Map-Reduce</h1> <span class="subtitle"></span> <!-- $$$qtconcurrentmap.html-description --> <div class="descr"> <a name="details"></a> <p>The QtConcurrent::map(), QtConcurrent::mapped() and QtConcurrent::mappedReduced() functions run computations in parallel on the items in a sequence such as a <a href="../qtcore/qlist.html">QList</a> or a <a href="../qtcore/qvector.html">QVector</a>. QtConcurrent::map() modifies a sequence in-place, QtConcurrent::mapped() returns a new sequence containing the modified content, and QtConcurrent::mappedReduced() 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 has 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/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="comment">// Each call blocks until the entire operation is finished.</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> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMapped(images<span class="operator">,</span> scaled); <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMap(images<span class="operator">,</span> scale); <span class="type">QImage</span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMappedReduced(images<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage); </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/qlist.html">QList</a><QImage> and QImage).</p> <a name="concurrent-map"></a> <h2 id="concurrent-map">Concurrent Map</h2> <p>QtConcurrent::mapped() takes an input sequence and a map function. This map function is then called for each item in the sequence, and a new sequence containing the return values from the map function is returned.</p> <p>The map function must be of the form:</p> <pre class="cpp"> U function(<span class="keyword">const</span> T <span class="operator">&</span>t); </pre> <p>T and U can be any type (and they can even be the same type), but T must match the type stored in the sequence. The function returns the modified or <i>mapped</i> content.</p> <p>This example shows how to apply a scale function to all the items in a sequence:</p> <pre class="cpp"> <span class="type">QImage</span> scaled(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>image) { <span class="keyword">return</span> image<span class="operator">.</span>scaled(<span class="number">100</span><span class="operator">,</span> <span class="number">100</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> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> scaled); </pre> <p>The results of the map 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::map(). The map function must then be of the form:</p> <pre class="cpp"> U function(T <span class="operator">&</span>t); </pre> <p>Note that the return value and return type of the map function are not used.</p> <p>Using QtConcurrent::map() is similar to using QtConcurrent::mapped():</p> <pre class="cpp"> <span class="type">void</span> scale(<span class="type">QImage</span> <span class="operator">&</span>image) { image <span class="operator">=</span> image<span class="operator">.</span>scaled(<span class="number">100</span><span class="operator">,</span> <span class="number">100</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> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>map(images<span class="operator">,</span> scale); </pre> <p>Since the sequence is modified in place, QtConcurrent::map() 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 map.</p> <a name="concurrent-map-reduce"></a> <h2 id="concurrent-map-reduce">Concurrent Map-Reduce</h2> <p>QtConcurrent::mappedReduced() is similar to QtConcurrent::mapped(), but instead of returning a sequence with the new 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 return type of the map function. Note that the return value and return type of the reduce function are not used.</p> <p>Call QtConcurrent::mappedReduced() like this:</p> <pre class="cpp"> <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>thumbnail) { <span class="type">QPainter</span> p(<span class="operator">&</span>collage); <span class="keyword">static</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> offset <span class="operator">=</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>); p<span class="operator">.</span>drawImage(offset<span class="operator">,</span> thumbnail); offset <span class="operator">+</span><span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</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> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(images<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage); </pre> <p>The reduce function will be called once for each result returned by the map function, and should merge the <i>intermediate</i> into the <i>result</i> variable. QtConcurrent::mappedReduced() 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. If <a href="qtconcurrent.html#ReduceOption-enum">QtConcurrent::UnorderedReduce</a> is used (the default), the order is undefined, while <a href="qtconcurrent.html#ReduceOption-enum">QtConcurrent::OrderedReduce</a> ensures that the reduction is done in the order of the original sequence.</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/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> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">.</span>constBegin()<span class="operator">,</span> images<span class="operator">.</span>constEnd()<span class="operator">,</span> scaled); <span class="comment">// Map 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>map(images<span class="operator">.</span>begin()<span class="operator">,</span> images<span class="operator">.</span>end()<span class="operator">,</span> scale); <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>mappedReduced(images<span class="operator">.</span>constBegin()<span class="operator">,</span> images<span class="operator">.</span>constEnd()<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage); </pre> <a name="blocking-variants"></a> <h3 >Blocking Variants</h3> <p>Each of the above functions has 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/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="comment">// Each call blocks until the entire operation is finished.</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> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMapped(images<span class="operator">,</span> scaled); <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMap(images<span class="operator">,</span> scale); <span class="type">QImage</span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMappedReduced(images<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage); </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/qlist.html">QList</a><QImage> and QImage).</p> <a name="using-member-functions"></a> <h3 >Using Member Functions</h3> <p>QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() 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">// Squeeze all strings in a QStringList.</span> <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> squeezedStrings <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>map(strings<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>squeeze); <span class="comment">// Swap the rgb values of all pixels on a list of 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> bgrImages <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> <span class="operator">&</span><span class="type">QImage</span><span class="operator">::</span>rgbSwapped); <span class="comment">// Create a set of the lengths of all strings in a list.</span> <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> wordLengths <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(strings<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>length<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">int</span><span class="operator">></span><span class="operator">::</span>insert); </pre> <p>Note that when using QtConcurrent::mappedReduced(), 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::mappedReduced().</span> <span class="comment">// Compute the average length of a list of strings.</span> <span class="keyword">extern</span> <span class="type">void</span> computeAverage(<span class="type">int</span> <span class="operator">&</span>average<span class="operator">,</span> <span class="type">int</span> length); <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">int</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>mappedReduced(strings<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>length<span class="operator">,</span> computeAverage); <span class="comment">// Create a set of the color distribution of all images in a list.</span> <span class="keyword">extern</span> <span class="type">int</span> colorDistribution(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>string); <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"><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> totalColorDistribution <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(images<span class="operator">,</span> colorDistribution<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>insert); </pre> <a name="using-function-objects"></a> <h3 >Using Function Objects</h3> <p>QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept function objects for the map 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> Scaled { Scaled(<span class="type">int</span> size) : m_size(size) { } <span class="keyword">typedef</span> <span class="type">QImage</span> result_type; <span class="type">QImage</span> <span class="keyword">operator</span>()(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>image) { <span class="keyword">return</span> image<span class="operator">.</span>scaled(m_size<span class="operator">,</span> m_size); } <span class="type">int</span> m_size; }; <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> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> Scaled(<span class="number">100</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> ImageTransform { <span class="type">void</span> <span class="keyword">operator</span>()(<span class="type">QImage</span> <span class="operator">&</span>result<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>value); }; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> thumbNails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced<span class="operator"><</span><span class="type">QImage</span><span class="operator">></span>(images<span class="operator">,</span> Scaled(<span class="number">100</span>)<span class="operator">,</span> ImageTransform()<span class="operator">,</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>SequentialReduce); </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 map function that 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'll use QImage::scaledToWidth():</p> <pre class="cpp"> <span class="type">QImage</span> <span class="type">QImage</span><span class="operator">::</span>scaledToWidth(<span class="type">int</span> width<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TransformationMode) <span class="keyword">const</span>; </pre> <p>scaledToWidth takes three arguments (including the "this" pointer) and can't be used with QtConcurrent::mapped() directly, because QtConcurrent::mapped() expects a function that takes one argument. To use QImage::scaledToWidth() with QtConcurrent::mapped() we have to provide a value for the <i>width</i> and the <i>transformation mode</i>:</p> <pre class="cpp"> <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>; std<span class="operator">::</span>function<span class="operator"><</span><span class="type">QImage</span>(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>)<span class="operator">></span> scale <span class="operator">=</span> <span class="operator">[</span><span class="operator">]</span>(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&</span>img) { <span class="keyword">return</span> img<span class="operator">.</span>scaledToWidth(<span class="number">100</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SmoothTransformation); }; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">QImage</span><span class="operator">></span> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> scale); </pre> </div> <!-- @@@qtconcurrentmap.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>