Commit c90e5b50 authored by Eike Ziller's avatar Eike Ziller Committed by Tobias Hunger

Algorithm: Support std::vector<std::unique_ptr> with contains and findOr

Change-Id: I01cb71c06b405f071498ed8752f9acac44d2b223
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent e2baa116
......@@ -108,12 +108,33 @@ typename T::value_type findOr(const T &container, typename T::value_type other,
return *it;
}
// container of unique_ptr (with element_type and get()),
// and with two template arguments like std::vector
template<template<typename, typename> class C, typename AllocC, typename T, typename F>
typename T::element_type *findOr(const C<T, AllocC> &container, typename T::element_type *other, F function)
{
typename C<T, AllocC>::const_iterator begin = std::begin(container);
typename C<T, AllocC>::const_iterator end = std::end(container);
typename C<T, AllocC>::const_iterator it = std::find_if(begin, end, function);
if (it == end)
return other;
return it->get();
}
template<typename T, typename R, typename S>
typename T::value_type findOr(const T &container, typename T::value_type other, R (S::*function)() const)
{
return findOr(container, other, std::mem_fn(function));
}
// container of unique_ptr (with element_type and get()),
// and with two template arguments like std::vector
template<template<typename, typename> class C, typename AllocC, typename T, typename R, typename S>
typename T::element_type *findOr(const C<T, AllocC> &container, typename T::element_type *other, R (S::*function)() const)
{
return findOr(container, other, std::mem_fn(function));
}
template<typename T, typename F>
int indexOf(const T &container, F function)
......@@ -139,6 +160,23 @@ typename T::value_type findOrDefault(const T &container, R (S::*function)() cons
return findOr(container, typename T::value_type(), function);
}
// container of unique_ptr (with element_type and get()),
// and with two template arguments like std::vector
template<template<typename, typename> class C, typename AllocC, typename T, typename F>
typename T::element_type *findOrDefault(const C<T, AllocC> &container, F function)
{
return findOr(container, nullptr, function);
}
// container of unique_ptr (with element_type and get()),
// and with two template arguments like std::vector
template<template<typename, typename> class C, typename AllocC, typename T, typename R, typename S>
typename T::element_type *findOrDefault(const C<T, AllocC> &container, R (S::*function)() const)
{
return findOr(container, nullptr, std::mem_fn(function));
}
//////////////////
// max element
//////////////////
......
......@@ -37,6 +37,9 @@ class tst_Algorithm : public QObject
private slots:
void transform();
void sort();
void contains();
void findOr();
void findOrDefault();
};
......@@ -50,6 +53,8 @@ struct Struct
{
Struct(int m) : member(m) {}
bool operator==(const Struct &other) const { return member == other.member; }
bool isOdd() const { return member % 2 == 1; }
bool isEven() const { return !isOdd(); }
int member;
};
......@@ -168,6 +173,64 @@ void tst_Algorithm::sort()
QCOMPARE(valarray[i], valarrayResult[i]);
}
void tst_Algorithm::contains()
{
std::vector<int> v1{1, 2, 3, 4};
QVERIFY(Utils::contains(v1, [](int i) { return i == 2; }));
QVERIFY(!Utils::contains(v1, [](int i) { return i == 5; }));
std::vector<std::unique_ptr<int>> v2;
v2.emplace_back(std::make_unique<int>(1));
v2.emplace_back(std::make_unique<int>(2));
v2.emplace_back(std::make_unique<int>(3));
v2.emplace_back(std::make_unique<int>(4));
QVERIFY(Utils::contains(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; }));
QVERIFY(!Utils::contains(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; }));
}
void tst_Algorithm::findOr()
{
std::vector<int> v1{1, 2, 3, 4};
QCOMPARE(Utils::findOr(v1, 6, [](int i) { return i == 2; }), 2);
QCOMPARE(Utils::findOr(v1, 6, [](int i) { return i == 5; }), 6);
std::vector<std::unique_ptr<int>> v2;
v2.emplace_back(std::make_unique<int>(1));
v2.emplace_back(std::make_unique<int>(2));
v2.emplace_back(std::make_unique<int>(3));
v2.emplace_back(std::make_unique<int>(4));
int def = 6;
QCOMPARE(Utils::findOr(v2, &def, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get());
QCOMPARE(Utils::findOr(v2, &def, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), &def);
std::vector<std::unique_ptr<Struct>> v3;
v3.emplace_back(std::make_unique<Struct>(1));
v3.emplace_back(std::make_unique<Struct>(3));
v3.emplace_back(std::make_unique<Struct>(5));
v3.emplace_back(std::make_unique<Struct>(7));
Struct defS(6);
QCOMPARE(Utils::findOr(v3, &defS, &Struct::isOdd), v3.at(0).get());
QCOMPARE(Utils::findOr(v3, &defS, &Struct::isEven), &defS);
}
void tst_Algorithm::findOrDefault()
{
std::vector<int> v1{1, 2, 3, 4};
QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 2; }), 2);
QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 5; }), 0);
std::vector<std::unique_ptr<int>> v2;
v2.emplace_back(std::make_unique<int>(1));
v2.emplace_back(std::make_unique<int>(2));
v2.emplace_back(std::make_unique<int>(3));
v2.emplace_back(std::make_unique<int>(4));
QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get());
QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), nullptr);
std::vector<std::unique_ptr<Struct>> v3;
v3.emplace_back(std::make_unique<Struct>(1));
v3.emplace_back(std::make_unique<Struct>(3));
v3.emplace_back(std::make_unique<Struct>(5));
v3.emplace_back(std::make_unique<Struct>(7));
QCOMPARE(Utils::findOrDefault(v3, &Struct::isOdd), v3.at(0).get());
QCOMPARE(Utils::findOrDefault(v3, &Struct::isEven), nullptr);
}
QTEST_MAIN(tst_Algorithm)
#include "tst_algorithm.moc"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment