[libcxx-commits] [libcxx] [libc++] Implement part of P2562R1: constexpr `std::inplace_merge` (PR #129008)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 17 10:31:25 PDT 2025


================
@@ -8,168 +8,183 @@
 
 // <algorithm>
 
-// template<BidirectionalIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
-//   requires ShuffleIterator<Iter>
-//         && CopyConstructible<Compare>
-//   void
-//   inplace_merge(Iter first, Iter middle, Iter last, Compare comp);
+// template<class BidirectionalIterator, class Compare>
+//   constexpr void                                         // constexpr since C++26
+//   inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
 
 #include <algorithm>
 #include <cassert>
 #include <functional>
-#include <random>
 #include <vector>
 
+#include "constexpr_random.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 11
 #include <memory>
 
 struct indirect_less {
   template <class P>
-  bool operator()(const P& x, const P& y) const {
+  TEST_CONSTEXPR_CXX26 bool operator()(const P& x, const P& y) const {
     return *x < *y;
   }
 };
 
 struct S {
-    S() : i_(0) {}
-    S(int i) : i_(i) {}
+  TEST_CONSTEXPR_CXX26 S() : i_(0) {}
+  TEST_CONSTEXPR_CXX26 S(int i) : i_(i) {}
 
-    S(const S&  rhs) : i_(rhs.i_) {}
-    S(      S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; }
+  TEST_CONSTEXPR_CXX26 S(const S& rhs) : i_(rhs.i_) {}
+  TEST_CONSTEXPR_CXX26 S(S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; }
 
-    S& operator =(const S&  rhs) { i_ = rhs.i_;              return *this; }
-    S& operator =(      S&& rhs) { i_ = rhs.i_; rhs.i_ = -2; assert(this != &rhs); return *this; }
-    S& operator =(int i)         { i_ = i;                   return *this; }
-
-    bool operator  <(const S&  rhs) const { return i_ < rhs.i_; }
-    bool operator  >(const S&  rhs) const { return i_ > rhs.i_; }
-    bool operator ==(const S&  rhs) const { return i_ == rhs.i_; }
-    bool operator ==(int i)         const { return i_ == i; }
+  TEST_CONSTEXPR_CXX26 S& operator=(const S& rhs) {
+    i_ = rhs.i_;
+    return *this;
+  }
+  TEST_CONSTEXPR_CXX26 S& operator=(S&& rhs) {
+    i_     = rhs.i_;
+    rhs.i_ = -2;
+    assert(this != &rhs);
+    return *this;
+  }
+  TEST_CONSTEXPR_CXX26 S& operator=(int i) {
+    i_ = i;
+    return *this;
+  }
 
-    void set(int i) { i_ = i; }
+  TEST_CONSTEXPR_CXX26 bool operator<(const S& rhs) const { return i_ < rhs.i_; }
+  TEST_CONSTEXPR_CXX26 bool operator>(const S& rhs) const { return i_ > rhs.i_; }
+  TEST_CONSTEXPR_CXX26 bool operator==(const S& rhs) const { return i_ == rhs.i_; }
+  TEST_CONSTEXPR_CXX26 bool operator==(int i) const { return i_ == i; }
 
-    int i_;
-    };
+  TEST_CONSTEXPR_CXX26 void set(int i) { i_ = i; }
 
+  int i_;
+};
 
 #endif // TEST_STD_VER >= 11
 
 #include "test_iterators.h"
 #include "counting_predicates.h"
 
-std::mt19937 randomness;
-
-template <class Iter>
-void
-test_one(unsigned N, unsigned M)
-{
-    assert(M <= N);
-    typedef typename std::iterator_traits<Iter>::value_type value_type;
-    value_type* ia = new value_type[N];
-    for (unsigned i = 0; i < N; ++i)
-        ia[i] = i;
-    std::shuffle(ia, ia+N, randomness);
-    std::sort(ia, ia+M, std::greater<value_type>());
-    std::sort(ia+M, ia+N, std::greater<value_type>());
-    binary_counting_predicate<std::greater<value_type>, value_type, value_type> pred((std::greater<value_type>()));
-    std::inplace_merge(Iter(ia), Iter(ia+M), Iter(ia+N), std::ref(pred));
-    if(N > 0)
-    {
-        assert(ia[0] == static_cast<int>(N)-1);
-        assert(ia[N-1] == 0);
-        assert(std::is_sorted(ia, ia+N, std::greater<value_type>()));
+template <class Iter, class RandSrc>
+TEST_CONSTEXPR_CXX26 void test_one(unsigned N, unsigned M, RandSrc& randomness) {
+  assert(M <= N);
+  typedef typename std::iterator_traits<Iter>::value_type value_type;
+  value_type* ia = new value_type[N];
+  for (unsigned i = 0; i < N; ++i)
+    ia[i] = i;
+  support::shuffle(ia, ia + N, randomness);
+  std::sort(ia, ia + M, std::greater<value_type>());
+  std::sort(ia + M, ia + N, std::greater<value_type>());
+  binary_counting_predicate<std::greater<value_type>, value_type, value_type> pred((std::greater<value_type>()));
+  std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N), std::ref(pred));
+  if (N > 0) {
+    assert(ia[0] == static_cast<int>(N) - 1);
+    assert(ia[N - 1] == 0);
+    assert(std::is_sorted(ia, ia + N, std::greater<value_type>()));
 #if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
-        assert(pred.count() <= (N-1));
+    assert(pred.count() <= (N - 1));
 #endif
-    }
-    delete [] ia;
+  }
+  delete[] ia;
 }
 
