[PATCH] D122079: [ADT] add initializer list specialization for is_contained
Chris Bieneman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 29 10:23:55 PDT 2022
beanz updated this revision to Diff 418919.
beanz added a comment.
Updating based on feedback from @kuhar. Thank you!
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D122079/new/
https://reviews.llvm.org/D122079
Files:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -964,4 +964,29 @@
(std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value));
}
+enum Doggos {
+ Floofer,
+ Woofer,
+ SubWoofer,
+ Pupper,
+ Pupperino,
+ Longboi,
+};
+
+TEST(STLExtrasTest, IsContainedInitializerList) {
+ EXPECT_TRUE(is_contained({Woofer, SubWoofer}, Woofer));
+ EXPECT_TRUE(is_contained({Woofer, SubWoofer}, SubWoofer));
+ EXPECT_FALSE(is_contained({Woofer, SubWoofer}, Pupper));
+ EXPECT_FALSE(is_contained({}, Longboi));
+
+ static_assert(is_contained({Woofer, SubWoofer}, SubWoofer), "SubWoofer!");
+ static_assert(!is_contained({Woofer, SubWoofer}, Pupper), "Missing Pupper!");
+
+ EXPECT_TRUE(is_contained({1,2,3,4}, 3));
+ EXPECT_FALSE(is_contained({1,2,3,4}, 5));
+
+ static_assert(is_contained({1,2,3,4}, 3), "It's there!");
+ static_assert(!is_contained({1,2,3,4}, 5), "It's not there :(");
+}
+
} // namespace
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1657,6 +1657,15 @@
return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range);
}
+template <typename T>
+constexpr bool is_contained(std::initializer_list<T> set, T value) {
+ // TODO: Use std::find when we switch to C++20.
+ for (T v : set)
+ if (v == value)
+ return true;
+ return false;
+}
+
/// Wrapper function around std::is_sorted to check if elements in a range \p R
/// are sorted with respect to a comparator \p C.
template <typename R, typename Compare> bool is_sorted(R &&Range, Compare C) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122079.418919.patch
Type: text/x-patch
Size: 1809 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220329/a3d1061e/attachment.bin>
More information about the llvm-commits
mailing list