Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

<QtConcurrentMap> - Concurrent Map and Map-Reduce

The <QtConcurrentMap> header provides concurrent Map and MapReduce. More...

Functions

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 the Qt Concurrent framework.

The QtConcurrent::map(), QtConcurrent::mapped() and QtConcurrent::mappedReduced() functions run computations in parallel on the items in a sequence such as a QList or a QVector. QtConcurrent::map() modifies a sequence in-place, QtConcurrent::mapped() returns a new sequence containing the modified content, and QtConcurrent::mappedReduced() returns a single result.

Each of the above functions has a blocking variant that returns the final result instead of a QFuture. You use them in the same way as the asynchronous variants.

 QList<QImage> images = ...;

 // each call blocks until the entire operation is finished
 QList<QImage> future = QtConcurrent::blockingMapped(images, scaled);

 QtConcurrent::blockingMap(images, scale);

 QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);

Note that the result types above are not QFuture objects, but real result types (in this case, QList<QImage> and QImage).

Concurrent Map

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 or mapped content.

This example shows how to apply a scale function to all the items in a sequence:

 QImage scaled(const QImage &image)
 {
     return image.scaled(100, 100);
 }

 QList<QImage> images = ...;
 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scaled);

The results of the map are made available through QFuture. See the QFuture and QFutureWatcher documentation for more information on how to use QFuture in your applications.

If you want to modify a sequence in-place, use QtConcurrent::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.

Using QtConcurrent::map() is similar to using QtConcurrent::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 via QFuture. However, you can still use QFuture and QFutureWatcher to monitor the status of the map.

Concurrent Map-Reduce

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.

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.

Call QtConcurrent::mappedReduced() like this:

 void addToCollage(QImage &collage, const QImage &thumbnail)
 {
     QPainter p(&collage);
     static QPoint 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 the intermediate into the result 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 neccesary. The QtConcurrent::ReduceOptions enum provides a way to control the order in which the reduction is done. If QtConcurrent::UnorderedReduce is used (the default), the order is undefined, while QtConcurrent::OrderedReduce ensures that the reduction is done in the order of the original sequence.

Additional API Features

Using Iterators instead of 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 iterators
 QFuture<void> future = QtConcurrent::map(images.begin(), images.end(), scale);

 QFuture<QImage> collage = QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage);

Blocking Variants

Each of the above functions has a blocking variant that returns the final result instead of a QFuture. You use them in the same way as the asynchronous variants.

 QList<QImage> images = ...;

 // each call blocks until the entire operation is finished
 QList<QImage> future = QtConcurrent::blockingMapped(images, scaled);

 QtConcurrent::blockingMap(images, scale);

 QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);

Note that the result types above are not QFuture objects, but real result types (in this case, QList<QImage> and QImage).

Using Member Functions

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:

 // squeeze all strings in a QStringList
 QStringList strings = ...;
 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);

 // create a set of the lengths of all strings in a list
 QStringList strings = ...;
 QFuture<QSet<int> > wordLengths = QtConcurrent::mappedReduced(string, &QString::length, &QSet<int>::insert);

Note that when using QtConcurrent::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 strings
 extern void 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 list
 extern int colorDistribution(const QImage &string);
 QList<QImage> images = ...;
 QFuture<QSet<int> > totalColorDistribution = QtConcurrent::mappedReduced(images, colorDistribution, QSet<int>::insert);

Using Function Objects

QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::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) { }

     typedef QImage result_type;

     QImage operator()(const QImage &image)
     {
         return image.scaled(m_size, m_size);
     }

     int m_size;
 };

 QList<QImage> images = ...;
 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));

Using Bound Function Arguments

Note that Qt does not provide support for bound functions. This is provided by 3rd party libraries like Boost or C++ 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 use QImage::scaledToWidth():

 QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const;

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 width and the transformation mode:

 boost::bind(&QImage::scaledToWidth, 100, Qt::SmoothTransformation)

The return value from boost::bind() is a function object (functor) with the following signature:

 QImage scaledToWith(const QImage &image)

This matches what QtConcurrent::mapped() expects, and the complete example becomes:

 QList<QImage> images = ...;
 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, boost::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation));

Function Documentation

T QtConcurrent::blockingMappedReduced ( const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )

Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.

Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.

Note: This function will block until all items in the sequence have been processed.

See also mapped().

T QtConcurrent::blockingMappedReduced ( ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )

Calls mapFunction once for each item from begin to end. The return value of each mapFunction is passed to reduceFunction.

Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is undefined.

Note: This function will block until the iterator reaches the end of the sequence being processed.

See also blockingMappedReduced().

QFuture<void> QtConcurrent::map ( Sequence & sequence, MapFunction function )

Calls function once for each item in sequence. The function is passed a reference to the item, so that any modifications done to the item will appear in sequence.

QFuture<void> QtConcurrent::map ( Iterator begin, Iterator end, MapFunction function )

Calls function once for each item from begin to end. The function 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.

QFuture<T> QtConcurrent::mapped ( const Sequence & sequence, MapFunction function )

Calls function once for each item in sequence and returns a future with each mapped item as a result. You can use QFuture::const_iterator or QFutureIterator to iterate through the results.

QFuture<T> QtConcurrent::mapped ( ConstIterator begin, ConstIterator end, MapFunction function )

Calls function once for each item from begin to end and returns a future with each mapped item as a result. You can use QFuture::const_iterator or QFutureIterator to iterate through the results.

QFuture<T> QtConcurrent::mappedReduced ( const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )

Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.

Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.

QFuture<T> QtConcurrent::mappedReduced ( ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )

Calls mapFunction once for each item from begin to end. The return value of each mapFunction is passed to reduceFunction.

Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. By default, the order in which reduceFunction is called is undefined.

Note: QtConcurrent::OrderedReduce results in the ordered reduction.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 82
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Labs au hasard

Logo

Utiliser OpenCL avec Qt

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web