Skip to content

Commit

Permalink
[libc++][ranges] implement std::ranges::includes
Browse files Browse the repository at this point in the history
implement `std::ranges::includes` and delegate to `std::includes`

Differential Revision: https://reviews.llvm.org/D130116
  • Loading branch information
huixie90 committed Jul 22, 2022
1 parent 0f6364b commit c559964
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 63 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/RangesAlgorithms.csv
Expand Up @@ -29,7 +29,7 @@ Search,find_end,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,✅
Read-only,is_partitioned,Nikolas Klauser,`D124440 <https://llvm.org/D124440>`_,✅
Read-only,is_sorted,Nikolas Klauser,`D125608 <https://llvm.org/D125608>`_,✅
Read-only,is_sorted_until,Nikolas Klauser,`D125608 <https://llvm.org/D125608>`_,✅
Read-only,includes,Nikolas Klauser,n/a,Not started
Read-only,includes,Hui Xie,`D130116 <https://llvm.org/D130116>`,✅
Read-only,is_heap,Nikolas Klauser,n/a,Not started
Read-only,is_heap_until,Nikolas Klauser,n/a,Not started
Read-only,clamp,Nikolas Klauser,`D126193 <https://llvm.org/D126193>`_,Under review
Expand Down
58 changes: 29 additions & 29 deletions libcxx/include/__algorithm/includes.h
Expand Up @@ -13,48 +13,48 @@
#include <__algorithm/comp_ref_type.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>
_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
_Compare __comp)
{
for (; __first2 != __last2; ++__first1)
{
if (__first1 == __last1 || __comp(*__first2, *__first1))
return false;
if (!__comp(*__first1, *__first2))
++__first2;
}
return true;
template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Comp>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
__includes(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp&& __comp) {
for (; __first2 != __last2; ++__first1) {
if (__first1 == __last1 || __comp(*__first2, *__first1))
return false;
if (!__comp(*__first1, *__first2))
++__first2;
}
return true;
}

template <class _InputIterator1, class _InputIterator2, class _Compare>
_LIBCPP_NODISCARD_EXT inline
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
bool
includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
_Compare __comp)
{
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
return _VSTD::__includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool includes(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2,
_Compare __comp) {
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
return std::__includes(
std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), static_cast<_Comp_ref>(__comp));
}

