-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[llvm][ADT] Add getSingleElement
helper
#131508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[llvm][ADT] Add getSingleElement
helper
#131508
Conversation
@llvm/pr-subscribers-llvm-adt Author: Matthias Springer (matthias-springer) ChangesThis commit adds a new helper function: This function asserts that the container has a single element and then returns that element. This helper function is useful during 1:N dialect conversions in MLIR, where certain Full diff: https://github.com/llvm/llvm-project/pull/131508.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 78b7e94c2b3a1..dc0443c9244be 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -325,6 +325,14 @@ template <typename ContainerTy> bool hasSingleElement(ContainerTy &&C) {
return B != E && std::next(B) == E;
}
+/// Asserts that the given container has a single element and returns that
+/// element.
+template <typename ContainerTy>
+decltype(auto) getSingleElement(ContainerTy &&C) {
+ assert(hasSingleElement(C) && "expected container with single element");
+ return *adl_begin(C);
+}
+
/// Return a range covering \p RangeOrContainer with the first N elements
/// excluded.
template <typename T> auto drop_begin(T &&RangeOrContainer, size_t N = 1) {
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index dbb094b0a3088..df8c0a4e4819b 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1016,6 +1016,28 @@ TEST(STLExtrasTest, hasSingleElement) {
EXPECT_FALSE(hasSingleElement(S));
}
+TEST(STLExtrasTest, getSingleElement) {
+ // Note: Asserting behavior of getSingleElement cannot be tested because the
+ // program would crash.
+ const std::vector<int> V1 = {7};
+ EXPECT_EQ(getSingleElement(V1), 7);
+
+ std::vector<int> V2 = {8};
+ EXPECT_EQ(getSingleElement(V2), 8);
+
+ SmallVector<int> V3 {9};
+ EXPECT_EQ(getSingleElement(V3), 9);
+
+ std::list<int> L1 = {10};
+ EXPECT_EQ(getSingleElement(L1), 10);
+
+ // Make sure that we use the `begin`/`end` functions from `some_namespace`,
+ // using ADL.
+ some_namespace::some_struct S;
+ S.data = V2;
+ EXPECT_EQ(getSingleElement(S), 8);
+}
+
TEST(STLExtrasTest, hasNItems) {
const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2};
const std::list<int> V3 = {1, 3, 5};
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
75e8b95
to
65159c4
Compare
65159c4
to
78eca92
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/9489 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/64/builds/2511 Here is the relevant piece of the build log for the reference
|
This commit adds a new helper function:
getSingleElement
This function asserts that the container has a single element and then returns that element. This helper function is useful during 1:N dialect conversions in MLIR, where certain
ValueRange
s (returned from the adaptor) are known to have a single value.