Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorVladimir Byko-Ianko <v.bykoianko@corp.mail.ru>2016-05-26 13:49:01 +0300
committerVladimir Byko-Ianko <v.bykoianko@corp.mail.ru>2016-05-27 14:17:14 +0300
commite060fd2fa6898a41d8f849b8d575dd17cd1d2bb4 (patch)
tree398814db7e8d3463b04e6eff7b8d84a85974f9f5 /base
parent36c26c63894dfe127d5d3ecf521e9d6a17bf20f5 (diff)
SortUnique refactoring.
Diffstat (limited to 'base')
-rw-r--r--base/base_tests/stl_helpers_test.cpp49
-rw-r--r--base/stl_helpers.hpp17
2 files changed, 34 insertions, 32 deletions
diff --git a/base/base_tests/stl_helpers_test.cpp b/base/base_tests/stl_helpers_test.cpp
index aac0393117..ac60b8c064 100644
--- a/base/base_tests/stl_helpers_test.cpp
+++ b/base/base_tests/stl_helpers_test.cpp
@@ -83,26 +83,35 @@ UNIT_TEST(SortUnique)
TEST_EQUAL(v, expected, ());
}
-UNIT_TEST(SortUniquePred)
+UNIT_TEST(SortUniqueCompPredTest1)
{
- struct Foo
- {
- int i, j;
- };
-
- vector<Foo> v = {{1, 22}, {2, 33}, {1, 23}, {4, 54}, {3, 34}, {5, 23}, {2, 23}, {7, 32}, {1, 12}};
- my::SortUnique<Foo>([](Foo const & f1, Foo const & f2)
- {
- return f1.i < f2.i;
- },
- v);
-
- TEST_EQUAL(v.size(), 6, ());
- TEST_EQUAL(v[0].i, 1, ());
- TEST_EQUAL(v[1].i, 2, ());
- TEST_EQUAL(v[2].i, 3, ());
- TEST_EQUAL(v[3].i, 4, ());
- TEST_EQUAL(v[4].i, 5, ());
- TEST_EQUAL(v[5].i, 7, ());
+ 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));
+}
+
+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));
}
} // namespace
diff --git a/base/stl_helpers.hpp b/base/stl_helpers.hpp
index 035b77e161..b11f95eec7 100644
--- a/base/stl_helpers.hpp
+++ b/base/stl_helpers.hpp
@@ -86,20 +86,13 @@ void SortUnique(vector<T> & v)
v.erase(unique(v.begin(), v.end()), v.end());
}
-// Sorts and removes duplicate entries from |v| according to |comp|.
-// Note. If several entries are equivalent according to |comp| an arbitrary entry of them
-// is left in |v| after a call of this method.
-// Note. |comp| should implement operator<. It means the expression
-// !comp(t1, t2) && !comp(t2, t1) should return true iff t1 is equivalent to t2.
-// It's necessary for std::unique.
-template <typename T>
-void SortUnique(function<bool(T const &, T const &)> const & comp, 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)
{
sort(v.begin(), v.end(), comp);
- function<bool(T const &, T const &)> const pred = [&comp](T const & t1, T const & t2)
- {
- return !comp(t1, t2) && !comp(t2, t1);
- };
v.erase(unique(v.begin(), v.end(), pred), v.end());
}