search.h 8.2 KB

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