search.h 8.3 KB

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