diff options
author | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2016-05-26 15:45:08 +0300 |
---|---|---|
committer | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2016-05-27 14:22:59 +0300 |
commit | de449b38b67dddd6308c6b1875e6818e08851b1a (patch) | |
tree | caa1f6554530a9c1a1d7193ce44970326d790df0 /base | |
parent | 4591c38bfb2b976b6df6dfa284e68377b225e53f (diff) |
Review fixes.
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/stl_helpers_test.cpp | 64 | ||||
-rw-r--r-- | base/stl_helpers.hpp | 10 |
2 files changed, 36 insertions, 38 deletions
diff --git a/base/base_tests/stl_helpers_test.cpp b/base/base_tests/stl_helpers_test.cpp index ac60b8c064..f42e8ac4df 100644 --- a/base/base_tests/stl_helpers_test.cpp +++ b/base/base_tests/stl_helpers_test.cpp @@ -77,41 +77,37 @@ UNIT_TEST(EqualsBy) UNIT_TEST(SortUnique) { - vector<int> v = {1, 2, 1, 4, 3, 5, 2, 7, 1}; - my::SortUnique(v); - vector<int> const expected = {1, 2, 3, 4, 5, 7}; - TEST_EQUAL(v, expected, ()); -} + { + vector<int> v = {1, 2, 1, 4, 3, 5, 2, 7, 1}; + my::SortUnique(v); + vector<int> const expected = {1, 2, 3, 4, 5, 7}; + TEST_EQUAL(v, expected, ()); + } + { + using TValue = int; + using TPair = pair<TValue, int>; + vector<TPair> v = + {{1, 22}, {2, 33}, {1, 23}, {4, 54}, {3, 34}, {5, 23}, {2, 23}, {7, 32}, {1, 12}}; -UNIT_TEST(SortUniqueCompPredTest1) -{ - using TValue = int; - using TPair = pair<TValue, int>; - vector<TPair> v = - {{1, 22}, {2, 33}, {1, 23}, {4, 54}, {3, 34}, {5, 23}, {2, 23}, {7, 32}, {1, 12}}; - - my::SortUnique<TPair>(v, my::CompareBy(&TPair::first), - [](TPair const & p1, TPair const & p2) { return p1.first == p2.first; }); - - vector<TValue> const expected = {1, 2, 3, 4, 5, 7}; - TEST_EQUAL(v.size(), expected.size(), ()); - for (int i = 0; i < v.size(); ++i) - TEST_EQUAL(v[i].first, expected[i], (i)); -} + my::SortUnique<TPair>(v, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first)); -UNIT_TEST(SortUniqueCompPredTest2) -{ - using TValue = double; - using TPair = pair<TValue, int>; - vector<TPair> v = - {{0.5, 11}, {1000.99, 234}, {0.5, 23}, {1234.56789, 54}, {1000.99, 34}}; - - my::SortUnique<TPair>(v, my::CompareBy(&TPair::first), - [](TPair const & p1, TPair const & p2) { return p1.first == p2.first; }); - - vector<TValue> const expected = {0.5, 1000.99, 1234.56789}; - TEST_EQUAL(v.size(), expected.size(), ()); - for (int i = 0; i < v.size(); ++i) - TEST_EQUAL(v[i].first, expected[i], (i)); + vector<TValue> const expected = {1, 2, 3, 4, 5, 7}; + TEST_EQUAL(v.size(), expected.size(), ()); + for (int i = 0; i < v.size(); ++i) + TEST_EQUAL(v[i].first, expected[i], (i)); + } + { + using TValue = double; + using TPair = pair<TValue, int>; + vector<TPair> v = + {{0.5, 11}, {1000.99, 234}, {0.5, 23}, {1234.56789, 54}, {1000.99, 34}}; + + my::SortUnique<TPair>(v, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first)); + + vector<TValue> const expected = {0.5, 1000.99, 1234.56789}; + TEST_EQUAL(v.size(), expected.size(), ()); + for (int i = 0; i < v.size(); ++i) + TEST_EQUAL(v[i].first, expected[i], (i)); + } } } // namespace diff --git a/base/stl_helpers.hpp b/base/stl_helpers.hpp index b11f95eec7..0121e12613 100644 --- a/base/stl_helpers.hpp +++ b/base/stl_helpers.hpp @@ -2,8 +2,10 @@ #include "std/algorithm.hpp" #include "std/functional.hpp" +#include "std/utility.hpp" #include "std/vector.hpp" + namespace my { namespace impl @@ -89,11 +91,11 @@ void SortUnique(vector<T> & v) // Sorts according to |comp| and removes duplicate entries according to |pred| from |v|. // Note. If several entries are equal according to |pred| an arbitrary entry of them // is left in |v| after a call of this function. -template <typename T, typename TComp, typename TPred> -void SortUnique(vector<T> & v, TComp && comp, TPred && pred) +template <typename T, typename TLess, typename TEquals> +void SortUnique(vector<T> & v, TLess && less, TEquals && equals) { - sort(v.begin(), v.end(), comp); - v.erase(unique(v.begin(), v.end(), pred), v.end()); + sort(v.begin(), v.end(), forward<TLess>(less)); + v.erase(unique(v.begin(), v.end(), forward<TEquals>(equals)), v.end()); } template <typename T, class TFn> |