[libcxx-commits] [libcxx] 1cdec6c - [libcxx][ranges] implement `std::ranges::set_difference`

Hui Xie via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 8 05:30:00 PDT 2022


Author: Hui Xie
Date: 2022-07-08T13:26:23+01:00
New Revision: 1cdec6c96e852f4ebac61711a622535bcaf07542

URL: https://github.com/llvm/llvm-project/commit/1cdec6c96e852f4ebac61711a622535bcaf07542
DIFF: https://github.com/llvm/llvm-project/commit/1cdec6c96e852f4ebac61711a622535bcaf07542.diff

LOG: [libcxx][ranges] implement `std::ranges::set_difference`

implement `std::ranges::set_difference`
reused classic std::set_difference
added unit tests

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

Added: 
    libcxx/include/__algorithm/ranges_set_difference.h
    libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp

Modified: 
    libcxx/docs/Status/RangesAlgorithms.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/__algorithm/make_projected.h
    libcxx/include/__algorithm/set_difference.h
    libcxx/include/algorithm
    libcxx/include/module.modulemap.in
    libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
    libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
    libcxx/test/libcxx/private_headers.verify.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp
    libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h
    libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
    libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index 7047a0054f04b..19cc7c0458f54 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -61,7 +61,7 @@ Write,unique_copy,Not assigned,n/a,Not started
 Write,partition_copy,Not assigned,n/a,Not started
 Write,partial_sort_copy,Not assigned,n/a,Not started
 Merge,merge,Hui Xie,`D128611 <https://llvm.org/D128611>`_,✅
-Merge,set_
diff erence,Hui Xie,n/a,Not started
+Merge,set_
diff erence,Hui Xie,`D128983 <https://llvm.org/D128983>`,✅
 Merge,set_intersection,Hui Xie,n/a,Not started
 Merge,set_symmetric_
