Qt之Concurrent Filter和Filter-Reduce

简述

QtConcurrent::filter()、QtConcurrent::filtered() 和 QtConcurrent::filteredReduced() 函数对一个序列(例如:QList、QVector )中的项目并行地进行过滤。QtConcurrent::filter() 就地修改一个序列,QtConcurrent::filtered() 返回一个包含过滤内容的新序列,QtConcurrent::filteredReduced() 返回一个单一的结果。

这些函数是 Qt之Concurrent框架 的一部分。

上述每个函数都有一个 blocking 变量,其返回的是最终结果,而不是一个 QFuture。可以用和异步变量同样的方式来使用它们。

QStringList strings = ...;

// 每次调用都会被阻塞,直到整个操作完成
QStringList lowerCaseStrings = QtConcurrent::blockingFiltered(strings, allLowerCase);

QtConcurrent::blockingFilter(strings, allLowerCase);

QSet<QString> dictionary = QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);

注意:上述的结果类型不是 QFuture 对象,而是实际结果类型(在这种情况下,是 QStringList 和 QSet<QString>)。

Concurrent Filter

QtConcurrent::filtered() 接受一个输入序列和一个 filter 函数,该 filter 函数被序列中的每一项调用,并返回一个包含过滤值的新序列。

该 filter 函数必须是以下形式:

bool function(const T &t);

T 必须匹配存储在序列中的类型。如果项目应该保留,函数返回 true;如果应该丢弃,则返回 false。

下面示例,介绍了如何保留 QStringList 中的小写字符串:

bool allLowerCase(const QString &string)
{
    return string.lowered() == string;
}

QStringList strings = ...;
QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings, allLowerCase);

filter 的结果通过 QFuture 提供。有关如何在应用程序中使用 QFuture 的更多信息,请参阅 QFuture 和 QFutureWatcher 文档。

如果想就地修改一个序列,使用 QtConcurrent::filter():

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

由于该序列被就地修改,QtConcurrent::filter() 不通过 QFuture 返回任何结果。然而,仍然可以使用 QFuture 和 QFutureWatcher 监控 filter 的状态。

Concurrent Filter-Reduce

QtConcurrent::filteredReduced() 类似于 QtConcurrent::filtered(),但是不是使用过滤的结果来返回一个序列,而是使用 reduce 函数将结果组合成一个单一值。

reduce 函数必须是以下形式:

V function(T &result, const U &intermediate)

T 是最终结果的类型,U 是要过滤项的类型。注意: reduce 函数的返回值、返回类型没有被使用。

调用 QtConcurrent::filteredReduced() 如下所示:

void addToDictionary(QSet<QString> &dictionary, const QString &string)
{
    dictionary.insert(string);
}

QStringList strings = ...;
QFuture<QSet<QString> > dictionary = QtConcurrent::filteredReduced(strings, allLowerCase, addToDictionary);

对于由过滤器函数保存的每个结果,reduce 函数将被调用一次,并且应该将中间体合并到结果变量中。QtConcurrent::filteredReduced() 保证一次只有一个线程调用 reduce,所以没有必要用一个 mutex 锁定结果变量。QtConcurrent::ReduceOptions 枚举提供了一种方法来控制 reduction 完成的顺序。

附加 API 功能

使用迭代器而不是序列

上述每个函数都有一个变量,需要一个迭代器范围,而不是一个序列。可以用和序列变量同样的方式来使用它们。

QStringList strings = ...;
QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase);

// filter 仅就地运行在 non-const 迭代器上
QFuture<void> future = QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase);

QFuture<QSet<QString> > dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);

使用成员函数

QtConcurrent::filter()、QtConcurrent::filtered() 和 QtConcurrent::filteredReduced() 接受成员函数的指针。成员函数类类型必须与存储在序列中的类型匹配:

// 仅保留带 alpha 通道的图像
QList<QImage> images = ...;
QFuture<void> alphaImages = QtConcurrent::filter(strings, &QImage::hasAlphaChannel);

// 仅保留灰度图像
QList<QImage> images = ...;
QFuture<QImage> grayscaleImages = QtConcurrent::filtered(images, &QImage::isGrayscale);

// 创建一个可打印字符的集合  
QList<QChar> characters = ...;
QFuture<QSet<QChar> > set = QtConcurrent::filteredReduced(characters, &QChar::isPrint, &QSet<QChar>::insert);

注意:当使用 QtConcurrent::filteredReduced() 时,可以自由地混合使用正常函数和成员函数:

// 可以使用 QtConcurrent::filteredReduced() 混合正常函数和成员函数

// 创建一个所有小写格式字符串的字典
extern bool allLowerCase(const QString &string);
QStringList strings = ...;
QFuture<QSet<int> > averageWordLength = QtConcurrent::filteredReduced(strings, allLowerCase, QSet<QString>::insert);

// 创建一个所有灰度图像的拼贴
extern void addToCollage(QImage &collage, const QImage &grayscaleImage);
QList<QImage> images = ...;
QFuture<QImage> collage = QtConcurrent::filteredReduced(images, &QImage::isGrayscale, addToCollage);

使用函数对象

QtConcurrent::filter()、QtConcurrent::filtered() 和 QtConcurrent::filteredReduced() 接受函数对象,可用于向函数调用添加状态。result_type typedef 必须定义结果函数调用操作符的类型:

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")));

使用绑定函数参数

如果想使用一个接受多个参数的 filter 函数,可以使用 std::bind() 来将其转换到接受一个参数的函数。如果 C++11 支持不可用,boost::bind()std::tr1::bind() 是合适的替代品。

例如,使用 QString::contains():

bool QString::contains(const QRegExp &regexp) const;

QString::contains() 接受 2 个参数(包括“this”指针),不能直接使用 QtConcurrent::filtered(),因为 QtConcurrent::filtered() 期望一个接受一个参数的函数。为了与 QtConcurrent::filtered() 结合使用 QString::contains(),必须为 regexp 参数提供一个值。

std::bind(&QString::contains, QRegExp("^\\S+$")); // 匹配没有空格的字符串

std::bind() 的返回值是一个具有以下签名的函数对象(functor):

bool contains(const QString &string)

这符合 QtConcurrent::filtered() 期望的,完整的示例变为:

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

更多参考

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In Verilog, a non-net refers to a variable that is not a wire or a register, but rather a constant or a parameter. Concurrent assignment refers to the assignment of a value to a variable using the "=" operator in a module outside of any procedural blocks (such as always or initial blocks). A concurrent assignment to a non-net is not allowed in Verilog. This is because a non-net does not have a storage element and cannot hold a value assigned to it. Instead, it is typically used as a constant or a parameter that is available for use in the module. To assign a value to a non-net, it should be done within a procedural block using the appropriate assignment operator (such as "<=" for registers or "assign" for wires). Alternatively, the value can be passed as an argument to the module using the parameter keyword. For example: module my_module #(parameter WIDTH = 8) ( input clk, input [WIDTH-1:0] data_in, output [WIDTH-1:0] data_out ); // This is a non-net parameter parameter ADD_VALUE = 5; // This is a register that can be assigned using the "=" operator within an always block reg [WIDTH-1:0] register_data; always @(posedge clk) begin register_data <= data_in + ADD_VALUE; end // This is a wire that can be assigned using the "assign" keyword assign data_out = register_data; endmodule In this example, ADD_VALUE is a non-net parameter that is used in the always block to add a constant value to the input data. The register_data variable is assigned using the "=" operator within the always block. The data_out wire is assigned using the "assign" keyword.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值