[llvm] r288619 - [stl-extras] Provide an adaptor of std::count for ranges.
Michael Gottesman via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 4 02:26:54 PST 2016
Author: mgottesman
Date: Sun Dec 4 04:26:53 2016
New Revision: 288619
URL: http://llvm.org/viewvc/llvm-project?rev=288619&view=rev
Log:
[stl-extras] Provide an adaptor of std::count for ranges.
Modified:
llvm/trunk/include/llvm/ADT/STLExtras.h
llvm/trunk/unittests/ADT/STLExtrasTest.cpp
Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=288619&r1=288618&r2=288619&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Sun Dec 4 04:26:53 2016
@@ -639,6 +639,14 @@ bool is_contained(R &&Range, const E &El
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>
Modified: llvm/trunk/unittests/ADT/STLExtrasTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/STLExtrasTest.cpp?rev=288619&r1=288618&r2=288619&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/STLExtrasTest.cpp (original)
+++ llvm/trunk/unittests/ADT/STLExtrasTest.cpp Sun Dec 4 04:26:53 2016
@@ -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));
+}
}
More information about the llvm-commits
mailing list