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

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


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG73f2a3b6a48b: [STLExtras] Use std::get in less_first,less_second to support more types (authored by fhahn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145489/new/

https://reviews.llvm.org/D145489

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
@@ -14,6 +14,8 @@
 #include <array>
 #include <climits>
 #include <list>
+#include <tuple>
+#include <utility>
 #include <vector>
 
 using namespace llvm;
@@ -1055,4 +1057,36 @@
                 "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
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1513,19 +1513,21 @@
 //     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));
   }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145489.503291.patch
Type: text/x-patch
Size: 2622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230308/d3678476/attachment.bin>


More information about the llvm-commits mailing list