gpt4 book ai didi

c++ - 为 mappedReduce 指定 reduce 仿函数

转载 作者:行者123 更新时间:2023-11-30 04:17:35 24 4
gpt4 key购买 nike

我正在使用 QtConcurrent 并行运行某个任务。但是,当我决定改为使用 mappedReduced 而不是 map 时,我遇到了问题。

这是我使用的代码:

struct ProcMapWrapper {
ConcurrentProcessTask *instance;
ProcMapWrapper(ConcurrentProcessTask *w): instance(w) {}
QString & operator()(const QString& data) {
return instance->map(data);
}
ProcMapWrapper(const ProcMapWrapper & src)
{
instance = src.instance;
}
typedef QString result_type;
};

struct ProcReduceWrapper {
ConcurrentProcessTask *instance;
ProcReduceWrapper(ConcurrentProcessTask *w): instance(w) {}
void operator()(int &number, const QString &fname) {
return instance->reduce(number,fname);
}
ProcReduceWrapper(const ProcReduceWrapper & src)
{
instance = src.instance;
}
typedef int result_type;
};

ProcMapWrapper mw(this);
ProcReduceWrapper rw(this);
futureWatcher.setFuture(QtConcurrent::mappedReduced(_files.begin(),_files.end(),mw,rw));

想法是使用 processOneItem 函数从输入字符串生成 stage1Data 结构,然后在 reduce 函数中清除并将结果保存在适当的容器中。

我做了一些研究,使我找到了 this source file , 指的是 these code snippets .在那里,映射仿函数的模板如下:

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

这确实适用于 QtConcurrent 的 mapmapped 函数,但是我尝试使用相同的模式制作 reduce 仿函数再次导致这些错误:

..\qt_photoaud\task.cpp(230) : error C2893: Failed to specialize function template 'QFuture::ResultType> QtConcurrent::mappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' With the following template arguments: 'QList::iterator' with [ T=QString ] 'ProcMapWrapper' 'ProcReduceWrapper' ..\qt_photoaud\task.cpp(230) : error C2783: 'QFuture QtConcurrent::mappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType' c:\qt\4.8.4\include\qtcore../../src/corelib/concurrent/qtconcurrentmap.h(152) : see declaration of 'QtConcurrent::mappedReduced' ..\qt_photoaud\task.cpp(230) : error C2893: Failed to specialize function template 'QFuture::ResultType> QtConcurrent::mappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' With the following template arguments: 'QList::iterator' with [ T=QString ] 'QList::iterator' with [ T=QString ] 'ProcMapWrapper' ..\qt_photoaud\task.cpp(230) : error C2783: 'QFuture QtConcurrent::mappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType' c:\qt\4.8.4\include\qtcore../../src/corelib/concurrent/qtconcurrentmap.h(125) : see declaration of 'QtConcurrent::mappedReduced'

reduce functor 真的被定义和/或识别了吗? QtPrivate::ReduceResultType::ResultType 是如何工作的?它需要什么?

编辑:假设仿函数与 boost::function 兼容,我尝试在 reduce 仿函数中指定 first_argument_type、second_argument_type、arg1_type 和 arg2_type。但这没有任何帮助。

最佳答案

这是对我有用的:

struct concurrentProcessDBMapper
{
typedef stage1Data result_type;
stage1Data operator()(QString fname)
{
return concurrentProcessDBMap(fname);
}
};

struct concurrentProcessDBReducer
{
DatabaseConcurrentProcessTask *instance;
concurrentProcessDBReducer(DatabaseConcurrentProcessTask *i)
{
instance = i;
}

void operator()(int & number, const stage1Data in)
{
//concurrentProcessDBReduce
instance->reduce(number,in);
}
};
...
QFutureWatcher<int> futureWatcher;

QList<QString> _filesInWork;

...
futureWatcher.setFuture(
QtConcurrent::mappedReduced
<int, QList<QString>, concurrentProcessDBMapper,concurrentProcessDBReducer>
(_filesInWork,concurrentProcessDBMapper(),concurrentProcessDBReducer(this)));
...

总而言之,随着类型转换像 -(map)->-(reduce)-> 一样,map 仿函数将 result_type 定义为 stage1Data,而 reduce 仿函数根本没有定义任何 result_type。 Future watcher 将其类型定义为 reduce 结果(即,其第一个输出参数的类型)——在我的例子中是 int——mappedReduced 的模板参数是明确定义的。

关于c++ - 为 mappedReduce 指定 reduce 仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com