![]() |
Home · Examples |
The <QtConcurrentFilter> header provides concurrent Filter and Filter-Reduce. These functions are a part of the Qt Concurrent framework.
The QtConcurrent::filter(), QtConcurrent::filtered() and QtConcurrent::filteredReduced() functions filter items in a sequence such as a QList or a QVector 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. The filter function must be of the form: This example shows how to keep strings that are all lower-case from a QStringList:Concurrent Filter
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.
The following code example is written in c++.
bool function(const T &t);
T must match the type stored in the sequence. The function returns true if the item should be kept, false if it should be discarded.
The following code example is written in c++.
bool allLowerCase(const QString &string)
{
return string.lowered() == string;
}
QStringList strings = ...;
QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings, allLowerCase);
The results of the filter 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::filter():
The following code example is written in c++.
QStringList strings = ...; QFuture<void> future = QtConcurrent::filter(strings, allLowerCase);Since the sequence is modified in place, QtConcurrent::filter() does not return any results via QFuture. However, you can still use QFuture and QFutureWatcher to monitor the status of the filter.
The reduce function must be of the form:
The following code example is written in c++.
V function(T &result, const U &intermediate)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.
Call QtConcurrent::filteredReduced() like this:
The following code example is written in c++.
void addToDictionary(QSet<QString> &dictionary, const QString &string) { dictionary.insert(string); } QStringList strings = ...; QFuture<QSet<QString> > dictionary = QtConcurrent::filteredReduced(strings, allLowerCase, addToDictionary);The reduce function will be called once for each result kept by the filter function, and should merge the intermediate into the result 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 neccesary. The QtConcurrent::ReduceOptions enum provides a way to control the order in which the reduction is done.
QStringList strings = ...; QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase); // filter in-place only works on non-const iterators QFuture<void> future = QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase); QFuture<QSet<QString> > dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);
QStringList strings = ...; // each call blocks until the entire operation is finished QStringList lowerCaseStrings = QtConcurrent::blockingFiltered(strings, allLowerCase); QtConcurrent::blockingFilter(strings, allLowerCase); QSet<QString> dictionary = QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);Note that the result types above are not QFuture objects, but real result types (in this case, QStringList and QSet<QString>).
// keep only images with an alpha channel QList<QImage> images = ...; QFuture<void> alphaImages = QtConcurrent::filter(strings, &QImage::hasAlphaChannel); // keep only gray scale images QList<QImage> images = ...; QFuture<QImage> grayscaleImages = QtConcurrent::filtered(images, &QImage::isGrayscale); // create a set of all printable characters QList<QChar> characters = ...; QFuture<QSet<QChar> > set = QtConcurrent::filteredReduced(characters, &QChar::isPrint, &QSet<QChar>::insert);Note that when using QtConcurrent::filteredReduced(), you can mix the use of normal and member functions freely:
// can mix normal functions and member functions with QtConcurrent::filteredReduced() // create a dictionary of all lower cased strings extern bool allLowerCase(const QString &string); QStringList strings = ...; QFuture<QSet<int> > averageWordLength = QtConcurrent::filteredReduced(strings, allLowerCase, QSet<QString>::insert); // create a collage of all gray scale images extern void addToCollage(QImage &collage, const QImage &grayscaleImage); QList<QImage> images = ...; QFuture<QImage> collage = QtConcurrent::filteredReduced(images, &QImage::isGrayscale, addToCollage);
struct StartsWith { StartsWith(const QString &string) : m_string(string) { } typedef bool result_type; bool operator()(const QString &testString) { return testString.startsWith(m_string); } QString m_string; }; QList<QString> strings = ...; QFuture<QString> fooString = QtConcurrent::filtered(images, StartsWith(QLatin1String("Foo")));
If you want to use a filter function 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 use QString::contains():
The following code example is written in c++.
bool QString::contains(const QRegExp ®exp) const;QString::contains() 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 QString::contains() with QtConcurrent::filtered() we have to provide a value for the regexp argument:
boost::bind(&QString::contains, QRegExp("^\\S+$")); // matches strings without whitespaceThe return value from boost::bind() is a function object (functor) with the following signature:
bool contains(const QString &string)This matches what QtConcurrent::filtered() expects, and the complete example becomes:
QStringList strings = ...; boost::bind(static_cast<bool(QString::*)(const QRegExp&)>( &QString::contains ), QRegExp("..." ));
Copyright © 2008 Nokia | Trademarks | Qt Jambi 4.4.3_01 |