search.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_H
  10. #define _LIBCPP___ALGORITHM_SEARCH_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/iterator_traits.h>
  19. #include <__type_traits/enable_if.h>
  20. #include <__type_traits/is_callable.h>
  21. #include <__utility/pair.h>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. template <class _AlgPolicy,
  27. class _Iter1,
  28. class _Sent1,
  29. class _Iter2,
  30. class _Sent2,
  31. class _Pred,
  32. class _Proj1,
  33. class _Proj2>
  34. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_forward_impl(
  35. _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  36. if (__first2 == __last2)
  37. return std::make_pair(__first1, __first1); // Everything matches an empty sequence
  38. while (true) {
  39. // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
  40. while (true) {
  41. if (__first1 == __last1) { // return __last1 if no element matches *__first2
  42. _IterOps<_AlgPolicy>::__advance_to(__first1, __last1);
  43. return std::make_pair(__first1, __first1);
  44. }
  45. if (std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  46. break;
  47. ++__first1;
  48. }
  49. // *__first1 matches *__first2, now match elements after here
  50. _Iter1 __m1 = __first1;
  51. _Iter2 __m2 = __first2;
  52. while (true) {
  53. if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
  54. return std::make_pair(__first1, ++__m1);
  55. if (++__m1 == __last1) { // Otherwise if source exhaused, pattern not found
  56. return std::make_pair(__m1, __m1);
  57. }
  58. // if there is a mismatch, restart with a new __first1
  59. if (!std::__invoke(__pred, std::__invoke(__proj1, *__m1), std::__invoke(__proj2, *__m2))) {
  60. ++__first1;
  61. break;
  62. } // else there is a match, check next elements
  63. }
  64. }
  65. }
  66. template <class _AlgPolicy,
  67. class _Iter1,
  68. class _Sent1,
  69. class _Iter2,
  70. class _Sent2,
  71. class _Pred,
  72. class _Proj1,
  73. class _Proj2,
  74. class _DiffT1,
  75. class _DiffT2>
  76. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_random_access_impl(
  77. _Iter1 __first1,
  78. _Sent1 __last1,
  79. _Iter2 __first2,
  80. _Sent2 __last2,
  81. _Pred& __pred,
  82. _Proj1& __proj1,
  83. _Proj2& __proj2,
  84. _DiffT1 __size1,
  85. _DiffT2 __size2) {
  86. const _Iter1 __s = __first1 + __size1 - _DiffT1(__size2 - 1); // Start of pattern match can't go beyond here
  87. while (true) {
  88. while (true) {
  89. if (__first1 == __s) {
  90. _IterOps<_AlgPolicy>::__advance_to(__first1, __last1);
  91. return std::make_pair(__first1, __first1);
  92. }
  93. if (std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  94. break;
  95. ++__first1;
  96. }
  97. _Iter1 __m1 = __first1;
  98. _Iter2 __m2 = __first2;
  99. while (true) {
  100. if (++__m2 == __last2)
  101. return std::make_pair(__first1, __first1 + _DiffT1(__size2));
  102. ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
  103. if (!std::__invoke(__pred, std::__invoke(__proj1, *__m1), std::__invoke(__proj2, *__m2))) {
  104. ++__first1;
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. template <class _Iter1,
  111. class _Sent1,
  112. class _Iter2,
  113. class _Sent2,
  114. class _Pred,
  115. class _Proj1,
  116. class _Proj2,
  117. __enable_if_t<__has_random_access_iterator_category<_Iter1>::value &&
  118. __has_random_access_iterator_category<_Iter2>::value,
  119. int> = 0>
  120. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_impl(
  121. _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  122. auto __size2 = __last2 - __first2;
  123. if (__size2 == 0)
  124. return std::make_pair(__first1, __first1);
  125. auto __size1 = __last1 - __first1;
  126. if (__size1 < __size2) {
  127. return std::make_pair(__last1, __last1);
  128. }
  129. return std::__search_random_access_impl<_ClassicAlgPolicy>(
  130. __first1, __last1, __first2, __last2, __pred, __proj1, __proj2, __size1, __size2);
  131. }
  132. template <
  133. class _Iter1,
  134. class _Sent1,
  135. class _Iter2,
  136. class _Sent2,
  137. class _Pred,
  138. class _Proj1,
  139. class _Proj2,
  140. __enable_if_t<__has_forward_iterator_category<_Iter1>::value && __has_forward_iterator_category<_Iter2>::value &&
  141. !(__has_random_access_iterator_category<_Iter1>::value &&
  142. __has_random_access_iterator_category<_Iter2>::value),
  143. int> = 0>
  144. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_impl(
  145. _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  146. return std::__search_forward_impl<_ClassicAlgPolicy>(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
  147. }
  148. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  149. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
  150. search(_ForwardIterator1 __first1,
  151. _ForwardIterator1 __last1,
  152. _ForwardIterator2 __first2,
  153. _ForwardIterator2 __last2,
  154. _BinaryPredicate __pred) {
  155. static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value,
  156. "BinaryPredicate has to be callable");
  157. auto __proj = __identity();
  158. return std::__search_impl(__first1, __last1, __first2, __last2, __pred, __proj, __proj).first;
  159. }
  160. template <class _ForwardIterator1, class _ForwardIterator2>
  161. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
  162. search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) {
  163. return std::search(__first1, __last1, __first2, __last2, __equal_to());
  164. }
  165. #if _LIBCPP_STD_VER >= 17
  166. template <class _ForwardIterator, class _Searcher>
  167. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  168. search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher& __s) {
  169. return __s(__f, __l).first;
  170. }
  171. #endif
  172. _LIBCPP_END_NAMESPACE_STD
  173. #endif // _LIBCPP___ALGORITHM_SEARCH_H