Utils::transform: Add support for various map types as result

Add support for output as std::map/unordered_map/set and QMap and QHash,
when giving the full result type as template argument. For std::
(unordered_)map, the function must return a std::pair<Key,Value>, for
QMap and QHash it can also be QPair<Key,Value>.

Change-Id: If3dff17ab6aa5d1b11abc244813fd885d10c75a4
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Eike Ziller
2017-12-17 15:41:56 +01:00
parent f62b24c475
commit 8944ba0391
2 changed files with 127 additions and 0 deletions

View File

@@ -30,6 +30,7 @@
#include <list>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
// must get included after the containers above or gcc4.9 will have a problem using
@@ -359,6 +360,70 @@ void tst_Algorithm::transform()
const std::set<int> trans = Utils::transform<std::set<int>>(v, [](int i) { return i + 1; });
QCOMPARE(trans, std::set<int>({2, 3, 4, 5}));
}
// various map/set/hash without push_back
{
// std::vector -> std::map
const std::vector<int> v({1, 2, 3, 4});
const std::map<int, int> trans = Utils::transform<std::map<int, int>>(v, [](int i) {
return std::make_pair(i, i + 1);
});
const std::map<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
{
// std::vector -> std::unordered_set
const std::vector<int> v({1, 2, 3, 4});
const std::unordered_set<int> trans = Utils::transform<std::unordered_set<int>>(v, [](int i) {
return i + 1;
});
QCOMPARE(trans, std::unordered_set<int>({2, 3, 4, 5}));
}
{
// std::vector -> std::unordered_map
const std::vector<int> v({1, 2, 3, 4});
const std::unordered_map<int, int> trans
= Utils::transform<std::unordered_map<int, int>>(v, [](int i) {
return std::make_pair(i, i + 1);
});
const std::unordered_map<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
{
// std::vector -> QMap using std::pair
const std::vector<int> v({1, 2, 3, 4});
const QMap<int, int> trans = Utils::transform<QMap<int, int>>(v, [](int i) {
return std::make_pair(i, i + 1);
});
const QMap<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
{
// std::vector -> QMap using QPair
const std::vector<int> v({1, 2, 3, 4});
const QMap<int, int> trans = Utils::transform<QMap<int, int>>(v, [](int i) {
return qMakePair(i, i + 1);
});
const QMap<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
{
// std::vector -> QHash using std::pair
const std::vector<int> v({1, 2, 3, 4});
const QHash<int, int> trans = Utils::transform<QHash<int, int>>(v, [](int i) {
return std::make_pair(i, i + 1);
});
const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
{
// std::vector -> QHash using QPair
const std::vector<int> v({1, 2, 3, 4});
const QHash<int, int> trans = Utils::transform<QHash<int, int>>(v, [](int i) {
return qMakePair(i, i + 1);
});
const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
}
void tst_Algorithm::sort()