[llvm] 211314d - [ADT] Add is_sorted_constexpr, equivalent to C++20 std::is_sorted (#180867)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 09:43:28 PST 2026


Author: Ryan Buchner
Date: 2026-02-13T09:43:22-08:00
New Revision: 211314d5824498e885c7d3d4fb5a77924e064ab4

URL: https://github.com/llvm/llvm-project/commit/211314d5824498e885c7d3d4fb5a77924e064ab4
DIFF: https://github.com/llvm/llvm-project/commit/211314d5824498e885c7d3d4fb5a77924e064ab4.diff

LOG: [ADT] Add is_sorted_constexpr, equivalent to C++20 std::is_sorted  (#180867)

`std::is_sorted` is not `constexpr` until Cpp20, so need custom `is_sorted_constexpr` function.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/STLExtras.h
    llvm/include/llvm/ADT/STLForwardCompat.h
    llvm/unittests/ADT/STLExtrasTest.cpp
    llvm/unittests/ADT/STLForwardCompatTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 74ad080b7bed2..80c97e77724e9 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1977,6 +1977,14 @@ template <typename R> bool is_sorted(R &&Range) {
   return std::is_sorted(adl_begin(Range), adl_end(Range));
 }
 
+/// Check if elements in a range \p R are sorted with respect to a comparator \p
+/// C. constexpr allows use in static_assert
+/// TODO: Remove and use std::is_sorted once upgraded to Cpp20
+template <typename R, typename Cmp = std::less<>>
+constexpr bool is_sorted_constexpr(R &&Range, Cmp C = Cmp{}) {
+  return llvm::is_sorted_constexpr(adl_begin(Range), adl_end(Range), C);
+}
+
 /// Provide wrappers to std::includes which take ranges instead of having to
 /// pass begin/end explicitly.
 /// This function checks if the sorted range \p R2 is a subsequence of the

diff  --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h
index d61c880747502..a02b7f7a8a410 100644
--- a/llvm/include/llvm/ADT/STLForwardCompat.h
+++ b/llvm/include/llvm/ADT/STLForwardCompat.h
@@ -18,6 +18,7 @@
 #define LLVM_ADT_STLFORWARDCOMPAT_H
 
 #include "llvm/Support/Compiler.h"
+#include <functional>
 #include <optional>
 #include <tuple>
 #include <type_traits>
@@ -158,6 +159,23 @@ invoke(FnT &&Fn, ArgsT &&...Args) { // NOLINT(readability-identifier-naming)
                     std::forward_as_tuple(std::forward<ArgsT>(Args)...));
 }
 
+/// Check if elements in range \p First to \p Last are sorted with respect to a
+/// comparator \p C. constexpr allows use in static_assert
+/// TODO: Use std::is_sorted once upgraded to C++20 since that becomes constexpr
+template <typename ForwardIterator, typename Cmp = std::less<>>
+constexpr bool is_sorted_constexpr(ForwardIterator First, ForwardIterator Last,
+                                   Cmp C = Cmp{}) {
+  if (First == Last)
+    return true;
+  ForwardIterator Prev = First;
+  for (ForwardIterator I = std::next(First); I != Last; ++I) {
+    if (C(*I, *Prev))
+      return false;
+    Prev = I;
+  }
+  return true;
+}
+
 //===----------------------------------------------------------------------===//
 //     Features from C++23
 //===----------------------------------------------------------------------===//

diff  --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index cb59c6d9d1ca4..f068563dae404 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1964,4 +1964,14 @@ TEST(STLExtrasTest, AdjacentFind) {
   EXPECT_EQ(*std::next(It13), 3);
 }
 
+// Compile-time tests for llvm::is_sorted_constexpr
+// Check to ensure range based functions as expected
+static constexpr std::array<int, 5> CSorted{{1, 2, 2, 3, 5}};
+static_assert(llvm::is_sorted_constexpr(CSorted),
+              "Non-descending order with duplicates should be sorted");
+static_assert(llvm::is_sorted_constexpr(CSorted, std::less<>()),
+              "Explicit std::less non-descending order should be sorted");
+static_assert(!llvm::is_sorted_constexpr(CSorted, std::greater<>()),
+              "Non-descending order should not be sorted by std::greater");
+
 } // namespace

diff  --git a/llvm/unittests/ADT/STLForwardCompatTest.cpp b/llvm/unittests/ADT/STLForwardCompatTest.cpp
index 8831062109cb3..68976a7fede45 100644
--- a/llvm/unittests/ADT/STLForwardCompatTest.cpp
+++ b/llvm/unittests/ADT/STLForwardCompatTest.cpp
@@ -10,6 +10,7 @@
 #include "CountCopyAndMove.h"
 #include "gtest/gtest.h"
 
+#include <iterator>
 #include <optional>
 #include <type_traits>
 #include <utility>
@@ -605,5 +606,40 @@ TEST(STLForwardCompatTest, BindFrontBindBack) {
   EXPECT_TRUE(any_of(V, Spec1));
 }
 
+// Compile-time tests for llvm::is_sorted_constexpr
+static constexpr int CEmptyHelper[]{-1};
+static_assert(llvm::is_sorted_constexpr(std::begin(CEmptyHelper),
+                                        std::begin(CEmptyHelper)),
+              "Empty range should be sorted");
+
+static constexpr int CSingle[]{42};
+static_assert(llvm::is_sorted_constexpr(std::begin(CSingle), std::end(CSingle)),
+              "Single element range should be sorted");
+static_assert(llvm::is_sorted_constexpr(std::begin(CSingle), std::end(CSingle),
+                                        std::greater<>()),
+              "Single element range should be sorted with std::greater");
+
+static constexpr int CSorted[]{1, 2, 2, 3, 5};
+static_assert(llvm::is_sorted_constexpr(std::begin(CSorted), std::end(CSorted)),
+              "Non-descending order with duplicates should be sorted");
+static_assert(llvm::is_sorted_constexpr(std::begin(CSorted), std::end(CSorted),
+                                        std::less<>()),
+              "Explicit std::less non-descending order should be sorted");
+static_assert(!llvm::is_sorted_constexpr(std::begin(CSorted), std::end(CSorted),
+                                         std::greater<>()),
+              "Non-descending order should not be sorted by std::greater");
+
+static constexpr int CUnsorted[]{1, 3, 2, 4, 5};
+static_assert(!llvm::is_sorted_constexpr(std::begin(CUnsorted),
+                                         std::end(CUnsorted)),
+              "Unsorted range should not be sorted");
+
+static constexpr int CDesc[]{9, 7, 7, 3, 0};
+static_assert(llvm::is_sorted_constexpr(std::begin(CDesc), std::end(CDesc),
+                                        std::greater<>()),
+              "Non-ascending order with std::greater should be sorted");
+static_assert(llvm::is_sorted_constexpr(std::rbegin(CDesc), std::rend(CDesc)),
+              "Reverse iterators should be supported.");
+
 } // namespace
 } // namespace llvm


        


More information about the llvm-commits mailing list