Skip to content

Commit

Permalink
[libcxx][ranges] implement std::ranges::set_difference
Browse files Browse the repository at this point in the history
implement `std::ranges::set_difference`
reused classic std::set_difference
added unit tests

Differential Revision: https://reviews.llvm.org/D128983
  • Loading branch information
huixie90 committed Jul 8, 2022
1 parent 8b75671 commit 1cdec6c
Show file tree
Hide file tree
Showing 15 changed files with 804 additions and 72 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/RangesAlgorithms.csv
Expand Up @@ -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_difference,Hui Xie,n/a,Not started
Merge,set_difference,Hui Xie,`D128983 <https://llvm.org/D128983>`,✅
Merge,set_intersection,Hui Xie,n/a,Not started
Merge,set_symmetric_difference,Hui Xie,n/a,Not started
Merge,set_union,Hui Xie,n/a,Not started
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -108,6 +108,7 @@ set(files
__algorithm/ranges_replace.h
__algorithm/ranges_replace_if.h
__algorithm/ranges_reverse.h
__algorithm/ranges_set_difference.h
__algorithm/ranges_sort.h
__algorithm/ranges_stable_sort.h
__algorithm/ranges_swap_ranges.h
Expand Down
22 changes: 21 additions & 1 deletion libcxx/include/__algorithm/make_projected.h
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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
Expand Down
104 changes: 104 additions & 0 deletions libcxx/include/__algorithm/ranges_set_difference.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_difference.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_difference_result = in_out_result<_InIter, _OutIter>;

namespace __set_difference {

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_difference_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_difference(
__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_difference_result<borrowed_iterator_t<_Range1>, _OutIter>
operator()(
_Range1&& __range1,
_Range2&& __range2,
_OutIter __result,
_Comp __comp = {},
_Proj1 __proj1 = {},
_Proj2 __proj2 = {}) const {
auto __ret = std::__set_difference(
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_difference

inline namespace __cpo {
inline constexpr auto set_difference = __set_difference::__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
81 changes: 44 additions & 37 deletions libcxx/include/__algorithm/set_difference.h
Expand Up @@ -13,58 +13,65 @@
#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
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
__set_difference(_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_difference(
_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_difference(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
{
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
return _VSTD::__set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_difference(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2,
_OutputIterator __result,
_Compare __comp) {
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
return std::__set_difference<_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_difference(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
{
return _VSTD::set_difference(__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_difference(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2,
_OutputIterator __result) {
return std::__set_difference(
__first1,
__last1,
__first2,
__last2,
__result,
__less<typename iterator_traits<_InputIterator1>::value_type,
typename iterator_traits<_InputIterator2>::value_type>()).second;
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
19 changes: 19 additions & 0 deletions libcxx/include/algorithm
Expand Up @@ -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_difference_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_difference_result<I1, O>
set_difference(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_difference_result<borrowed_iterator_t<R1>, O>
set_difference(R1&& r1, R2&& r2, O result,
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
}
Expand Down Expand Up @@ -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_difference.h>
#include <__algorithm/ranges_sort.h>
#include <__algorithm/ranges_stable_sort.h>
#include <__algorithm/ranges_swap_ranges.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap.in
Expand Up @@ -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_difference { private header "__algorithm/ranges_set_difference.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" }
Expand Down
Expand Up @@ -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_difference(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
//(void)std::ranges::set_difference(a, b, first2, Less(&copies)); assert(copies == 0);
(void)std::ranges::set_difference(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
(void)std::ranges::set_difference(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_difference(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
Expand Down
Expand Up @@ -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_difference(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::set_difference(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
(void)std::ranges::set_difference(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
(void)std::ranges::set_difference(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_difference(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
Expand Down
1 change: 1 addition & 0 deletions libcxx/test/libcxx/private_headers.verify.cpp
Expand Up @@ -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_difference.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_difference.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'}}
Expand Down

0 comments on commit 1cdec6c

Please sign in to comment.