Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

  • Qt 4.8
  • QtCore
  • <QtConcurrentFilter> - Concurrent Filter and Filter-Reduce

<QtConcurrentFilter> - Concurrent Filter and Filter-Reduce

The <QtConcurrentFilter> header provides concurrent Filter and Filter-Reduce.More...

    Functions

    QFuture<void>filter(Sequence & sequence, FilterFunction filterFunction)
    QFuture<T>filtered(const Sequence & sequence, FilterFunction filterFunction)
    QFuture<T>filtered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
    QFuture<T>filteredReduced(const Sequence & sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
    QFuture<T>filteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)

    These functions are a part of theQt Concurrent framework.

    TheQtConcurrent::filter(),QtConcurrent::filtered() andQtConcurrent::filteredReduced() functions filter items in a sequence such as aQList or aQVector in parallel.QtConcurrent::filter() modifies a sequence in-place,QtConcurrent::filtered() returns a new sequence containing the filtered content, andQtConcurrent::filteredReduced() returns a single result.

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

    QStringList strings=...;// each call blocks until the entire operation is finishedQStringList lowerCaseStrings=QtConcurrent::blockingFiltered(strings, allLowerCase);QtConcurrent::blockingFilter(strings, allLowerCase);QSet<QString> dictionary=QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);

    Note that the result types above are notQFuture objects, but real result types (in this case,QStringList andQSet<QString>).

    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 filter function must be of the form:

    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.

    This example shows how to keep strings that are all lower-case from aQStringList:

    bool allLowerCase(constQString&string){return string.lowered()== string;}QStringList strings=...;QFuture<QString> lowerCaseStrings=QtConcurrent::filtered(strings, allLowerCase);

    The results of the filter 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::filter():

    QStringList strings=...;QFuture<void> future=QtConcurrent::filter(strings, allLowerCase);

    Since the sequence is modified in place,QtConcurrent::filter() does not return any results viaQFuture. However, you can still useQFuture andQFutureWatcher to monitor the status of the filter.

    Concurrent Filter-Reduce

    QtConcurrent::filteredReduced() is similar toQtConcurrent::filtered(), but instead of returing 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:

    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.

    CallQtConcurrent::filteredReduced() like this:

    void addToDictionary(QSet<QString>&dictionary,constQString&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 theintermediate into theresult 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. TheQtConcurrent::ReduceOptions enum provides a way to control the order in which the reduction is done.

    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:

    QStringList strings=...;QFuture<QString> lowerCaseStrings=QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase);// filter in-place only works on non-const iteratorsQFuture<void> future=QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase);QFuture<QSet<QString>> dictionary=QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);

    Using Member Functions

    QtConcurrent::filter(),QtConcurrent::filtered(), andQtConcurrent::filteredReduced() accept pointers to member functions. The member function class type must match the type stored in the sequence:

    // keep only images with an alpha channelQList<QImage> images=...;QFuture<void> alphaImages=QtConcurrent::filter(strings,&QImage::hasAlphaChannel);// keep only gray scale imagesQList<QImage> images=...;QFuture<QImage> grayscaleImages=QtConcurrent::filtered(images,&QImage::isGrayscale);// create a set of all printable charactersQList<QChar> characters=...;QFuture<QSet<QChar>> set=QtConcurrent::filteredReduced(characters,&QChar::isPrint,&QSet<QChar>::insert);

    Note that when usingQtConcurrent::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 stringsextern bool allLowerCase(constQString&string);QStringList strings=...;QFuture<QSet<int>> averageWordLength=QtConcurrent::filteredReduced(strings, allLowerCase,QSet<QString>::insert);// create a collage of all gray scale imagesexternvoid addToCollage(QImage&collage,constQImage&grayscaleImage);QList<QImage> images=...;QFuture<QImage> collage=QtConcurrent::filteredReduced(images,&QImage::isGrayscale, addToCollage);

    Using Function Objects

    QtConcurrent::filter(),QtConcurrent::filtered(), andQtConcurrent::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:

    struct StartsWith{    StartsWith(constQString&string)    : m_string(string) { }typedef bool result_type;    booloperator()(constQString&testString)    {return testString.startsWith(m_string);    }QString m_string;};QList<QString> strings=...;QFuture<QString> fooString=QtConcurrent::filtered(images, StartsWith(QLatin1String("Foo")));

    Using Bound Function Arguments

    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 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 useQString::contains():

    boolQString::contains(constQRegExp&regexp)const;

    QString::contains() takes 2 arguments (including the "this" pointer) and can't be used withQtConcurrent::filtered() directly, becauseQtConcurrent::filtered() expects a function that takes one argument. To useQString::contains() withQtConcurrent::filtered() we have to provide a value for theregexp argument:

    boost::bind(&QString::contains,QRegExp("^\\S+$"));// matches strings without whitespace

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

    bool contains(constQString&string)

    This matches whatQtConcurrent::filtered() expects, and the complete example becomes:

    QStringList strings=...;boost::bind(static_cast<bool(QString::*)(constQRegExp&)>(&QString::contains ),QRegExp("..." ));

    Function Documentation

    QFuture<void> QtConcurrent::filter(Sequence & sequence,FilterFunction filterFunction)

    CallsfilterFunction once for each item insequence. IffilterFunction returns true, the item is kept insequence; otherwise, the item is removed fromsequence.

    QFuture<T> QtConcurrent::filtered(constSequence & sequence,FilterFunction filterFunction)

    CallsfilterFunction once for each item insequence and returns a new Sequence of kept items. IffilterFunction returns true, a copy of the item is put in the new Sequence. Otherwise, the item willnot appear in the new Sequence.

    QFuture<T> QtConcurrent::filtered(ConstIterator begin,ConstIterator end,FilterFunction filterFunction)

    CallsfilterFunction once for each item frombegin toend and returns a new Sequence of kept items. IffilterFunction returns true, a copy of the item is put in the new Sequence. Otherwise, the item willnot appear in the new Sequence.

    QFuture<T> QtConcurrent::filteredReduced(constSequence & sequence,FilterFunction filterFunction,ReduceFunction reduceFunction,QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)

    CallsfilterFunction once for each item insequence. IffilterFunction returns true for an item, that item is then passed toreduceFunction. In other words, the return value is the result ofreduceFunction for each item wherefilterFunction returns true.

    Note that whilefilterFunction is called concurrently, only one thread at a time will callreduceFunction. The order in whichreduceFunction is called is undefined ifreduceOptions isQtConcurrent::UnorderedReduce. IfreduceOptions isQtConcurrent::OrderedReduce,reduceFunction is called in the order of the original sequence.

    QFuture<T> QtConcurrent::filteredReduced(ConstIterator begin,ConstIterator end,FilterFunction filterFunction,ReduceFunction reduceFunction,QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)

    CallsfilterFunction once for each item frombegin toend. IffilterFunction returns true for an item, that item is then passed toreduceFunction. In other words, the return value is the result ofreduceFunction for each item wherefilterFunction returns true.

    Note that whilefilterFunction is called concurrently, only one thread at a time will callreduceFunction. The order in whichreduceFunction is called is undefined ifreduceOptions isQtConcurrent::UnorderedReduce. IfreduceOptions isQtConcurrent::OrderedReduce, thereduceFunction is called in the order of the original sequence.

    © 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.


    [8]ページ先頭

    ©2009-2025 Movatter.jp