Review fixes.

This commit is contained in:
Vladimir Byko-Ianko 2016-09-12 13:54:54 +03:00
parent ceeee1a8ec
commit 500560a6c2
3 changed files with 24 additions and 33 deletions

View file

@ -20,24 +20,24 @@ private:
int m_v;
};
template<template<class, class> class TContainer>
template<template<class, class> class Cont>
void TestSortUnique()
{
{
TContainer<int, allocator<int>> actual = {1, 2, 1, 4, 3, 5, 2, 7, 1};
Cont<int, allocator<int>> actual = {1, 2, 1, 4, 3, 5, 2, 7, 1};
my::SortUnique(actual);
TContainer<int, allocator<int>> const expected = {1, 2, 3, 4, 5, 7};
Cont<int, allocator<int>> const expected = {1, 2, 3, 4, 5, 7};
TEST_EQUAL(actual, expected, ());
}
{
using TValue = int;
using TPair = pair<TValue, int>;
TContainer<TPair, allocator<TPair>> d =
Cont<TPair, allocator<TPair>> d =
{{1, 22}, {2, 33}, {1, 23}, {4, 54}, {3, 34}, {5, 23}, {2, 23}, {7, 32}, {1, 12}};
my::SortUnique<TPair>(d, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
my::SortUnique(d, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
TContainer<TValue, allocator<TValue>> const expected = {1, 2, 3, 4, 5, 7};
Cont<TValue, allocator<TValue>> const expected = {1, 2, 3, 4, 5, 7};
TEST_EQUAL(d.size(), expected.size(), ());
for (int i = 0; i < d.size(); ++i)
TEST_EQUAL(d[i].first, expected[i], (i));
@ -45,39 +45,39 @@ void TestSortUnique()
{
using TValue = double;
using TPair = pair<TValue, int>;
TContainer<TPair, allocator<TPair>> d =
Cont<TPair, allocator<TPair>> d =
{{0.5, 11}, {1000.99, 234}, {0.5, 23}, {1234.56789, 54}, {1000.99, 34}};
my::SortUnique<TPair>(d, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
my::SortUnique(d, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
TContainer<TValue, allocator<TValue>> const expected = {0.5, 1000.99, 1234.56789};
Cont<TValue, allocator<TValue>> const expected = {0.5, 1000.99, 1234.56789};
TEST_EQUAL(d.size(), expected.size(), ());
for (int i = 0; i < d.size(); ++i)
TEST_EQUAL(d[i].first, expected[i], (i));
}
}
template<template<class, class> class TContainer>
template<template<class, class> class Cont>
void TestEqualsBy()
{
{
using TValue = pair<int, int>;
TContainer<TValue, allocator<TValue>> actual = {{1, 2}, {1, 3}, {2, 100}, {3, 7}, {3, 8}, {2, 500}};
Cont<TValue, allocator<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());
TContainer<int, allocator<int>> const expected = {{1, 2, 3, 2}};
Cont<int, allocator<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, ());
}
{
TContainer<Int, allocator<Int>> actual;
Cont<Int, allocator<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());
TContainer<int, allocator<int>> const expected = {{0, 1, 2, 0}};
Cont<int, allocator<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(), ());
@ -117,20 +117,12 @@ UNIT_TEST(LessBy)
UNIT_TEST(EqualsBy_VectorTest)
{
TestEqualsBy<vector>();
}
UNIT_TEST(EqualsBy_DequeTest)
{
TestEqualsBy<deque>();
}
UNIT_TEST(SortUnique_VectorTest)
{
TestSortUnique<vector>();
}
UNIT_TEST(SortUnique_DequeTest)
{
TestSortUnique<deque>();
}
} // namespace

View file

@ -82,8 +82,8 @@ struct Equals<false, T, C>
} // namespace impl
// Sorts and removes duplicate entries from |c|.
template <typename T, template <typename, typename = allocator<T>> class Container>
void SortUnique(Container<T> & c)
template <typename T, template <typename, typename = allocator<T>> class Cont>
void SortUnique(Cont<T> & c)
{
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
@ -92,18 +92,17 @@ void SortUnique(Container<T> & c)
// Sorts according to |comp| and removes duplicate entries according to |pred| from |c|.
// Note. If several entries are equal according to |pred| an arbitrary entry of them
// is left in |c| after a call of this function.
template <typename T, template <typename, typename = allocator<T>> class Container,
typename TLess, typename TEquals>
void SortUnique(Container<T> & c, TLess && less, TEquals && equals)
template <class Cont, typename Less, typename Equals>
void SortUnique(Cont & c, Less && less, Equals && equals)
{
sort(c.begin(), c.end(), forward<TLess>(less));
c.erase(unique(c.begin(), c.end(), forward<TEquals>(equals)), c.end());
sort(c.begin(), c.end(), forward<Less>(less));
c.erase(unique(c.begin(), c.end(), forward<Equals>(equals)), c.end());
}
template <typename T, template <typename, typename = allocator<T>> class Container, class TFn>
void EraseIf(Container<T> & c, TFn && fn)
template <class Cont, class Fn>
void EraseIf(Cont & c, Fn && fn)
{
c.erase(remove_if(c.begin(), c.end(), forward<TFn>(fn)), c.end());
c.erase(remove_if(c.begin(), c.end(), forward<Fn>(fn)), c.end());
}
// Creates a comparer being able to compare two instances of class C

View file

@ -5,8 +5,8 @@
#endif
#include <memory>
using std::auto_ptr;
using std::allocator;
using std::auto_ptr;
#ifdef DEBUG_NEW
#define new DEBUG_NEW