
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
The <QtConcurrentMap> header provides concurrent Map and MapReduce.More...
| T | blockingMappedReduced(const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce) |
| T | blockingMappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce) |
| QFuture<void> | map(Sequence & sequence, MapFunction function) |
| QFuture<void> | map(Iterator begin, Iterator end, MapFunction function) |
| QFuture<T> | mapped(const Sequence & sequence, MapFunction function) |
| QFuture<T> | mapped(ConstIterator begin, ConstIterator end, MapFunction function) |
| QFuture<T> | mappedReduced(const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce) |
| QFuture<T> | mappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce) |
These functions are a part of theQt Concurrent framework.
TheQtConcurrent::map(),QtConcurrent::mapped() andQtConcurrent::mappedReduced() functions run computations in parallel on the items in a sequence such as aQList or aQVector.QtConcurrent::map() modifies a sequence in-place,QtConcurrent::mapped() returns a new sequence containing the modified content, andQtConcurrent::mappedReduced() returns a single result.
Each of the above functions has a blocking variant that returns the final result instead of aQFuture. You use them in the same way as the asynchronous variants.
QList<QImage> images=...;// each call blocks until the entire operation is finishedQList<QImage> future=QtConcurrent::blockingMapped(images, scaled);QtConcurrent::blockingMap(images, scale);QImage collage=QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);
Note that the result types above are notQFuture objects, but real result types (in this case,QList<QImage> andQImage).
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.
The map function must be of the form:
U function(const T&t);
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 ormapped content.
This example shows how to apply a scale function to all the items in a sequence:
QImage scaled(constQImage&image){return image.scaled(100,100);}QList<QImage> images=...;QFuture<QImage> thumbnails=QtConcurrent::mapped(images, scaled);
The results of the map are made available throughQFuture. See theQFuture andQFutureWatcher documentation for more information on how to useQFuture in your applications.
If you want to modify a sequence in-place, useQtConcurrent::map(). The map function must then be of the form:
U function(T&t);Note that the return value and return type of the map function are not used.
UsingQtConcurrent::map() is similar to usingQtConcurrent::mapped():
void scale(QImage&image){ image= image.scaled(100,100);}QList<QImage> images=...;QFuture<void> future=QtConcurrent::map(images, scale);
Since the sequence is modified in place,QtConcurrent::map() does not return any results viaQFuture. However, you can still useQFuture andQFutureWatcher to monitor the status of the map.
QtConcurrent::mappedReduced() is similar toQtConcurrent::mapped(), but instead of returning a sequence with the new results, the results are combined into a single value using a reduce function.
The reduce function must be of the form:
V function(T&result,const U&intermediate)
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.
CallQtConcurrent::mappedReduced() like this:
void addToCollage(QImage&collage,constQImage&thumbnail){QPainter p(&collage);staticQPoint offset=QPoint(0,0); p.drawImage(offset, thumbnail); offset+=...;}QList<QImage> images=...;QFuture<QImage> collage=QtConcurrent::mappedReduced(images, scaled, addToCollage);
The reduce function will be called once for each result returned by the map function, and should merge theintermediate into theresult 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. TheQtConcurrent::ReduceOptions enum provides a way to control the order in which the reduction is done. IfQtConcurrent::UnorderedReduce is used (the default), the order is undefined, whileQtConcurrent::OrderedReduce ensures that the reduction is done in the order of the original sequence.
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:
QList<QImage> images=...;QFuture<QImage> thumbnails=QtConcurrent::mapped(images.constBegin(), images.constEnd(), scaled);// map in-place only works on non-const iteratorsQFuture<void> future=QtConcurrent::map(images.begin(), images.end(), scale);QFuture<QImage> collage=QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage);
Each of the above functions has a blocking variant that returns the final result instead of aQFuture. You use them in the same way as the asynchronous variants.
QList<QImage> images=...;// each call blocks until the entire operation is finishedQList<QImage> future=QtConcurrent::blockingMapped(images, scaled);QtConcurrent::blockingMap(images, scale);QImage collage=QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);
Note that the result types above are notQFuture objects, but real result types (in this case,QList<QImage> andQImage).
QtConcurrent::map(),QtConcurrent::mapped(), andQtConcurrent::mappedReduced() accept pointers to member functions. The member function class type must match the type stored in the sequence:
// squeeze all strings in a QStringListQStringList strings=...;QFuture<void> squeezedStrings=QtConcurrent::map(strings,&QString::squeeze);// swap the rgb values of all pixels on a list of imagesQList<QImage> images=...;QFuture<QImage> bgrImages=QtConcurrent::mapped(images,&QImage::rgbSwapped);// create a set of the lengths of all strings in a listQStringList strings=...;QFuture<QSet<int>> wordLengths=QtConcurrent::mappedReduced(string,&QString::length,&QSet<int>::insert);
Note that when usingQtConcurrent::mappedReduced(), you can mix the use of normal and member functions freely:
// can mix normal functions and member functions with QtConcurrent::mappedReduced()// compute the average length of a list of stringsexternvoid computeAverage(int&average,int length);QStringList strings=...;QFuture<int> averageWordLength=QtConcurrent::mappedReduced(strings,&QString::length, computeAverage);// create a set of the color distribution of all images in a listexternint colorDistribution(constQImage&string);QList<QImage> images=...;QFuture<QSet<int>> totalColorDistribution=QtConcurrent::mappedReduced(images, colorDistribution,QSet<int>::insert);
QtConcurrent::map(),QtConcurrent::mapped(), andQtConcurrent::mappedReduced() accept function objects, which can be used to add state to a function call. The result_type typedef must define the result type of the function call operator:
struct Scaled{ Scaled(int size) : m_size(size) { }typedefQImage result_type;QImageoperator()(constQImage&image) {return image.scaled(m_size, m_size); }int m_size;};QList<QImage> images=...;QFuture<QImage> thumbnails=QtConcurrent::mapped(images, Scaled(100));
Note that Qt does not provide support for bound functions. This is provided by 3rd party libraries likeBoost orC++ TR1 Library Extensions.
If you want to use a map function that takes more than one argument you can use boost::bind() or std::tr1::bind() to transform it onto a function that takes one argument.
As an example, we'll useQImage::scaledToWidth():
scaledToWidth takes three arguments (including the "this" pointer) and can't be used withQtConcurrent::mapped() directly, becauseQtConcurrent::mapped() expects a function that takes one argument. To useQImage::scaledToWidth() withQtConcurrent::mapped() we have to provide a value for thewidth and thetransformation mode:
The return value from boost::bind() is a function object (functor) with the following signature:
This matches whatQtConcurrent::mapped() expects, and the complete example becomes:
CallsmapFunction once for each item insequence. The return value of eachmapFunction is passed toreduceFunction.
Note that whilemapFunction is called concurrently, only one thread at a time will callreduceFunction. The order in whichreduceFunction is called is determined byreduceOptions.
Note:This function will block until all items in the sequence have been processed.
See alsomapped().
CallsmapFunction once for each item frombegin toend. The return value of eachmapFunction is passed toreduceFunction.
Note that whilemapFunction is called concurrently, only one thread at a time will callreduceFunction. The order in whichreduceFunction is called is undefined.
Note:This function will block until the iterator reaches the end of the sequence being processed.
See alsoblockingMappedReduced().
Callsfunction once for each item insequence. Thefunction is passed a reference to the item, so that any modifications done to the item will appear insequence.
Callsfunction once for each item frombegin toend. Thefunction is passed a reference to the item, so that any modifications done to the item will appear in the sequence which the iterators belong to.
Callsfunction once for each item insequence and returns a future with each mapped item as a result. You can useQFuture::const_iterator orQFutureIterator to iterate through the results.
Callsfunction once for each item frombegin toend and returns a future with each mapped item as a result. You can useQFuture::const_iterator orQFutureIterator to iterate through the results.
CallsmapFunction once for each item insequence. The return value of eachmapFunction is passed toreduceFunction.
Note that whilemapFunction is called concurrently, only one thread at a time will callreduceFunction. The order in whichreduceFunction is called is determined byreduceOptions.
CallsmapFunction once for each item frombegin toend. The return value of eachmapFunction is passed toreduceFunction.
Note that whilemapFunction is called concurrently, only one thread at a time will callreduceFunction. By default, the order in whichreduceFunction is called is undefined.
Note:QtConcurrent::OrderedReduce results in the ordered reduction.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. 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.