template <class _InputIterator1, class _InputIterator2>
_LIBCPP_NODISCARD_EXT inline
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
bool
includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
{
return _VSTD::includes(__first1, __last1, __first2, __last2,
__less<typename iterator_traits<_InputIterator1>::value_type,
typename iterator_traits<_InputIterator2>::value_type>());
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
return std::includes(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
__less<typename iterator_traits<_InputIterator1>::value_type,
typename iterator_traits<_InputIterator2>::value_type>());
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
59 changes: 38 additions & 21 deletions libcxx/include/__algorithm/ranges_includes.h
Expand Up @@ -9,8 +9,8 @@
#ifndef _LIBCPP___ALGORITHM_RANGES_INCLUDES_H
#define _LIBCPP___ALGORITHM_RANGES_INCLUDES_H

#include <__algorithm/make_projected.h>
#include <__algorithm/includes.h>
#include <__algorithm/make_projected.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/invoke.h>
Expand All @@ -35,29 +35,46 @@ namespace ranges {
namespace __includes {

struct __fn {

template <input_iterator _Iter1, sentinel_for<_Iter1> _Sent1, input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
class _Proj1 = identity, class _Proj2 = identity,
indirect_strict_weak_order<projected<_Iter1, _Proj1>, projected<_Iter2, _Proj2>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr
bool operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp __comp = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
// TODO: implement
(void)__first1; (void)__last1; (void)__first2; (void)__last2; (void)__comp; (void)__proj1; (void)__proj2;
return {};
template <
input_iterator _Iter1,
sentinel_for<_Iter1> _Sent1,
input_iterator _Iter2,
sentinel_for<_Iter2> _Sent2,
class _Proj1 = identity,
class _Proj2 = identity,
indirect_strict_weak_order<projected<_Iter1, _Proj1>, projected<_Iter2, _Proj2>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
_Iter1 __first1,
_Sent1 __last1,
_Iter2 __first2,
_Sent2 __last2,
_Comp __comp = {},
_Proj1 __proj1 = {},
_Proj2 __proj2 = {}) const {
return std::__includes(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
ranges::__make_projected_comp(__comp, __proj1, __proj2));
}

template <input_range _Range1, input_range _Range2, class _Proj1 = identity, class _Proj2 = identity,
indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
projected<iterator_t<_Range2>, _Proj2>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr
bool operator()(_Range1&& __range1, _Range2&& __range2, _Comp __comp = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
// TODO: implement
(void)__range1; (void)__range2; (void)__comp; (void)__proj1; (void)__proj2;
return {};
template <
input_range _Range1,
input_range _Range2,
class _Proj1 = identity,
class _Proj2 = identity,
indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>, projected<iterator_t<_Range2>, _Proj2>>
_Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
_Range1&& __range1, _Range2&& __range2, _Comp __comp = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
return std::__includes(
ranges::begin(__range1),
ranges::end(__range1),
ranges::begin(__range2),
ranges::end(__range2),
ranges::__make_projected_comp(__comp, __proj1, __proj2));
}

};

} // namespace __includes
Expand Down
15 changes: 15 additions & 0 deletions libcxx/include/algorithm
Expand Up @@ -801,6 +801,20 @@ namespace ranges {
constexpr set_union_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
set_union(R1&& r1, R2&& r2, 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,
class Proj1 = identity, class Proj2 = identity,
indirect_strict_weak_order<projected<I1, Proj1>, projected<I2, Proj2>> Comp =
ranges::less>
constexpr bool includes(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {}); // Since C++20
template<input_range R1, input_range R2, class Proj1 = identity,
class Proj2 = identity,
indirect_strict_weak_order<projected<iterator_t<R1>, Proj1>,
projected<iterator_t<R2>, Proj2>> Comp = ranges::less>
constexpr bool includes(R1&& r1, R2&& r2, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {}); // Since C++20
}
constexpr bool // constexpr in C++20
Expand Down Expand Up @@ -1551,6 +1565,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_find_if_not.h>
#include <__algorithm/ranges_for_each.h>
#include <__algorithm/ranges_for_each_n.h>
#include <__algorithm/ranges_includes.h>
#include <__algorithm/ranges_is_partitioned.h>
#include <__algorithm/ranges_is_sorted.h>
#include <__algorithm/ranges_is_sorted_until.h>
Expand Down
Expand Up @@ -124,8 +124,8 @@ constexpr bool all_the_algorithms()
//(void)std::ranges::generate(first, last, NullaryValue(&copies)); assert(copies == 0);
//(void)std::ranges::generate(a, NullaryValue(&copies)); assert(copies == 0);
//(void)std::ranges::generate_n(first, count, NullaryValue(&copies)); assert(copies == 0);
//(void)std::ranges::includes(first, last, first2, last2, Less(&copies)); assert(copies == 0);
//(void)std::ranges::includes(a, b, Less(&copies)); assert(copies == 0);
(void)std::ranges::includes(first, last, first2, last2, Less(&copies)); assert(copies == 0);
(void)std::ranges::includes(a, b, Less(&copies)); assert(copies == 0);
//(void)std::ranges::is_heap(first, last, Less(&copies)); assert(copies == 0);
//(void)std::ranges::is_heap(a, Less(&copies)); assert(copies == 0);
//(void)std::ranges::is_heap_until(first, last, Less(&copies)); assert(copies == 0);
Expand Down
Expand Up @@ -107,8 +107,8 @@ constexpr bool all_the_algorithms()
(void)std::ranges::for_each(first, last, UnaryVoid(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::for_each(a, UnaryVoid(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::for_each_n(first, count, UnaryVoid(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::includes(first, last, first2, last2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::includes(a, b, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
(void)std::ranges::includes(first, last, first2, last2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
(void)std::ranges::includes(a, b, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::is_heap(first, last, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::is_heap(a, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::is_heap_until(first, last, Less(), Proj(&copies)); assert(copies == 0);
Expand Down

0 comments on commit c559964

Please sign in to comment.