Review fixes.

This commit is contained in:
Yuri Gorshenin 2016-05-27 11:29:21 +03:00
parent 60e74b995a
commit 03f67d43f7
2 changed files with 39 additions and 25 deletions

View file

@ -21,9 +21,9 @@ private:
UNIT_TEST(LessBy)
{
using TValue = pair<int, int>;
{
using TValue = pair<int, int>;
vector<TValue> v = {{2, 2}, {0, 4}, {3, 1}, {4, 0}, {1, 3}};
sort(v.begin(), v.end(), my::LessBy(&TValue::first));
for (size_t i = 0; i < v.size(); ++i)
@ -51,13 +51,27 @@ UNIT_TEST(LessBy)
UNIT_TEST(EqualsBy)
{
using TValue = pair<int, int>;
vector<TValue> actual = {{1, 2}, {1, 3}, {2, 100}, {3, 7}, {3, 8}, {2, 500}};
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&TValue::first)), actual.end());
{
using TValue = pair<int, int>;
vector<TValue> actual = {{1, 2}, {1, 3}, {2, 100}, {3, 7}, {3, 8}, {2, 500}};
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&TValue::first)), actual.end());
vector<int> expected = {{1, 2, 3, 2}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].first, ());
vector<int> const expected = {{1, 2, 3, 2}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].first, ());
}
{
vector<Int> actual;
for (auto const v : {0, 0, 1, 2, 2, 0})
actual.emplace_back(v);
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&Int::Get)), actual.end());
vector<int> const expected = {{0, 1, 2, 0}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].Get(), ());
}
}
} // namespace

View file

@ -16,31 +16,31 @@ struct Less;
template <typename T, typename C>
struct Less<true, T, C>
{
Less(T(C::*p)) : p_(p) {}
Less(T(C::*p)) : m_p(p) {}
inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*p_ < rhs.*p_; }
inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*m_p < rhs.*m_p; }
inline bool operator()(C const * const lhs, C const * const rhs) const
{
return lhs->*p_ < rhs->*p_;
return lhs->*m_p < rhs->*m_p;
}
T(C::*p_);
T(C::*m_p);
};
template <typename T, typename C>
struct Less<false, T, C>
{
Less(T (C::*p)() const) : p_(p) {}
Less(T (C::*p)() const) : m_p(p) {}
inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*p_)() < (rhs.*p_)(); }
inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*m_p)() < (rhs.*m_p)(); }
inline bool operator()(C const * const lhs, C const * const rhs) const
{
return (lhs->*p_)() < (rhs->*p_)();
return (lhs->*m_p)() < (rhs->*m_p)();
}
T (C::*p_)() const;
T (C::*m_p)() const;
};
template <bool isField, typename T, typename C>
@ -49,31 +49,31 @@ struct Equals;
template <typename T, typename C>
struct Equals<true, T, C>
{
Equals(T(C::*p)) : p_(p) {}
Equals(T(C::*p)) : m_p(p) {}
inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*p_ == rhs.*p_; }
inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*m_p == rhs.*m_p; }
inline bool operator()(C const * const lhs, C const * const rhs) const
{
return lhs->*p_ == rhs->*p_;
return lhs->*m_p == rhs->*m_p;
}
T(C::*p_);
T(C::*m_p);
};
template <typename T, typename C>
struct Equals<false, T, C>
{
Equals(T (C::*p)() const) : p_(p) {}
Equals(T (C::*p)() const) : m_p(p) {}
inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*p_)() == (rhs.*p_)(); }
inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*m_p)() == (rhs.*m_p)(); }
inline bool operator()(C const * const lhs, C const * const rhs) const
{
return (lhs->*p_)() == (rhs->*p_)();
return (lhs->*m_p)() == (rhs->*m_p)();
}
T (C::*p_)() const;
T (C::*m_p)() const;
};
} // namespace impl