search_n.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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
  30. pair<_Iter, _Iter> __search_n_forward_impl(_Iter __first, _Sent __last,
  31. _SizeT __count,
  32. const _Type& __value,
  33. _Pred& __pred,
  34. _Proj& __proj) {
  35. if (__count <= 0)
  36. return std::make_pair(__first, __first);
  37. while (true) {
  38. // Find first element in sequence that matchs __value, with a mininum of loop checks
  39. while (true) {
  40. if (__first == __last) { // return __last if no element matches __value
  41. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  42. return std::make_pair(__first, __first);
  43. }
  44. if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
  45. break;
  46. ++__first;
  47. }
  48. // *__first matches __value, now match elements after here
  49. _Iter __m = __first;
  50. _SizeT __c(0);
  51. while (true) {
  52. if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
  53. return std::make_pair(__first, ++__m);
  54. if (++__m == __last) { // Otherwise if source exhaused, pattern not found
  55. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  56. return std::make_pair(__first, __first);
  57. }
  58. // if there is a mismatch, restart with a new __first
  59. if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value))
  60. {
  61. __first = __m;
  62. ++__first;
  63. break;
  64. } // else there is a match, check next elements
  65. }
  66. }
  67. }
  68. template <class _AlgPolicy, class _Pred, class _Iter, class _Sent, class _SizeT, class _Type, class _Proj, class _DiffT>
  69. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  70. std::pair<_Iter, _Iter> __search_n_random_access_impl(_Iter __first, _Sent __last,
  71. _SizeT __count,
  72. const _Type& __value,
  73. _Pred& __pred,
  74. _Proj& __proj,
  75. _DiffT __size1) {
  76. using difference_type = typename iterator_traits<_Iter>::difference_type;
  77. if (__count == 0)
  78. return std::make_pair(__first, __first);
  79. if (__size1 < static_cast<_DiffT>(__count)) {
  80. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  81. return std::make_pair(__first, __first);
  82. }
  83. const auto __s = __first + __size1 - difference_type(__count - 1); // Start of pattern match can't go beyond here
  84. while (true) {
  85. // Find first element in sequence that matchs __value, with a mininum of loop checks
  86. while (true) {
  87. if (__first >= __s) { // return __last if no element matches __value
  88. _IterOps<_AlgPolicy>::__advance_to(__first, __last);
  89. return std::make_pair(__first, __first);
  90. }
  91. if (std::__invoke(__pred, std::__invoke(__proj, *__first), __value))
  92. break;
  93. ++__first;
  94. }
  95. // *__first matches __value_, now match elements after here
  96. auto __m = __first;
  97. _SizeT __c(0);
  98. while (true) {
  99. if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
  100. return std::make_pair(__first, __first + _DiffT(__count));
  101. ++__m; // no need to check range on __m because __s guarantees we have enough source
  102. // if there is a mismatch, restart with a new __first
  103. if (!std::__invoke(__pred, std::__invoke(__proj, *__m), __value))
  104. {
  105. __first = __m;
  106. ++__first;
  107. break;
  108. } // else there is a match, check next elements
  109. }
  110. }
  111. }
  112. template <class _Iter, class _Sent,
  113. class _DiffT,
  114. class _Type,
  115. class _Pred,
  116. class _Proj>
  117. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  118. pair<_Iter, _Iter> __search_n_impl(_Iter __first, _Sent __last,
  119. _DiffT __count,
  120. const _Type& __value,
  121. _Pred& __pred,
  122. _Proj& __proj,
  123. __enable_if_t<__has_random_access_iterator_category<_Iter>::value>* = nullptr) {
  124. return std::__search_n_random_access_impl<_ClassicAlgPolicy>(__first, __last,
  125. __count,
  126. __value,
  127. __pred,
  128. __proj,
  129. __last - __first);
  130. }
  131. template <class _Iter1, class _Sent1,
  132. class _DiffT,
  133. class _Type,
  134. class _Pred,
  135. class _Proj>
  136. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  137. pair<_Iter1, _Iter1> __search_n_impl(_Iter1 __first, _Sent1 __last,
  138. _DiffT __count,
  139. const _Type& __value,
  140. _Pred& __pred,
  141. _Proj& __proj,
  142. __enable_if_t<__has_forward_iterator_category<_Iter1>::value
  143. && !__has_random_access_iterator_category<_Iter1>::value>* = nullptr) {
  144. return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last,
  145. __count,
  146. __value,
  147. __pred,
  148. __proj);
  149. }
  150. template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
  151. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  152. _ForwardIterator search_n(_ForwardIterator __first, _ForwardIterator __last,
  153. _Size __count,
  154. const _Tp& __value,
  155. _BinaryPredicate __pred) {
  156. static_assert(__is_callable<_BinaryPredicate, decltype(*__first), const _Tp&>::value,
  157. "BinaryPredicate has to be callable");
  158. auto __proj = __identity();
  159. return std::__search_n_impl(__first, __last, std::__convert_to_integral(__count), __value, __pred, __proj).first;
  160. }
  161. template <class _ForwardIterator, class _Size, class _Tp>
  162. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  163. _ForwardIterator search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value) {
  164. return std::search_n(__first, __last, std::__convert_to_integral(__count), __value, __equal_to());
  165. }
  166. _LIBCPP_END_NAMESPACE_STD
  167. #endif // _LIBCPP___ALGORITHM_SEARCH_N_H