[llvm] 73f2a3b - [STLExtras] Use std::get in less_first,less_second to support more types

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 8 03:07:38 PST 2023


Author: Florian Hahn
Date: 2023-03-08T12:07:14+01:00
New Revision: 73f2a3b6a48b97f1deba605e4615b048533ee278

URL: https://github.com/llvm/llvm-project/commit/73f2a3b6a48b97f1deba605e4615b048533ee278
DIFF: https://github.com/llvm/llvm-project/commit/73f2a3b6a48b97f1deba605e4615b048533ee278.diff

LOG: [STLExtras] Use std::get in less_first,less_second to support more types

Update less_first,less_second to use std::get to access the first and
second component. This extends support to any type implementing
std::get, like tuples.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D145489

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 dc7bf6a106862..06a8b86a7feb0 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1513,19 +1513,21 @@ template <typename ContainerTy> auto make_second_range(ContainerTy &&c) {
 //     Extra additions to <utility>
 //===----------------------------------------------------------------------===//
 
-/// Function object to check whether the first component of a std::pair
-/// compares less than the first component of another std::pair.
+/// Function object to check whether the first component of a container
+/// supported by std::get (like std::pair and std::tuple) compares less than the
+/// first component of another container.
 struct less_first {
   template <typename T> bool operator()(const T &lhs, const T &rhs) const {
-    return std::less<>()(lhs.first, rhs.first);
+    return std::less<>()(std::get<0>(lhs), std::get<0>(rhs));
   }
 };
 
-/// Function object to check whether the second component of a std::pair
-/// compares less than the second component of another std::pair.
+/// Function object to check whether the second component of a container
+/// supported by std::get (like std::pair and std::tuple) compares less than the
+/// second component of another container.
 struct less_second {
   template <typename T> bool operator()(const T &lhs, const T &rhs) const {
-    return std::less<>()(lhs.second, rhs.second);
+    return std::less<>()(std::get<1>(lhs), std::get<1>(rhs));
   }
 };
 

diff  --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index a02b1dc8af22c..f39d0975109a5 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -14,6 +14,8 @@
 #include <array>
 #include <climits>
 #include <list>
+#include <tuple>
+#include <utility>
 #include <vector>
 
 using namespace llvm;
@@ -1055,4 +1057,36 @@ TEST(STLExtrasTest, addEnumValues) {
                 "addEnumValues(ULongLongMax, C::Two) failed.");
 }
 
+TEST(STLExtrasTest, LessFirst) {
+  {
+    std::pair<int, int> A(0, 1);
+    std::pair<int, int> B(1, 0);
+    EXPECT_TRUE(less_first()(A, B));
+    EXPECT_FALSE(less_first()(B, A));
+  }
+
+  {
+    std::tuple<int, int> A(0, 1);
+    std::tuple<int, int> B(1, 0);
+    EXPECT_TRUE(less_first()(A, B));
+    EXPECT_FALSE(less_first()(B, A));
+  }
+}
+
+TEST(STLExtrasTest, LessSecond) {
+  {
+    std::pair<int, int> A(0, 1);
+    std::pair<int, int> B(1, 0);
+    EXPECT_FALSE(less_second()(A, B));
+    EXPECT_TRUE(less_second()(B, A));
+  }
+
+  {
+    std::tuple<int, int> A(0, 1);
+    std::tuple<int, int> B(1, 0);
+    EXPECT_FALSE(less_second()(A, B));
+    EXPECT_TRUE(less_second()(B, A));
+  }
+}
+
 } // namespace


        


More information about the llvm-commits mailing list