Skip to content

Commit

Permalink
[libc++][ranges] implement std::ranges::set_intersection
Browse files Browse the repository at this point in the history
implement `std::ranges::set_intersection` by reusing the classic `std::set_intersenction`
added unit tests

Differential Revision: https://reviews.llvm.org/D129233
  • Loading branch information
huixie90 committed Jul 11, 2022
1 parent c13d04e commit 96b674f
Show file tree
Hide file tree
Showing 15 changed files with 817 additions and 75 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/RangesAlgorithms.csv
Expand Up @@ -62,7 +62,7 @@ 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,`D128983 <https://llvm.org/D128983>`,✅
Merge,set_intersection,Hui Xie,n/a,Not started
Merge,set_intersection,Hui Xie,`D129233 <https://llvm.org/D129233>`,✅
Merge,set_symmetric_difference,Hui Xie,n/a,Not started
Merge,set_union,Hui Xie,n/a,Not started
Permutation,remove,Nikolas Klauser,`D128618 <https://llvm.org/D128618>`_,✅
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -113,6 +113,7 @@ set(files
__algorithm/ranges_replace_if.h
__algorithm/ranges_reverse.h
__algorithm/ranges_set_difference.h
__algorithm/ranges_set_intersection.h
__algorithm/ranges_sort.h
__algorithm/ranges_sort_heap.h
__algorithm/ranges_stable_sort.h
Expand Down
8 changes: 8 additions & 0 deletions libcxx/include/__algorithm/iterator_operations.h
Expand Up @@ -13,6 +13,7 @@
#include <__iterator/advance.h>
#include <__iterator/distance.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/next.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand All @@ -24,6 +25,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
struct _RangesIterOps {
static constexpr auto advance = ranges::advance;
static constexpr auto distance = ranges::distance;
static constexpr auto next = ranges::next;
};
#endif

Expand All @@ -40,6 +42,12 @@ struct _StdIterOps {
return std::distance(__first, __last);
}

template <class _Iterator>
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_AFTER_CXX11
_Iterator next(_Iterator, _Iterator __last) {
return __last;
}

};

_LIBCPP_END_NAMESPACE_STD
Expand Down
117 changes: 117 additions & 0 deletions libcxx/include/__algorithm/ranges_set_intersection.h
@@ -0,0 +1,117 @@
//===----------------------------------------------------------------------===//
//
// 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_INTERSECTION_H
#define _LIBCPP___ALGORITHM_RANGES_SET_INTERSECTION_H

#include <__algorithm/in_in_out_result.h>
#include <__algorithm/iterator_operations.h>
#include <__algorithm/make_projected.h>
#include <__algorithm/set_intersection.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 <__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 _InIter1, class _InIter2, class _OutIter>
using set_intersection_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;

namespace __set_intersection {

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_intersection_result<_InIter1, _InIter2, _OutIter> operator()(
_InIter1 __first1,
_Sent1 __last1,
_InIter2 __first2,
_Sent2 __last2,
_OutIter __result,
_Comp __comp = {},
_Proj1 __proj1 = {},
_Proj2 __proj2 = {}) const {
auto __ret = std::__set_intersection<_RangesIterOps>(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
std::move(__result),
ranges::__make_projected_comp(__comp, __proj1, __proj2));
return {std::move(__ret.in1), std::move(__ret.in2), std::move(__ret.out)};
}

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_intersection_result<borrowed_iterator_t<_Range1>,
borrowed_iterator_t<_Range2>,
_OutIter>
operator()(
_Range1&& __range1,
_Range2&& __range2,
_OutIter __result,
_Comp __comp = {},
_Proj1 __proj1 = {},
_Proj2 __proj2 = {}) const {
auto __ret = std::__set_intersection<_RangesIterOps>(
ranges::begin(__range1),
ranges::end(__range1),
ranges::begin(__range2),
ranges::end(__range2),
std::move(__result),
ranges::__make_projected_comp(__comp, __proj1, __proj2));
return {std::move(__ret.in1), std::move(__ret.in2), std::move(__ret.out)};
}
};

} // namespace __set_intersection

