forked from qt-creator/qt-creator
mapReduce: Support progress information and add (unordered) map
If a container is given to mapReduce, it takes the responsibility to report progress information for the whole operation. If the map function reports its own progress, that is taken into account for the overall progress. The (so far only unordered) Utils::map operation can be used to replace MultiTask, by passing a member function of the items in the container as a map function. Change-Id: I18ca38a6ad2899d73f590bfe59bf2e6eb2f1a57a Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
@@ -34,6 +34,8 @@ class tst_MapReduce : public QObject
|
||||
|
||||
private slots:
|
||||
void mapReduce();
|
||||
void mapReduceRvalueContainer();
|
||||
void map();
|
||||
};
|
||||
|
||||
static int returnxx(int x)
|
||||
@@ -92,6 +94,43 @@ void tst_MapReduce::mapReduce()
|
||||
}
|
||||
}
|
||||
|
||||
void tst_MapReduce::mapReduceRvalueContainer()
|
||||
{
|
||||
{
|
||||
QFuture<int> future = Utils::mapReduce(QList<int>({1, 2, 3, 4, 5}),
|
||||
[](QFutureInterface<int>&) { return 0; },
|
||||
[](int value) { return value; },
|
||||
[](QFutureInterface<int>&, int &state, int value) { state += value; },
|
||||
[](QFutureInterface<int> &fi, int &state) { fi.reportResult(state); });
|
||||
// here, lifetime of the QList temporary ends
|
||||
QCOMPARE(future.results(), QList<int>({15}));
|
||||
}
|
||||
}
|
||||
|
||||
void tst_MapReduce::map()
|
||||
{
|
||||
{
|
||||
QList<double> results = Utils::map(QList<int>({2, 5, 1}),
|
||||
[](int x) { return x*2.5; }
|
||||
).results();
|
||||
Utils::sort(results);
|
||||
QCOMPARE(results, QList<double>({2.5, 5., 12.5}));
|
||||
}
|
||||
{
|
||||
// void result
|
||||
QList<int> results;
|
||||
QMutex mutex;
|
||||
Utils::map(
|
||||
// container
|
||||
QList<int>({2, 5, 1}),
|
||||
// map
|
||||
[&mutex, &results](int x) { QMutexLocker l(&mutex); results.append(x); }
|
||||
).waitForFinished();
|
||||
Utils::sort(results); // mapping order is undefined
|
||||
QCOMPARE(results, QList<int>({1, 2, 5}));
|
||||
}
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_MapReduce)
|
||||
|
||||
#include "tst_mapreduce.moc"
|
||||
|
||||
Reference in New Issue
Block a user