[llvm] c9e52fb - [ADT] Add all_equal predicate
Jakub Kuderski via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 22 21:01:16 PDT 2022
Author: Jakub Kuderski
Date: 2022-08-22T23:55:23-04:00
New Revision: c9e52fbe4d4fa37a87b2855ad7e118dcdee2db68
URL: https://github.com/llvm/llvm-project/commit/c9e52fbe4d4fa37a87b2855ad7e118dcdee2db68
DIFF: https://github.com/llvm/llvm-project/commit/c9e52fbe4d4fa37a87b2855ad7e118dcdee2db68.diff
LOG: [ADT] Add all_equal predicate
`llvm::all_equal` checks if all values in the given range are equal, i.e., there are no two elements that are not equal.
Similar to `llvm::all_of`, it returns `true` when the range is empty.
`llvm::all_equal` is intended to supersede `llvm::is_splat`, which will be deprecated and removed in future patches.
See the discussion thread for more details:
https://discourse.llvm.org/t/adt-is-splat-and-empty-ranges/64692.
Reviewed By: dblaikie, shchenz
Differential Revision: https://reviews.llvm.org/D132334
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 9173719d851df..44f86e48d7e4e 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1763,16 +1763,25 @@ template <typename L, typename R> bool equal(L &&LRange, R &&RRange) {
adl_end(RRange));
}
-/// Wrapper function around std::equal to detect if all elements
-/// in a container are same.
-template <typename R>
-bool is_splat(R &&Range) {
- size_t range_size = size(Range);
- return range_size != 0 && (range_size == 1 ||
- std::equal(adl_begin(Range) + 1, adl_end(Range), adl_begin(Range)));
+/// Returns true if all elements in Range are equal or when the Range is empty.
+template <typename R> bool all_equal(R &&Range) {
+ auto Begin = adl_begin(Range);
+ auto End = adl_end(Range);
+ return Begin == End || std::equal(Begin + 1, End, Begin);
+}
+
+/// Returns true if all Values in the initializer lists are equal or the list
+// is empty.
+template <typename T> bool all_equal(std::initializer_list<T> Values) {
+ return all_equal<std::initializer_list<T>>(std::move(Values));
+}
+
+/// Returns true if Range consists of the same value repeated multiple times.
+template <typename R> bool is_splat(R &&Range) {
+ return !llvm::empty(Range) && all_equal(Range);
}
-/// Returns true iff all Values in the initializer lists are same.
+/// Returns true if Values consists of the same value repeated multiple times.
template <typename T> bool is_splat(std::initializer_list<T> Values) {
return is_splat<std::initializer_list<T>>(std::move(Values));
}
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 8ae50202cbd6c..9b0af51658c24 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -588,6 +588,29 @@ TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) {
EXPECT_EQ(EIR.end(), I);
}
+TEST(STLExtrasTest, AllEqual) {
+ std::vector<int> V;
+ EXPECT_TRUE(all_equal(V));
+
+ V.push_back(1);
+ EXPECT_TRUE(all_equal(V));
+
+ V.push_back(1);
+ V.push_back(1);
+ EXPECT_TRUE(all_equal(V));
+
+ V.push_back(2);
+ EXPECT_FALSE(all_equal(V));
+}
+
+TEST(STLExtrasTest, AllEqualInitializerList) {
+ EXPECT_TRUE(all_equal({1}));
+ EXPECT_TRUE(all_equal({1, 1}));
+ EXPECT_FALSE(all_equal({1, 2}));
+ EXPECT_FALSE(all_equal({2, 1}));
+ EXPECT_TRUE(all_equal({1, 1, 1}));
+}
+
TEST(STLExtrasTest, IsSplat) {
std::vector<int> V;
EXPECT_FALSE(is_splat(V));
More information about the llvm-commits
mailing list