mirror of
https://github.com/RPCSX/llvm.git
synced 2025-02-28 17:06:31 +00:00
[stl-extras] Provide an adaptor of std::count for ranges.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288619 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
a31bc24eae
commit
6a2af3eb1d
@ -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>
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user