![]() |
CopperSpice API
1.8.2
|
The <QtConcurrentFilter> header provides Concurrent Filter and Filter Reduce.
These functions are provided to support Concurrent Programming.
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.
Each of the above functions have a blocking variant that returns the final result instead of a QFuture. You use them in the same way as the asynchronous variants.
The result types above are not QFuture objects but real result types. In this case, QStringList and QSet<QString>.
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 filter function must be of the form:
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.
This example shows how to keep strings that are all lower-case from a QStringList:
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():
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.
QtConcurrent::filteredReduced() is similar to QtConcurrent::filtered(), but instead of returning a sequence with the filtered results, the results are combined into a single value using a reduce function.
The reduce function must be of the form:
T is the type of the final result, U is the type of items being filtered. The return value and return type of the reduce function are not used. Call QtConcurrent::filteredReduced() as shown below.
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 necessary. The QtConcurrent::ReduceOptions enum provides a way to control the order in which the reduction is done.
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:
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:
When using QtConcurrent::filteredReduced() you can mix the use of normal and member functions.
QtConcurrent::filter(), QtConcurrent::filtered(), and QtConcurrent::filteredReduced() 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:
If you want to use a filter function which takes more than one argument use a lambda.
As an example, we will use QString::contains(). Since QString::contains() is a method it can not be used with QtConcurrent::filtered() directly. This is because QtConcurrent::filtered() expects a function which takes one argument.