Skip to content

Commit

Permalink
Revert "[libc++] Implement ranges::find_end, ranges::search{, _n}"
Browse files Browse the repository at this point in the history
This reverts commit 76a7651.
  • Loading branch information
philnik777 committed Jul 13, 2022
1 parent 3ec2b2f commit 1f04759
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 1,829 deletions.
6 changes: 3 additions & 3 deletions libcxx/docs/Status/RangesAlgorithms.csv
Expand Up @@ -23,9 +23,9 @@ Search,max_element,Nikolas Klauser,`D117523 <https://llvm.org/D117523>`_,✅
Search,minmax_element,Nikolas Klauser,`D120637 <https://llvm.org/D120637>`_,✅
Search,count,Nikolas Klauser,`D121523 <https://llvm.org/D121523>`_,✅
Search,count_if,Nikolas Klauser,`D121523 <https://llvm.org/D121523>`_,✅
Search,search,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,
Search,search_n,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,
Search,find_end,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,
Search,search,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,Under review
Search,search_n,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,Under review
Search,find_end,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,Under review
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>`_,✅
Expand Down
3 changes: 0 additions & 3 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -83,7 +83,6 @@ set(files
__algorithm/ranges_fill.h
__algorithm/ranges_fill_n.h
__algorithm/ranges_find.h
__algorithm/ranges_find_end.h
__algorithm/ranges_find_first_of.h
__algorithm/ranges_find_if.h
__algorithm/ranges_find_if_not.h
Expand Down Expand Up @@ -130,8 +129,6 @@ set(files
__algorithm/ranges_reverse.h
__algorithm/ranges_reverse_copy.h
__algorithm/ranges_rotate_copy.h
__algorithm/ranges_search.h
__algorithm/ranges_search_n.h
__algorithm/ranges_set_difference.h
__algorithm/ranges_set_intersection.h
__algorithm/ranges_set_union.h
Expand Down
181 changes: 51 additions & 130 deletions libcxx/include/__algorithm/find_end.h
Expand Up @@ -11,69 +11,44 @@
#define _LIBCPP___ALGORITHM_FIND_END_OF_H

#include <__algorithm/comp.h>
#include <__algorithm/iterator_operations.h>
#include <__algorithm/search.h>
#include <__config>
#include <__functional/identity.h>
#include <__iterator/advance.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/next.h>
#include <__iterator/reverse_iterator.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 _IterOps,
class _Iter1,
class _Sent1,
class _Iter2,
class _Sent2,
class _Pred,
class _Proj1,
class _Proj2>
_LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_Iter1, _Iter1> __find_end_impl(
_Iter1 __first1,
_Sent1 __last1,
_Iter2 __first2,
_Sent2 __last2,
_Pred& __pred,
_Proj1& __proj1,
_Proj2& __proj2,
forward_iterator_tag,
forward_iterator_tag) {
template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __pred, forward_iterator_tag,
forward_iterator_tag) {
// modeled after search algorithm
_Iter1 __match_first = _IterOps::next(__first1, __last1); // __last1 is the "default" answer
_Iter1 __match_last = __match_first;
_ForwardIterator1 __r = __last1; // __last1 is the "default" answer
if (__first2 == __last2)
return pair<_Iter1, _Iter1>(__match_last, __match_last);
return __r;
while (true) {
while (true) {
if (__first1 == __last1) // if source exhausted return last correct answer (or __last1 if never found)
return pair<_Iter1, _Iter1>(__match_first, __match_last);
if (std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
if (__first1 == __last1) // if source exhausted return last correct answer
return __r; // (or __last1 if never found)
if (__pred(*__first1, *__first2))
break;
++__first1;
}
// *__first1 matches *__first2, now match elements after here
_Iter1 __m1 = __first1;
_Iter2 __m2 = __first2;
_ForwardIterator1 __m1 = __first1;
_ForwardIterator2 __m2 = __first2;
while (true) {
if (++__m2 == __last2) { // Pattern exhaused, record answer and search for another one
__match_first = __first1;
__match_last = ++__m1;
__r = __first1;
++__first1;
break;
}
if (++__m1 == __last1) // Source exhausted, return last answer
return pair<_Iter1, _Iter1>(__match_first, __match_last);
// mismatch, restart with a new __first
if (!std::__invoke(__pred, std::__invoke(__proj1, *__m1), std::__invoke(__proj2, *__m2)))
return __r;
if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
{
++__first1;
break;
Expand All @@ -82,146 +57,92 @@ _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_Iter1, _Iter1>
}
}

template <
class _IterOps,
class _Pred,
class _Iter1,
class _Sent1,
class _Iter2,
class _Sent2,
class _Proj1,
class _Proj2>
_LIBCPP_CONSTEXPR_AFTER_CXX17 _Iter1 __find_end(
_Iter1 __first1,
_Sent1 __sent1,
_Iter2 __first2,
_Sent2 __sent2,
_Pred& __pred,
_Proj1& __proj1,
_Proj2& __proj2,
bidirectional_iterator_tag,
bidirectional_iterator_tag) {
auto __last1 = _IterOps::next(__first1, __sent1);
auto __last2 = _IterOps::next(__first2, __sent2);
template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1 __find_end(
_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1, _BidirectionalIterator2 __first2,
_BidirectionalIterator2 __last2, _BinaryPredicate __pred, bidirectional_iterator_tag, bidirectional_iterator_tag) {
// modeled after search algorithm (in reverse)
if (__first2 == __last2)
return __last1; // Everything matches an empty sequence
_Iter1 __l1 = __last1;
_Iter2 __l2 = __last2;
_BidirectionalIterator1 __l1 = __last1;
_BidirectionalIterator2 __l2 = __last2;
--__l2;
while (true) {
// Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
while (true) {
if (__first1 == __l1) // return __last1 if no element matches *__first2
return __last1;
if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2)))
if (__pred(*--__l1, *__l2))
break;
}
// *__l1 matches *__l2, now match elements before here
_Iter1 __m1 = __l1;
_Iter2 __m2 = __l2;
_BidirectionalIterator1 __m1 = __l1;
_BidirectionalIterator2 __m2 = __l2;
while (true) {
if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
return __m1;
if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
return __last1;

// if there is a mismatch, restart with a new __l1
if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(__proj2, *--__m2)))
if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
{
break;
} // else there is a match, check next elements
}
}
}

template <
class _IterOps,
class _Pred,
class _Iter1,
class _Sent1,
class _Iter2,
class _Sent2,
class _Proj1,
class _Proj2>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _Iter1 __find_end(
_Iter1 __first1,
_Sent1 __sent1,
_Iter2 __first2,
_Sent2 __sent2,
_Pred& __pred,
_Proj1& __proj1,
_Proj2& __proj2,
random_access_iterator_tag,
random_access_iterator_tag) {
typedef typename iterator_traits<_Iter1>::difference_type _D1;
auto __last1 = _IterOps::next(__first1, __sent1);
auto __last2 = _IterOps::next(__first2, __sent2);
template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 __find_end(
_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
_RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, random_access_iterator_tag) {
typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
// Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
auto __len2 = __last2 - __first2;
_D2 __len2 = __last2 - __first2;
if (__len2 == 0)
return __last1;
auto __len1 = __last1 - __first1;
_D1 __len1 = __last1 - __first1;
if (__len1 < __len2)
return __last1;
const _Iter1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
_Iter1 __l1 = __last1;
_Iter2 __l2 = __last2;
const _RandomAccessIterator1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
_RandomAccessIterator1 __l1 = __last1;
_RandomAccessIterator2 __l2 = __last2;
--__l2;
while (true) {
while (true) {
if (__s == __l1)
return __last1;
if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2)))
if (__pred(*--__l1, *__l2))
break;
}
_Iter1 __m1 = __l1;
_Iter2 __m2 = __l2;
_RandomAccessIterator1 __m1 = __l1;
_RandomAccessIterator2 __m2 = __l2;
while (true) {
if (__m2 == __first2)
return __m1;
// no need to check range on __m1 because __s guarantees we have enough source
if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(*--__m2))) {
if (!__pred(*--__m1, *--__m2)) {
break;
}
}
}
}

template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
_ForwardIterator1 __find_end_classic(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate& __pred) {
auto __proj = __identity();
return std::__find_end_impl<_StdIterOps>(
__first1,
__last1,
__first2,
__last2,
__pred,
__proj,
__proj,
typename iterator_traits<_ForwardIterator1>::iterator_category(),
typename iterator_traits<_ForwardIterator2>::iterator_category())
.first;
}

template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
_ForwardIterator1 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __pred) {
return std::__find_end_classic(__first1, __last1, __first2, __last2, __pred);
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __pred) {
return _VSTD::__find_end<_BinaryPredicate&>(
__first1, __last1, __first2, __last2, __pred, typename iterator_traits<_ForwardIterator1>::iterator_category(),
typename iterator_traits<_ForwardIterator2>::iterator_category());
}

template <class _ForwardIterator1, class _ForwardIterator2>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
_ForwardIterator1 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2) {
using __v1 = typename iterator_traits<_ForwardIterator1>::value_type;
using __v2 = typename iterator_traits<_ForwardIterator2>::value_type;
return std::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) {
typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
1 change: 0 additions & 1 deletion libcxx/include/__algorithm/iterator_operations.h
Expand Up @@ -39,7 +39,6 @@ struct _IterOps<_RangeAlgPolicy> {
static constexpr auto __iter_move = ranges::iter_move;
static constexpr auto iter_swap = ranges::iter_swap;
static constexpr auto next = ranges::next;
static constexpr auto __advance_to = ranges::advance;
};
#endif

Expand Down

0 comments on commit 1f04759

Please sign in to comment.