inline namespace __cpo {
inline constexpr auto set_intersection = __set_intersection::__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_INTERSECTION_H
100 changes: 65 additions & 35 deletions libcxx/include/__algorithm/set_intersection.h
Expand Up @@ -11,57 +11,87 @@

#include <__algorithm/comp.h>
#include <__algorithm/comp_ref_type.h>
#include <__algorithm/iterator_operations.h>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__utility/move.h>

#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_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
{
while (__first1 != __last1 && __first2 != __last2)
{
if (__comp(*__first1, *__first2))
++__first1;
else
{
if (!__comp(*__first2, *__first1))
{
*__result = *__first1;
++__result;
++__first1;
}
++__first2;
}
template <class _InIter1, class _InIter2, class _OutIter>
struct __set_intersection_result {
_InIter1 in1;
_InIter2 in2;
_OutIter out;

// need a constructor as C++03 aggregate init is hard
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
__set_intersection_result(_InIter1&& __inIter1, _InIter2&& __inIter2, _OutIter&& __outIter)
: in1(std::move(__inIter1)), in2(std::move(__inIter2)), out(std::move(__outIter)) {}
};

template < class _IterOper, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 __set_intersection_result<_InIter1, _InIter2, _OutIter>
__set_intersection(
_InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
while (__first1 != __last1 && __first2 != __last2) {
if (__comp(*__first1, *__first2))
++__first1;
else {
if (!__comp(*__first2, *__first1)) {
*__result = *__first1;
++__result;
++__first1;
}
++__first2;
}
return __result;
}

return __set_intersection_result<_InIter1, _InIter2, _OutIter>(
_IterOper::next(std::move(__first1), std::move(__last1)),
_IterOper::next(std::move(__first2), std::move(__last2)),
std::move(__result));
}

template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator
set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
{
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
return _VSTD::__set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_intersection(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2,
_OutputIterator __result,
_Compare __comp) {
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
return std::__set_intersection<_StdIterOps, _Comp_ref>(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
std::move(__result),
__comp)
.out;
}

template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator
set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
{
return _VSTD::set_intersection(__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_intersection(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2,
_OutputIterator __result) {
return std::__set_intersection<_StdIterOps>(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
std::move(__result),
__less<typename iterator_traits<_InputIterator1>::value_type,
typename iterator_traits<_InputIterator2>::value_type>())
.out;
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
20 changes: 20 additions & 0 deletions libcxx/include/algorithm
Expand Up @@ -604,6 +604,25 @@ namespace ranges {
set_difference(R1&& r1, R2&& r2, O result,
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
template<class I1, class I2, class O>
using set_intersection_result = in_in_out_result<I1, I2, 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_intersection_result<I1, I2, O>
set_intersection(I1 first1, S1 last1, I2 first2, S2 last2, O result,
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // 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_intersection_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
set_intersection(R1&& r1, R2&& r2, O result,
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
}
constexpr bool // constexpr in C++20
Expand Down Expand Up @@ -1378,6 +1397,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_replace_if.h>
#include <__algorithm/ranges_reverse.h>
#include <__algorithm/ranges_set_difference.h>
#include <__algorithm/ranges_set_intersection.h>
#include <__algorithm/ranges_sort.h>
#include <__algorithm/ranges_sort_heap.h>
#include <__algorithm/ranges_stable_sort.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap.in
Expand Up @@ -352,6 +352,7 @@ module std [system] {
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_set_intersection { private header "__algorithm/ranges_set_intersection.h" }
module ranges_sort { private header "__algorithm/ranges_sort.h" }
module ranges_sort_heap { private header "__algorithm/ranges_sort_heap.h" }
module ranges_stable_sort { private header "__algorithm/ranges_stable_sort.h" }
Expand Down
Expand Up @@ -201,8 +201,8 @@ constexpr bool all_the_algorithms()
//(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_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_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);
//(void)std::ranges::set_symmetric_difference(a, b, first2, Less(&copies)); assert(copies == 0);
//(void)std::ranges::set_union(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0);
Expand Down
Expand Up @@ -192,8 +192,8 @@ constexpr bool all_the_algorithms()
//(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_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_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);
//(void)std::ranges::set_symmetric_difference(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::set_union(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 @@ -150,6 +150,7 @@ END-SCRIPT
#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_set_intersection.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_intersection.h'}}
#include <__algorithm/ranges_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_sort.h'}}
#include <__algorithm/ranges_sort_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_sort_heap.h'}}
#include <__algorithm/ranges_stable_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_stable_sort.h'}}
Expand Down
Expand Up @@ -205,27 +205,11 @@ constexpr void testImpl() {

// check that ranges::dangling is returned for non-borrowed_range and iterator_t is returned for borrowed_range
{
struct NonBorrowedRange {
int* data_;
size_t size_;

// TODO: std::ranges::merge 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 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};
std::array<int, 7> out;
std::same_as<merge_result<std::array<int, 4>::iterator, std::ranges::dangling, int*>> decltype(auto) result =
std::ranges::merge(r1, NonBorrowedRange{r2.data(), r2.size()}, out.data());
std::ranges::merge(r1, NonBorrowedRange<In2>{r2.data(), r2.size()}, out.data());
assert(base(result.in1) == r1.end());
assert(base(result.out) == out.data() + out.size());
assert(std::ranges::equal(out, std::array{2, 3, 3, 4, 6, 7, 9}));
Expand Down

0 comments on commit 96b674f

Please sign in to comment.