search_n.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ALGORITHM_SEARCH_N_H
  10. #define _LIBCPP___ALGORITHM_SEARCH_N_H
  11. #include <__algorithm/comp.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__config>
  14. #include <__functional/identity.h>
  15. #include <__functional/invoke.h>
  16. #include <__iterator/advance.h>
  17. #include <__iterator/concepts.h>
  18. #include <__iterator/distance.h>
  19. #include <__iterator/iterator_traits.h>
  20. #include <__ranges/concepts.h>
  21. #include <__type_traits/is_callable.h>
  22. #include <__utility/convert_to_integral.h>
  23. #include <__utility/pair.h>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj>
  29. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_forward_impl(
  30. _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
  31. if (__count <= 0)
  32. return std::make_pair(__first, __first);
  33. while (true) {
  34. // Find first element in sequence that matchs __value, with a mininum of loop checks
  35. while (true) {
  36. if (__first == __last) { // return __last if no element matches __value
  37. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  38. return std::make_pair(__first, __first);
  39. }
  40. if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
  41. break;
  42. ++__first;
  43. }
  44. // *__first matches __value, now match elements after here
  45. _Iter __m = __first;
  46. _SizeT __c(0);
  47. while (true) {
  48. if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
  49. return std::make_pair(__first, ++__m);
  50. if (++__m == __last) { // Otherwise if source exhaused, pattern not found
  51. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  52. return std::make_pair(__first, __first);
  53. }
  54. // if there is a mismatch, restart with a new __first
  55. if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
  56. __first = __m;
  57. ++__first;
  58. break;
  59. } // else there is a match, check next elements
  60. }
  61. }
  62. }
  63. template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj, class _DiffT>
  64. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __search_n_random_access_impl(
  65. _Iter __first, _Sent __last, _SizeT __count, const _Type& __value, _Pred& __pred, _Proj& __proj, _DiffT __size1) {
  66. using difference_type = typename iterator_traits<_Iter>::difference_type;
  67. if (__count == 0)
  68. return std::make_pair(__first, __first);
  69. if (__size1 < static_cast<_DiffT>(__count)) {
  70. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  71. return std::make_pair(__first, __first);
  72. }
  73. const auto __s = __first + __size1 - difference_type(__count - 1); // Start of pattern match can't go beyond here
  74. while (true) {
  75. // Find first element in sequence that matchs __value, with a mininum of loop checks
  76. while (true) {
  77. if (__first >= __s) { // return __last if no element matches __value
  78. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  79. return std::make_pair(__first, __first);
  80. }
  81. if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
  82. break;
  83. ++__first;
  84. }
  85. // *__first matches __value_, now match elements after here
  86. auto __m = __first;
  87. _SizeT __c(0);
  88. while (true) {
  89. if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
  90. return std::make_pair(__first, __first + _DiffT(__count));
  91. ++__m; // no need to check range on __m because __s guarantees we have enough source
  92. // if there is a mismatch, restart with a new __first
  93. if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value)) {
  94. __first = __m;
  95. ++__first;
  96. break;
  97. } // else there is a match, check next elements
  98. }
  99. }
  100. }
  101. template <class _Iter,
  102. class _Sent,
  103. class _DiffT,
  104. class _Type,
  105. class _Pred,
  106. class _Proj,
  107. __enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
  108. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
  109. __search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
  110. return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
  111. __first, __last, __count, __value, __pred, __proj, __last - __first);
  112. }
  113. template <class _Iter1,
  114. class _Sent1,
  115. class _DiffT,
  116. class _Type,
  117. class _Pred,
  118. class _Proj,
  119. __enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
  120. !__has_random_access_iterator_category<_Iter1>::value,
  121. int> = 0>
  122. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
  123. __search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
  124. return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
  125. }
  126. template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
  127. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator search_n(
  128. _ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) {
  129. static_assert(
  130. __is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value, "BinaryPredicate has to be callable");
  131. auto __proj = __identity();
  132. return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
  133. }
  134. template <class _ForwardIterator, class _Size, class _Tp>
  135. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  136. search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
  137. return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
  138. }
  139. _LIBCPP_END_NAMESPACE_STD
  140. #endif // _LIBCPP___ALGORITHM_SEARCH_N_H