diff erence,Hui Xie,n/a,Not started
 Merge,set_union,Hui Xie,n/a,Not started

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 479c4090d083c..09ce7bfd4b533 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -108,6 +108,7 @@ set(files
   __algorithm/ranges_replace.h
   __algorithm/ranges_replace_if.h
   __algorithm/ranges_reverse.h
+  __algorithm/ranges_set_
diff erence.h
   __algorithm/ranges_sort.h
   __algorithm/ranges_stable_sort.h
   __algorithm/ranges_swap_ranges.h

diff  --git a/libcxx/include/__algorithm/make_projected.h b/libcxx/include/__algorithm/make_projected.h
index 8141c4ed176fa..6d8ebfd3d90e8 100644
--- a/libcxx/include/__algorithm/make_projected.h
+++ b/libcxx/include/__algorithm/make_projected.h
@@ -13,6 +13,8 @@
 #include <__config>
 #include <__functional/identity.h>
 #include <__functional/invoke.h>
+#include <__type_traits/decay.h>
+#include <__type_traits/is_member_pointer.h>
 #include <__utility/forward.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -28,7 +30,7 @@ namespace ranges {
 template <class _Comp, class _Proj>
 _LIBCPP_HIDE_FROM_ABI constexpr static
 decltype(auto) __make_projected_comp(_Comp& __comp, _Proj& __proj) {
-  if constexpr (same_as<_Proj, identity>) {
+  if constexpr (same_as<decay_t<_Proj>, identity> && !is_member_pointer_v<decay_t<_Comp>>) {
     // Avoid creating the lambda and just use the pristine comparator -- for certain algorithms, this would enable
     // optimizations that rely on the type of the comparator.
     return __comp;
@@ -42,6 +44,24 @@ decltype(auto) __make_projected_comp(_Comp& __comp, _Proj& __proj) {
   }
 }
 
+template <class _Comp, class _Proj1, class _Proj2>
+_LIBCPP_HIDE_FROM_ABI constexpr static
+decltype(auto) __make_projected_comp(_Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) {
+  if constexpr (same_as<decay_t<_Proj1>, identity> && same_as<decay_t<_Proj2>, identity> &&
+                !is_member_pointer_v<decay_t<_Comp>>) {
+    // Avoid creating the lambda and just use the pristine comparator -- for certain algorithms, this would enable
+    // optimizations that rely on the type of the comparator.
+    return __comp;
+
+  } else {
+    return [&](auto&& __lhs, auto&& __rhs) {
+      return std::invoke(__comp,
+                        std::invoke(__proj1, std::forward<decltype(__lhs)>(__lhs)),
+                        std::invoke(__proj2, std::forward<decltype(__rhs)>(__rhs)));
+    };
+  }
+}
+
 } // namespace ranges
 
 _LIBCPP_END_NAMESPACE_STD

diff  --git a/libcxx/include/__algorithm/ranges_set_
diff erence.h b/libcxx/include/__algorithm/ranges_set_
diff erence.h
new file mode 100644
index 0000000000000..4eb3efad38955
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_set_
diff erence.h
@@ -0,0 +1,104 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_RANGES_SET_DIFFERENCE_H
+#define _LIBCPP___ALGORITHM_RANGES_SET_DIFFERENCE_H
+
+#include <__algorithm/in_out_result.h>
+#include <__algorithm/make_projected.h>
+#include <__algorithm/set_
diff erence.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
+#include <__functional/ranges_operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/mergeable.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/dangling.h>
+#include <__type_traits/decay.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+
+template <class _InIter, class _OutIter>
+using set_
diff erence_result = in_out_result<_InIter, _OutIter>;
+
+namespace __set_
diff erence {
+
+struct __fn {
+  template <
+      input_iterator _InIter1,
+      sentinel_for<_InIter1> _Sent1,
+      input_iterator _InIter2,
+      sentinel_for<_InIter2> _Sent2,
+      weakly_incrementable _OutIter,
+      class _Comp  = less,
+      class _Proj1 = identity,
+      class _Proj2 = identity>
+    requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
+  _LIBCPP_HIDE_FROM_ABI constexpr set_
diff erence_result<_InIter1, _OutIter> operator()(
+      _InIter1 __first1,
+      _Sent1 __last1,
+      _InIter2 __first2,
+      _Sent2 __last2,
+      _OutIter __result,
+      _Comp __comp   = {},
+      _Proj1 __proj1 = {},
+      _Proj2 __proj2 = {}) const {
+    auto __ret = std::__set_
diff erence(
+        __first1, __last1, __first2, __last2, __result, ranges::__make_projected_comp(__comp, __proj1, __proj2));
+    return {std::move(__ret.first), std::move(__ret.second)};
+  }
+
+  template <
+      input_range _Range1,
+      input_range _Range2,
+      weakly_incrementable _OutIter,
+      class _Comp  = less,
+      class _Proj1 = identity,
+      class _Proj2 = identity>
+    requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2> 
+  _LIBCPP_HIDE_FROM_ABI constexpr set_
diff erence_result<borrowed_iterator_t<_Range1>, _OutIter>
+    operator()(
+        _Range1&& __range1,
+        _Range2&& __range2,
+        _OutIter __result,
+        _Comp __comp   = {},
+        _Proj1 __proj1 = {},
+        _Proj2 __proj2 = {}) const {
+    auto __ret = std::__set_
diff erence(
+        ranges::begin(__range1),
+        ranges::end(__range1),
+        ranges::begin(__range2),
+        ranges::end(__range2),
+        __result,
+        ranges::__make_projected_comp(__comp, __proj1, __proj2));
+    return {std::move(__ret.first), std::move(__ret.second)};
+  }
+};
+
+} // namespace __set_
diff erence
+
+inline namespace __cpo {
+  inline constexpr auto set_
diff erence = __set_
diff erence::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
+#endif // _LIBCPP___ALGORITHM_RANGES_SET_DIFFERENCE_H

diff  --git a/libcxx/include/__algorithm/set_
diff erence.h b/libcxx/include/__algorithm/set_
diff erence.h
index 5e2dca24e446c..4378bd5304d9e 100644
--- a/libcxx/include/__algorithm/set_
diff erence.h
+++ b/libcxx/include/__algorithm/set_
diff erence.h
@@ -13,7 +13,12 @@
 #include <__algorithm/comp_ref_type.h>
 #include <__algorithm/copy.h>
 #include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
 #include <__iterator/iterator_traits.h>
+#include <__utility/move.h>
+#include <__utility/pair.h>
+#include <type_traits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -21,50 +26,52 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
-_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
-__set_
diff erence(_InputIterator1 __first1, _InputIterator1 __last1,
-                 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
-{
-    while (__first1 != __last1)
-    {
-        if (__first2 == __last2)
-            return _VSTD::copy(__first1, __last1, __result);
-        if (__comp(*__first1, *__first2))
-        {
-            *__result = *__first1;
-            ++__result;
-            ++__first1;
-        }
-        else
-        {
-            if (!__comp(*__first2, *__first1))
-                ++__first1;
-            ++__first2;
-        }
+template < class _Comp, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<__uncvref_t<_InIter1>, __uncvref_t<_OutIter> >
+__set_
diff erence(
+    _InIter1&& __first1, _Sent1&& __last1, _InIter2&& __first2, _Sent2&& __last2, _OutIter&& __result, _Comp&& __comp) {
+  while (__first1 != __last1 && __first2 != __last2) {
+    if (__comp(*__first1, *__first2)) {
+      *__result = *__first1;
+      ++__first1;
+      ++__result;
+    } else if (__comp(*__first2, *__first1)) {
+      ++__first2;
+    } else {
+      ++__first1;
+      ++__first2;
     }
-    return __result;
+  }
+  return std::__copy(std::move(__first1), std::move(__last1), std::move(__result));
 }
 
 template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
-_OutputIterator
-set_
diff erence(_InputIterator1 __first1, _InputIterator1 __last1,
-               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
-{
-    typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
-    return _VSTD::__set_
diff erence<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_
diff erence(
+    _InputIterator1 __first1,
+    _InputIterator1 __last1,
+    _InputIterator2 __first2,
+    _InputIterator2 __last2,
+    _OutputIterator __result,
+    _Compare __comp) {
+  typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
+  return std::__set_
diff erence<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp).second;
 }
 
 template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
-_OutputIterator
-set_
diff erence(_InputIterator1 __first1, _InputIterator1 __last1,
-               _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
-{
-    return _VSTD::set_
diff erence(__first1, __last1, __first2, __last2, __result,
-                                __less<typename iterator_traits<_InputIterator1>::value_type,
-                                       typename iterator_traits<_InputIterator2>::value_type>());
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_
diff erence(
+    _InputIterator1 __first1,
+    _InputIterator1 __last1,
+    _InputIterator2 __first2,
+    _InputIterator2 __last2,
+    _OutputIterator __result) {
+  return std::__set_
diff erence(
+      __first1,
+      __last1,
+      __first2,
+      __last2,
+      __result,
+      __less<typename iterator_traits<_InputIterator1>::value_type,
+             typename iterator_traits<_InputIterator2>::value_type>()).second;
 }
 
 _LIBCPP_END_NAMESPACE_STD

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index a7236a31d7061..fef1ad379e9d3 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -530,6 +530,24 @@ namespace ranges {
     requires permutable<iterator_t<R>>
     constexpr borrowed_subrange_t<R>
       ranges::remove_if(R&& r, Pred pred, Proj proj = {});                                          // since C++20
+  
+  template<class I, class O>
+    using set_
diff erence_result = in_out_result<I, O>;                                              // since C++20                                              
+
+  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
+           weakly_incrementable O, class Comp = ranges::less,
+           class Proj1 = identity, class Proj2 = identity>
+    requires mergeable<I1, I2, O, Comp, Proj1, Proj2>
+    constexpr set_
diff erence_result<I1, O>
+      set_
diff erence(I1 first1, S1 last1, I2 first2, S2 last2, O result,
+                     Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                           // since C++20
+
+  template<input_range R1, input_range R2, weakly_incrementable O,
+           class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity>
+    requires mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2>
+    constexpr set_
diff erence_result<borrowed_iterator_t<R1>, O>
+      set_
diff erence(R1&& r1, R2&& r2, O result,
+                     Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                           // since C++20
 
 }
 
@@ -1300,6 +1318,7 @@ template <class BidirectionalIterator, class Compare>
 #include <__algorithm/ranges_replace.h>
 #include <__algorithm/ranges_replace_if.h>
 #include <__algorithm/ranges_reverse.h>
+#include <__algorithm/ranges_set_
diff erence.h>
 #include <__algorithm/ranges_sort.h>
 #include <__algorithm/ranges_stable_sort.h>
 #include <__algorithm/ranges_swap_ranges.h>

diff  --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index b547b50ab2b1a..98bb00b9f038d 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -347,6 +347,7 @@ module std [system] {
       module ranges_replace                  { private header "__algorithm/ranges_replace.h" }
       module ranges_replace_if               { private header "__algorithm/ranges_replace_if.h" }
       module ranges_reverse                  { private header "__algorithm/ranges_reverse.h" }
+      module ranges_set_
diff erence           { private header "__algorithm/ranges_set_
diff erence.h" }
       module ranges_sort                     { private header "__algorithm/ranges_sort.h" }
       module ranges_stable_sort              { private header "__algorithm/ranges_stable_sort.h" }
       module ranges_swap_ranges              { private header "__algorithm/ranges_swap_ranges.h" }

diff  --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
index 4652e2c384463..ccb2551fa9361 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
@@ -199,8 +199,8 @@ constexpr bool all_the_algorithms()
     //(void)std::ranges::search(a, b, Equal(&copies)); assert(copies == 0);
     //(void)std::ranges::search_n(first, last, count, value, Equal(&copies)); assert(copies == 0);
     //(void)std::ranges::search_n(a, count, value, Equal(&copies)); assert(copies == 0);
-    //(void)std::ranges::set_
diff erence(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
-    //(void)std::ranges::set_
diff erence(a, b, first2, Less(&copies)); assert(copies == 0);
+    (void)std::ranges::set_
diff erence(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
+    (void)std::ranges::set_
diff erence(a, b, first2, Less(&copies)); assert(copies == 0);
     //(void)std::ranges::set_intersection(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
     //(void)std::ranges::set_intersection(a, b, first2, Less(&copies)); assert(copies == 0);
     //(void)std::ranges::set_symmetric_
diff erence(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);

diff  --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
index e47f675c18bd1..3cb73276010f7 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
@@ -190,8 +190,8 @@ constexpr bool all_the_algorithms()
     //(void)std::ranges::search(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::search_n(first, last, count, value, Equal(), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::search_n(a, count, value, Equal(), Proj(&copies)); assert(copies == 0);
-    //(void)std::ranges::set_
diff erence(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
-    //(void)std::ranges::set_
diff erence(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::set_
diff erence(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::set_
diff erence(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::set_intersection(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::set_intersection(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::set_symmetric_
diff erence(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);

diff  --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp
index ae5ff43e8be5b..1607c66e77eda 100644
--- a/libcxx/test/libcxx/private_headers.verify.cpp
+++ b/libcxx/test/libcxx/private_headers.verify.cpp
@@ -145,6 +145,7 @@ END-SCRIPT
 #include <__algorithm/ranges_replace.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_replace.h'}}
 #include <__algorithm/ranges_replace_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_replace_if.h'}}
 #include <__algorithm/ranges_reverse.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse.h'}}
+#include <__algorithm/ranges_set_
diff erence.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_
diff erence.h'}}
 #include <__algorithm/ranges_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_sort.h'}}
 #include <__algorithm/ranges_stable_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_stable_sort.h'}}
 #include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}}

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp
index 869535a999028..7273fc29df561 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp
@@ -11,15 +11,15 @@
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
 
-// template<input_­iterator I1, sentinel_­for<I1> S1, input_­iterator I2, sentinel_­for<I2> S2,
-//          weakly_­incrementable O, class Comp = ranges::less, class Proj1 = identity,
+// template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
+//          weakly_incrementable O, class Comp = ranges::less, class Proj1 = identity,
 //          class Proj2 = identity>
 //   requires mergeable<I1, I2, O, Comp, Proj1, Proj2>
 //   constexpr merge_result<I1, I2, O>
 //     merge(I1 first1, S1 last1, I2 first2, S2 last2, O result,
 //           Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                                    // since C++20
 //
-// template<input_­range R1, input_­range R2, weakly_­incrementable O, class Comp = ranges::less,
+// template<input_range R1, input_range R2, weakly_incrementable O, class Comp = ranges::less,
 //          class Proj1 = identity, class Proj2 = identity>
 //   requires mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2>
 //   constexpr merge_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
@@ -33,6 +33,7 @@
 #include "almost_satisfies_types.h"
 #include "MoveOnly.h"
 #include "test_iterators.h"
+#include "../sortable_helpers.h"
 
 // Test iterator overload's constraints:
 // =====================================
@@ -251,15 +252,15 @@ constexpr bool withAllPermutationsOfInIter1AndInIter2() {
 }
 
 constexpr void runAllIteratorPermutationsTests() {
-  withAllPermutationsOfInIter1AndInIter2<cpp17_output_iterator<int*>>();
   withAllPermutationsOfInIter1AndInIter2<cpp20_output_iterator<int*>>();
+  withAllPermutationsOfInIter1AndInIter2<cpp20_input_iterator<int*>>();
   withAllPermutationsOfInIter1AndInIter2<forward_iterator<int*>>();
   withAllPermutationsOfInIter1AndInIter2<bidirectional_iterator<int*>>();
   withAllPermutationsOfInIter1AndInIter2<random_access_iterator<int*>>();
   withAllPermutationsOfInIter1AndInIter2<contiguous_iterator<int*>>();
 
-  static_assert(withAllPermutationsOfInIter1AndInIter2<cpp17_output_iterator<int*>>());
   static_assert(withAllPermutationsOfInIter1AndInIter2<cpp20_output_iterator<int*>>());
+  static_assert(withAllPermutationsOfInIter1AndInIter2<cpp20_input_iterator<int*>>());
   static_assert(withAllPermutationsOfInIter1AndInIter2<forward_iterator<int*>>());
   static_assert(withAllPermutationsOfInIter1AndInIter2<bidirectional_iterator<int*>>());
   static_assert(withAllPermutationsOfInIter1AndInIter2<random_access_iterator<int*>>());
@@ -268,26 +269,6 @@ constexpr void runAllIteratorPermutationsTests() {
 
 constexpr bool test() {
   // check that every element is copied exactly once
-  struct TracedCopy {
-    int copy_assign = 0;
-    int data        = 0;
-
-    constexpr TracedCopy() = default;
-    constexpr TracedCopy(int i) : data(i) {}
-    constexpr TracedCopy(const TracedCopy& other) : copy_assign(other.copy_assign), data(other.data) {}
-
-    constexpr TracedCopy(TracedCopy&& other)            = delete;
-    constexpr TracedCopy& operator=(TracedCopy&& other) = delete;
-
-    constexpr TracedCopy& operator=(const TracedCopy& other) {
-      copy_assign = other.copy_assign + 1;
-      data        = other.data;
-      return *this;
-    }
-
-    constexpr bool operator==(const TracedCopy& o) const { return data == o.data; }
-    constexpr auto operator<=>(const TracedCopy& o) const { return data <=> o.data; }
-  };
   {
     std::array<TracedCopy, 3> r1{3, 5, 8};
     std::array<TracedCopy, 3> r2{1, 3, 8};
@@ -302,7 +283,7 @@ constexpr bool test() {
       assert(result.out == out.end());
       assert(std::ranges::equal(out, std::array<TracedCopy, 6>{1, 3, 3, 5, 8, 8}));
 
-      assert(std::ranges::all_of(out, [](const TracedCopy& e) { return e.copy_assign == 1; }));
+      assert(std::ranges::all_of(out, &TracedCopy::copiedOnce));
     }
 
     // range overload
@@ -315,7 +296,7 @@ constexpr bool test() {
       assert(result.out == out.end());
       assert(std::ranges::equal(out, std::array<TracedCopy, 6>{1, 3, 3, 5, 8, 8}));
 
-      assert(std::ranges::all_of(out, [](const TracedCopy& e) { return e.copy_assign == 1; }));
+      assert(std::ranges::all_of(out, &TracedCopy::copiedOnce));
     }
   }
 

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.
diff erence/ranges_set_
diff erence.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.
diff erence/ranges_set_
diff erence.pass.cpp
new file mode 100644
index 0000000000000..03560dbd67fdf
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.
diff erence/ranges_set_
diff erence.pass.cpp
@@ -0,0 +1,569 @@
+
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
+//          weakly_incrementable O, class Comp = ranges::less,
+//          class Proj1 = identity, class Proj2 = identity>
+//   requires mergeable<I1, I2, O, Comp, Proj1, Proj2>
+//   constexpr set_
diff erence_result<I1, O>
+//     set_
diff erence(I1 first1, S1 last1, I2 first2, S2 last2, O result,
+//                    Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                           // since C++20
+
+// template<input_range R1, input_range R2, weakly_incrementable O,
+//          class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity>
+//   requires mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2>
+//   constexpr set_
diff erence_result<borrowed_iterator_t<R1>, O>
+//     set_
diff erence(R1&& r1, R2&& r2, O result,
+//                    Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});                           // since C++20
+
+#include <algorithm>
+#include <array>
+#include <concepts>
+
+#include "almost_satisfies_types.h"
+#include "MoveOnly.h"
+#include "test_iterators.h"
+#include "../../sortable_helpers.h"
+
+// Test iterator overload's constraints:
+// =====================================
+template <
+    class InIter1,
+    class InIter2,
+    class OutIter,
+    class Sent1 = sentinel_wrapper<InIter1>,
+    class Sent2 = sentinel_wrapper<InIter2>>
+concept HasSetDifferenceIter =
+    requires(InIter1&& inIter1, InIter2&& inIter2, OutIter&& outIter, Sent1&& sent1, Sent2&& sent2) {
+      std::ranges::set_
diff erence(
+          std::forward<InIter1>(inIter1),
+          std::forward<Sent1>(sent1),
+          std::forward<InIter2>(inIter2),
+          std::forward<Sent2>(sent2),
+          std::forward<OutIter>(outIter));
+    };
+
+static_assert(HasSetDifferenceIter<int*, int*, int*, int*, int*>);
+
+// !std::input_iterator<I1>
+static_assert(!HasSetDifferenceIter<InputIteratorNotDerivedFrom, int*, int*>);
+
+// !std::sentinel_for<S1, I1>
+static_assert(!HasSetDifferenceIter<int*, int*, int*, SentinelForNotSemiregular>);
+
+// !std::input_iterator<I2>
+static_assert(!HasSetDifferenceIter<int*, InputIteratorNotDerivedFrom, int*>);
+
+// !std::sentinel_for<S2, I2>
+static_assert(!HasSetDifferenceIter<int*, int*, int*, int*, SentinelForNotSemiregular>);
+
+// !std::weakly_incrementable<O>
+static_assert(!HasSetDifferenceIter<int*, int*, WeaklyIncrementableNotMovable>);
+
+// !std::mergeable<I1, I2, O, Comp, Proj1, Proj2>
+static_assert(!HasSetDifferenceIter<MoveOnly*, MoveOnly*, MoveOnly*, MoveOnly*, MoveOnly*>);
+
+// Test range overload's constraints:
+// =====================================
+
+template <class Range1, class Range2, class OutIter>
+concept HasSetDifferenceRange =
+    requires(Range1&& range1, Range2&& range2, OutIter&& outIter) {
+      std::ranges::set_
diff erence(
+          std::forward<Range1>(range1), std::forward<Range2>(range2), std::forward<OutIter>(outIter));
+    };
+
+static_assert(HasSetDifferenceRange<UncheckedRange<int*>, UncheckedRange<int*>, int*>);
+
+// !std::input_range<R2>
+static_assert(!HasSetDifferenceRange<UncheckedRange<InputIteratorNotDerivedFrom>, UncheckedRange<int*>, int*>);
+
+// !std::input_range<R2>
+static_assert(!HasSetDifferenceRange<UncheckedRange<int*>, UncheckedRange<InputIteratorNotDerivedFrom>, int*>);
+
+// !std::weakly_incrementable<O>
+static_assert(!HasSetDifferenceRange<UncheckedRange<int*>, UncheckedRange<int*>, WeaklyIncrementableNotMovable >);
+
+// !std::mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2>
+static_assert(!HasSetDifferenceRange< UncheckedRange<MoveOnly*>, UncheckedRange<MoveOnly*>, MoveOnly*>);
+
+using std::ranges::set_
diff erence_result;
+
+template <class In1, class In2, class Out, std::size_t N1, std::size_t N2, std::size_t N3>
+constexpr void testSetDifferenceImpl(std::array<int, N1> in1, std::array<int, N2> in2, std::array<int, N3> expected) {
+  // TODO: std::ranges::set_
diff erence calls std::ranges::copy
+  // std::ranges::copy(contiguous_iterator<int*>, sentinel_wrapper<contiguous_iterator<int*>>, contiguous_iterator<int*>) doesn't seem to work.
+  // It seems that std::ranges::copy calls std::copy, which unwraps contiguous_iterator<int*> into int*,
+  // and then it failed because there is no == between int* and sentinel_wrapper<contiguous_iterator<int*>>
+  using Sent1 = std::conditional_t<std::contiguous_iterator<In1>, In1, sentinel_wrapper<In1>>;
+  using Sent2 = std::conditional_t<std::contiguous_iterator<In2>, In2, sentinel_wrapper<In2>>;
+
+  // iterator overload
+  {
+    std::array<int, N3> out;
+    std::same_as<set_
diff erence_result<In1, Out>> decltype(auto) result = std::ranges::set_
diff erence(
+        In1{in1.data()},
+        Sent1{In1{in1.data() + in1.size()}},
+        In2{in2.data()},
+        Sent2{In2{in2.data() + in2.size()}},
+        Out{out.data()});
+    assert(std::ranges::equal(out, expected));
+
+    assert(base(result.in) == in1.data() + in1.size());
+    assert(base(result.out) == out.data() + out.size());
+  }
+
+  // range overload
+  {
+    std::array<int, N3> out;
+    std::ranges::subrange r1{In1{in1.data()}, Sent1{In1{in1.data() + in1.size()}}};
+    std::ranges::subrange r2{In2{in2.data()}, Sent2{In2{in2.data() + in2.size()}}};
+    std::same_as<set_
diff erence_result<In1, Out>> decltype(auto) result =
+        std::ranges::set_
diff erence(r1, r2, Out{out.data()});
+    assert(std::ranges::equal(out, expected));
+
+    assert(base(result.in) == in1.data() + in1.size());
+    assert(base(result.out) == out.data() + out.size());
+  }
+}
+
+template <class In1, class In2, class Out>
+constexpr void testImpl() {
+  // range 1 shorter than range2
+  {
+    std::array in1{0, 1, 5, 6, 9, 10};
+    std::array in2{3, 6, 7, 9, 13, 15, 100};
+    std::array expected{0, 1, 5, 10};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 2 shorter than range 1
+  {
+    std::array in1{2, 6, 8, 12, 15, 16};
+    std::array in2{0, 2, 8};
+    std::array expected{6, 12, 15, 16};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 1 and range 2 has the same length but 
diff erent elements
+  {
+    std::array in1{2, 6, 8, 12, 15, 16};
+    std::array in2{0, 2, 8, 15, 17, 19};
+    std::array expected{6, 12, 16};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 1 == range 2
+  {
+    std::array in1{0, 1, 2};
+    std::array in2{0, 1, 2};
+    std::array<int, 0> expected{};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 1 is super set of range 2
+  {
+    std::array in1{8, 8, 10, 12, 13};
+    std::array in2{8, 10};
+    std::array expected{8, 12, 13};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 2 is super set of range 1
+  {
+    std::array in1{0, 1, 1};
+    std::array in2{0, 1, 1, 2, 5};
+    std::array<int, 0> expected{};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 1 is empty
+  {
+    std::array<int, 0> in1{};
+    std::array in2{3, 4, 5};
+    std::array<int, 0> expected{};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // range 2 is empty
+  {
+    std::array in1{3, 4, 5};
+    std::array<int, 0> in2{};
+    std::array expected{3, 4, 5};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // both ranges are empty
+  {
+    std::array<int, 0> in1{};
+    std::array<int, 0> in2{};
+    std::array<int, 0> expected{};
+    testSetDifferenceImpl<In1, In2, Out>(in1, in2, expected);
+  }
+
+  // check that ranges::dangling is returned for non-borrowed_range
+  {
+    struct NonBorrowedRange {
+      int* data_;
+      size_t size_;
+
+      // TODO: std::ranges::set_
diff erence calls std::__copy
+      // std::ranges::copy(contiguous_iterator<int*>, sentinel_wrapper<contiguous_iterator<int*>>, contiguous_iterator<int*>) doesn't seem to work.
+      // It seems that std::ranges::copy calls std::copy, which unwraps contiguous_iterator<int*> into int*,
+      // and then it failed because there is no == between int* and sentinel_wrapper<contiguous_iterator<int*>>
+      using Sent = std::conditional_t<std::contiguous_iterator<In2>, In2, sentinel_wrapper<In2>>;
+
+      constexpr NonBorrowedRange(int* d, size_t s) : data_{d}, size_{s} {}
+
+      constexpr In2 begin() const { return In2{data_}; };
+      constexpr Sent end() const { return Sent{In2{data_ + size_}}; };
+    };
+
+    std::array r1{3, 6, 7, 9};
+    std::array r2{2, 3, 4, 5, 6};
+    std::array<int, 2> out;
+    std::same_as<set_
diff erence_result<std::ranges::dangling, int*>> decltype(auto) result =
+        std::ranges::set_
diff erence(NonBorrowedRange{r1.data(), r1.size()}, r2, out.data());
+    assert(base(result.out) == out.data() + out.size());
+    assert(std::ranges::equal(out, std::array{7, 9}));
+  }
+}
+
+template <class InIter2, class OutIter>
+constexpr void withAllPermutationsOfInIter1() {
+  // C++17 InputIterator may or may not satisfy std::input_iterator
+  testImpl<cpp20_input_iterator<int*>, InIter2, OutIter>();
+  testImpl<forward_iterator<int*>, InIter2, OutIter>();
+  testImpl<bidirectional_iterator<int*>, InIter2, OutIter>();
+  testImpl<random_access_iterator<int*>, InIter2, OutIter>();
+  testImpl<contiguous_iterator<int*>, InIter2, OutIter>();
+}
+
+template <class OutIter>
+constexpr bool withAllPermutationsOfInIter1AndInIter2() {
+  withAllPermutationsOfInIter1<cpp20_input_iterator<int*>, OutIter>();
+  withAllPermutationsOfInIter1<forward_iterator<int*>, OutIter>();
+  withAllPermutationsOfInIter1<bidirectional_iterator<int*>, OutIter>();
+  withAllPermutationsOfInIter1<random_access_iterator<int*>, OutIter>();
+  withAllPermutationsOfInIter1<contiguous_iterator<int*>, OutIter>();
+  return true;
+}
+
+constexpr void runAllIteratorPermutationsTests() {
+  withAllPermutationsOfInIter1AndInIter2<cpp20_output_iterator<int*>>();
+  withAllPermutationsOfInIter1AndInIter2<cpp20_input_iterator<int*>>();
+  withAllPermutationsOfInIter1AndInIter2<forward_iterator<int*>>();
+  withAllPermutationsOfInIter1AndInIter2<bidirectional_iterator<int*>>();
+  withAllPermutationsOfInIter1AndInIter2<random_access_iterator<int*>>();
+  withAllPermutationsOfInIter1AndInIter2<contiguous_iterator<int*>>();
+
+  static_assert(withAllPermutationsOfInIter1AndInIter2<cpp20_output_iterator<int*>>());
+  static_assert(withAllPermutationsOfInIter1AndInIter2<cpp20_input_iterator<int*>>());
+  static_assert(withAllPermutationsOfInIter1AndInIter2<forward_iterator<int*>>());
+  static_assert(withAllPermutationsOfInIter1AndInIter2<bidirectional_iterator<int*>>());
+  static_assert(withAllPermutationsOfInIter1AndInIter2<random_access_iterator<int*>>());
+  static_assert(withAllPermutationsOfInIter1AndInIter2<contiguous_iterator<int*>>());
+}
+
+constexpr bool test() {
+  // check that every element is copied exactly once
+  {
+    std::array<TracedCopy, 5> r1{3, 5, 8, 15, 16};
+    std::array<TracedCopy, 3> r2{1, 3, 8};
+
+    // iterator overload
+    {
+      std::array<TracedCopy, 3> out;
+      auto result = std::ranges::set_
diff erence(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data());
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+      assert(std::ranges::equal(out, std::array<TracedCopy, 3>{5, 15, 16}));
+
+      assert(std::ranges::all_of(out, &TracedCopy::copiedOnce));
+    }
+
+    // range overload
+    {
+      std::array<TracedCopy, 3> out;
+      auto result = std::ranges::set_
diff erence(r1, r2, out.data());
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+      assert(std::ranges::equal(out, std::array<TracedCopy, 3>{5, 15, 16}));
+
+      assert(std::ranges::all_of(out, &TracedCopy::copiedOnce));
+    }
+  }
+
+  struct IntAndOrder {
+    int data;
+    int order;
+
+    constexpr auto operator==(const IntAndOrder& o) const { return data == o.data; }
+    constexpr auto operator<=>(const IntAndOrder& o) const { return data <=> o.data; }
+  };
+
+  // equal elements should be copied in the original order and only m-n elements are copied
+  {
+    std::array<IntAndOrder, 5> r1{{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}}};
+    std::array<IntAndOrder, 3> r2{{{0, 1}, {0, 2}, {0, 3}}};
+
+    // iterator overload
+    {
+      std::array<IntAndOrder, 2> out;
+      std::ranges::set_
diff erence(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data());
+
+      assert(std::ranges::equal(out, std::array{0, 0}, {}, &IntAndOrder::data));
+      // m-n elements are copied in order
+      assert(std::ranges::equal(out, std::array{3, 4}, {}, &IntAndOrder::order));
+    }
+
+    // range overload
+    {
+      std::array<IntAndOrder, 2> out;
+      std::ranges::set_
diff erence(r1, r2, out.data());
+
+      assert(std::ranges::equal(out, std::array{0, 0}, {}, &IntAndOrder::data));
+      // ID should be in their original order
+      assert(std::ranges::equal(out, std::array{3, 4}, {}, &IntAndOrder::order));
+    }
+  }
+
+  struct Data {
+    int data;
+
+    constexpr bool smallerThan(const Data& o) const { return data < o.data; }
+  };
+
+  // Test custom comparator
+  {
+    std::array r1{Data{4}, Data{8}, Data{12}};
+    std::array r2{Data{8}, Data{9}};
+    using Iter1 = std::array<Data, 3>::iterator;
+
+    // iterator overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result = std::ranges::set_
diff erence(
+          r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), [](const Data& x, const Data& y) {
+            return x.data < y.data;
+          });
+
+      assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+
+    // range overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result =
+          std::ranges::set_
diff erence(r1, r2, out.data(), [](const Data& x, const Data& y) { return x.data < y.data; });
+
+      assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+
+    // member pointer Comparator iterator overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result =
+          std::ranges::set_
diff erence(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), &Data::smallerThan);
+
+      assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+
+    // member pointer Comparator range overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result =
+          std::ranges::set_
diff erence(r1, r2, out.data(), &Data::smallerThan);
+
+      assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+  }
+
+  // Test Projection
+  {
+    std::array r1{Data{1}, Data{3}, Data{5}};
+    std::array r2{Data{2}, Data{3}, Data{4}};
+    using Iter1 = std::array<Data, 3>::iterator;
+
+    const auto proj = [](const Data& d) { return d.data; };
+
+    // iterator overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result = std::ranges::set_
diff erence(
+          r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), std::ranges::less{}, proj, proj);
+
+      assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+
+    // range overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result =
+          std::ranges::set_
diff erence(r1, r2, out.data(), std::ranges::less{}, proj, proj);
+
+      assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+
+    // member pointer Projection iterator overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result = std::ranges::set_
diff erence(
+          r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), {}, &Data::data, &Data::data);
+
+      assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+
+    // member pointer Projection range overload
+    {
+      std::array<Data, 2> out;
+      std::same_as<set_
diff erence_result<Iter1, Data*>> decltype(auto) result =
+          std::ranges::set_
diff erence(r1, r2, out.data(), std::ranges::less{}, &Data::data, &Data::data);
+
+      assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data));
+
+      assert(result.in == r1.end());
+      assert(result.out == out.end());
+    }
+  }
+
+  // Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection.
+  {
+    std::array<Data, 3> r1{{{4}, {8}, {12}}};
+    std::array<Data, 9> r2{{{1}, {2}, {3}, {5}, {6}, {7}, {9}, {10}, {11}}};
+    std::array expected{4, 8, 12};
+
+    const std::size_t maxOperation = 2 * (r1.size() + r2.size()) - 1;
+
+    // iterator overload
+    {
+      std::array<Data, 3> out;
+      std::size_t numberOfComp  = 0;
+      std::size_t numberOfProj1 = 0;
+      std::size_t numberOfProj2 = 0;
+
+      const auto comp = [&numberOfComp](int x, int y) {
+        ++numberOfComp;
+        return x < y;
+      };
+
+      const auto proj1 = [&numberOfProj1](const Data& d) {
+        ++numberOfProj1;
+        return d.data;
+      };
+
+      const auto proj2 = [&numberOfProj2](const Data& d) {
+        ++numberOfProj2;
+        return d.data;
+      };
+
+      std::ranges::set_
diff erence(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), comp, proj1, proj2);
+
+      assert(std::ranges::equal(out, expected, {}, &Data::data));
+      assert(numberOfComp < maxOperation);
+      assert(numberOfProj1 < maxOperation);
+      assert(numberOfProj2 < maxOperation);
+    }
+
+    // range overload
+    {
+      std::array<Data, 3> out;
+      std::size_t numberOfComp  = 0;
+      std::size_t numberOfProj1 = 0;
+      std::size_t numberOfProj2 = 0;
+
+      const auto comp = [&numberOfComp](int x, int y) {
+        ++numberOfComp;
+        return x < y;
+      };
+
+      const auto proj1 = [&numberOfProj1](const Data& d) {
+        ++numberOfProj1;
+        return d.data;
+      };
+
+      const auto proj2 = [&numberOfProj2](const Data& d) {
+        ++numberOfProj2;
+        return d.data;
+      };
+
+      std::ranges::set_
diff erence(r1, r2, out.data(), comp, proj1, proj2);
+
+      assert(std::ranges::equal(out, expected, {}, &Data::data));
+      assert(numberOfComp < maxOperation);
+      assert(numberOfProj1 < maxOperation);
+      assert(numberOfProj2 < maxOperation);
+    }
+  }
+
+  // Comparator convertible to bool
+  {
+    struct ConvertibleToBool {
+      bool b;
+      constexpr operator bool() const { return b; }
+    };
+    Data r1[] = {{2}, {4}};
+    Data r2[] = {{3}, {4}, {5}};
+
+    const auto comp = [](const Data& x, const Data& y) { return ConvertibleToBool{x.data < y.data}; };
+
+    // iterator overload
+    {
+      std::array<Data, 1> out;
+      std::ranges::set_
diff erence(r1, r1 + 2, r2, r2 + 3, out.data(), comp);
+      assert(std::ranges::equal(out, std::array{2}, {}, &Data::data));
+    }
+
+    // range overload
+    {
+      std::array<Data, 1> out;
+      std::ranges::set_
diff erence(r1, r2, out.data(), comp);
+      assert(std::ranges::equal(out, std::array{2}, {}, &Data::data));
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  runAllIteratorPermutationsTests();
+
+  return 0;
+}

diff  --git a/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h b/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h
index b749229a95e5e..e62eb652e7c8c 100644
--- a/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h
+++ b/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h
@@ -13,6 +13,10 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 17
+#include <compare>
+#endif 
+
 struct TrivialSortable {
     int value;
     TEST_CONSTEXPR TrivialSortable() : value(0) {}
@@ -75,4 +79,29 @@ static_assert(std::is_trivially_copyable<TrivialSortableWithComp>::value, "");
 static_assert(!std::is_trivially_copyable<NonTrivialSortable>::value, "");
 static_assert(!std::is_trivially_copyable<NonTrivialSortableWithComp>::value, "");
 
+#if TEST_STD_VER > 17
+struct TracedCopy {
+  int copied = 0;
+  int data   = 0;
+
+  constexpr TracedCopy() = default;
+  constexpr TracedCopy(int i) : data(i) {}
+  constexpr TracedCopy(const TracedCopy& other) : copied(other.copied + 1), data(other.data) {}
+
+  constexpr TracedCopy(TracedCopy&& other)            = delete;
+  constexpr TracedCopy& operator=(TracedCopy&& other) = delete;
+
+  constexpr TracedCopy& operator=(const TracedCopy& other) {
+    copied = other.copied + 1;
+    data   = other.data;
+    return *this;
+  }
+
+  constexpr bool copiedOnce() const { return copied == 1; }
+
+  constexpr bool operator==(const TracedCopy& o) const { return data == o.data; }
+  constexpr auto operator<=>(const TracedCopy& o) const { return data <=> o.data; }
+};
+#endif // TEST_STD_VER > 17
+
 #endif // SORTABLE_HELPERS_H

diff  --git a/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp b/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
index 5e518b3378933..623f75a1fd010 100644
--- a/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
@@ -37,7 +37,7 @@ static_assert(std::is_same_v<in_out_result<int, long>, move_backward_result<int,
 // static_assert(std::is_same_v<in_out_result<int, long>, replace_copy_if_result<int, long>>);
 // static_assert(std::is_same_v<in_out_result<int, long>, reverse_copy_result<int, long>>);
 // static_assert(std::is_same_v<in_out_result<int, long>, rotate_copy_result<int, long>>);
-// static_assert(std::is_same_v<in_out_result<int, long>, set_
diff erence_result<int, long>>);
+static_assert(std::is_same_v<in_out_result<int, long>, set_
diff erence_result<int, long>>);
 static_assert(std::is_same_v<in_out_result<int, long>, unary_transform_result<int, long>>);
 static_assert(std::is_same_v<in_out_result<int, long>, uninitialized_copy_result<int, long>>);
 static_assert(std::is_same_v<in_out_result<int, long>, uninitialized_copy_n_result<int, long>>);

diff  --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
index dda8f71e56914..7e0029ba580c4 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
@@ -132,7 +132,7 @@ static_assert(test(std::ranges::reverse, a));
 //static_assert(test(std::ranges::sample, a, a, 5));
 //static_assert(test(std::ranges::search, a, a));
 //static_assert(test(std::ranges::search_n, a, 10, 42));
-//static_assert(test(std::ranges::set_
diff erence, a, a, a));
+static_assert(test(std::ranges::set_
diff erence, a, a, a));
 //static_assert(test(std::ranges::set_intersection, a, a, a));
 //static_assert(test(std::ranges::set_symmetric_
diff erence, a, a, a));
 //static_assert(test(std::ranges::set_union, a, a, a));


        


More information about the libcxx-commits mailing list