[stl-extras] Provide an adaptor of std::count for ranges.

llvm-svn: 288619
This commit is contained in:
Michael Gottesman 2016-12-04 10:26:53 +00:00
parent 6e8c2b1b65
commit 0bc89fbf6a
2 changed files with 25 additions and 0 deletions

View File

@ -639,6 +639,14 @@ bool is_contained(R &&Range, const E &Element) {
std::end(Range);
}
/// Wrapper function around std::count to count the number of times an element
/// \p Element occurs in the given range \p Range.
template <typename R, typename E>
auto count(R &&Range, const E &Element) -> typename std::iterator_traits<
decltype(std::begin(Range))>::difference_type {
return std::count(std::begin(Range), std::end(Range), Element);
}
/// Wrapper function around std::count_if to count the number of times an
/// element satisfying a given predicate occurs in a range.
template <typename R, typename UnaryPredicate>

View File

@ -236,4 +236,21 @@ TEST(STLExtrasTest, ApplyTupleVariadic) {
EXPECT_EQ("Tes", std::get<1>(Values));
EXPECT_EQ('Y', std::get<2>(Values));
}
TEST(STLExtrasTest, CountAdaptor) {
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(2);
v.push_back(1);
EXPECT_EQ(3, count(v, 1));
EXPECT_EQ(2, count(v, 2));
EXPECT_EQ(1, count(v, 3));
EXPECT_EQ(1, count(v, 4));
}
}