-template <class Iter>
-void
-test(unsigned N)
-{
-    test_one<Iter>(N, 0);
-    test_one<Iter>(N, N/4);
-    test_one<Iter>(N, N/2);
-    test_one<Iter>(N, 3*N/4);
-    test_one<Iter>(N, N);
+template <class Iter, class RandSrc>
+TEST_CONSTEXPR_CXX26 void test(unsigned N, RandSrc& randomness) {
+  test_one<Iter>(N, 0, randomness);
+  test_one<Iter>(N, N / 4, randomness);
+  test_one<Iter>(N, N / 2, randomness);
+  test_one<Iter>(N, 3 * N / 4, randomness);
+  test_one<Iter>(N, N, randomness);
 }
 
-template <class Iter>
-void
-test()
-{
-    test_one<Iter>(0, 0);
-    test_one<Iter>(1, 0);
-    test_one<Iter>(1, 1);
-    test_one<Iter>(2, 0);
-    test_one<Iter>(2, 1);
-    test_one<Iter>(2, 2);
-    test_one<Iter>(3, 0);
-    test_one<Iter>(3, 1);
-    test_one<Iter>(3, 2);
-    test_one<Iter>(3, 3);
-    test<Iter>(4);
-    test<Iter>(20);
-    test<Iter>(100);
-    test<Iter>(1000);
+template <class Iter, class RandSrc>
+TEST_CONSTEXPR_CXX26 void test(RandSrc& randomness) {
+  test_one<Iter>(0, 0, randomness);
+  test_one<Iter>(1, 0, randomness);
+  test_one<Iter>(1, 1, randomness);
+  test_one<Iter>(2, 0, randomness);
+  test_one<Iter>(2, 1, randomness);
+  test_one<Iter>(2, 2, randomness);
+  test_one<Iter>(3, 0, randomness);
+  test_one<Iter>(3, 1, randomness);
+  test_one<Iter>(3, 2, randomness);
+  test_one<Iter>(3, 3, randomness);
+  test<Iter>(4, randomness);
+  test<Iter>(20, randomness);
+  test<Iter>(50, randomness);
+#if defined(_LIBCPP_HARDENING_MODE)
+  if (!TEST_IS_CONSTANT_EVALUATED) // avoid exceeding the constant evaluation step limit
+#endif
+  {
+    test<Iter>(100, randomness);
+  }
+  if (!TEST_IS_CONSTANT_EVALUATED) { // avoid exceeding the constant evaluation step limit
+    test<Iter>(1000, randomness);
+  }
----------------
mordante wrote:

Same here.

https://github.com/llvm/llvm-project/pull/129008


More information about the libcxx-commits mailing list