Utils: Extend transform for container insertion

You can now return a container and it will be appended to the result
container. That has the drawback of generating temporary container but
if we get them already it makes the code much more readable than a raw
loop.

Change-Id: Ibcd38e85ef759c18cd8da0fac0185f0f6fc123e2
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Marco Bubke
2023-09-28 15:05:25 +02:00
parent 7ae61da78d
commit a4a8fd5b81
2 changed files with 102 additions and 6 deletions

View File

@@ -402,6 +402,24 @@ void tst_Algorithm::transform()
const QHash<int, int> expected({{1, 2}, {2, 3}, {3, 4}, {4, 5}});
QCOMPARE(trans, expected);
}
{
// std::vector -> std::vector appending container
const std::vector<int> v({1, 2, 3, 4});
const auto trans = Utils::transform<std::vector<int>>(v, [](int i) -> std::vector<int> {
return {i, i * 2};
});
const std::vector<int> expected{1, 2, 2, 4, 3, 6, 4, 8};
QCOMPARE(trans, expected);
}
{
// QList -> QList appending container
const QList<int> v({1, 2, 3, 4});
const auto trans = Utils::transform<QList<int>>(v, [](int i) -> QList<int> {
return {i, i * 2};
});
const QList<int> expected{1, 2, 2, 4, 3, 6, 4, 8};
QCOMPARE(trans, expected);
}
}
void tst_Algorithm::sort()