find_end.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_FIND_END_OF_H
  10. #define _LIBCPP___ALGORITHM_FIND_END_OF_H
  11. #include <__algorithm/comp.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__algorithm/search.h>
  14. #include <__config>
  15. #include <__functional/identity.h>
  16. #include <__functional/invoke.h>
  17. #include <__iterator/advance.h>
  18. #include <__iterator/iterator_traits.h>
  19. #include <__iterator/next.h>
  20. #include <__iterator/reverse_iterator.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 inline _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __find_end_impl(
  35. _Iter1 __first1,
  36. _Sent1 __last1,
  37. _Iter2 __first2,
  38. _Sent2 __last2,
  39. _Pred& __pred,
  40. _Proj1& __proj1,
  41. _Proj2& __proj2,
  42. forward_iterator_tag,
  43. forward_iterator_tag) {
  44. // modeled after search algorithm
  45. _Iter1 __match_first = _IterOps<_AlgPolicy>::next(__first1, __last1); // __last1 is the "default" answer
  46. _Iter1 __match_last = __match_first;
  47. if (__first2 == __last2)
  48. return pair<_Iter1, _Iter1>(__match_last, __match_last);
  49. while (true) {
  50. while (true) {
  51. if (__first1 == __last1) // if source exhausted return last correct answer (or __last1 if never found)
  52. return pair<_Iter1, _Iter1>(__match_first, __match_last);
  53. if (std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  54. break;
  55. ++__first1;
  56. }
  57. // *__first1 matches *__first2, now match elements after here
  58. _Iter1 __m1 = __first1;
  59. _Iter2 __m2 = __first2;
  60. while (true) {
  61. if (++__m2 == __last2) { // Pattern exhaused, record answer and search for another one
  62. __match_first = __first1;
  63. __match_last = ++__m1;
  64. ++__first1;
  65. break;
  66. }
  67. if (++__m1 == __last1) // Source exhausted, return last answer
  68. return pair<_Iter1, _Iter1>(__match_first, __match_last);
  69. // mismatch, restart with a new __first
  70. if (!std::__invoke(__pred, std::__invoke(__proj1, *__m1), std::__invoke(__proj2, *__m2))) {
  71. ++__first1;
  72. break;
  73. } // else there is a match, check next elements
  74. }
  75. }
  76. }
  77. template < class _IterOps,
  78. class _Pred,
  79. class _Iter1,
  80. class _Sent1,
  81. class _Iter2,
  82. class _Sent2,
  83. class _Proj1,
  84. class _Proj2>
  85. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter1 __find_end(
  86. _Iter1 __first1,
  87. _Sent1 __sent1,
  88. _Iter2 __first2,
  89. _Sent2 __sent2,
  90. _Pred& __pred,
  91. _Proj1& __proj1,
  92. _Proj2& __proj2,
  93. bidirectional_iterator_tag,
  94. bidirectional_iterator_tag) {
  95. auto __last1 = _IterOps::next(__first1, __sent1);
  96. auto __last2 = _IterOps::next(__first2, __sent2);
  97. // modeled after search algorithm (in reverse)
  98. if (__first2 == __last2)
  99. return __last1; // Everything matches an empty sequence
  100. _Iter1 __l1 = __last1;
  101. _Iter2 __l2 = __last2;
  102. --__l2;
  103. while (true) {
  104. // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
  105. while (true) {
  106. if (__first1 == __l1) // return __last1 if no element matches *__first2
  107. return __last1;
  108. if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2)))
  109. break;
  110. }
  111. // *__l1 matches *__l2, now match elements before here
  112. _Iter1 __m1 = __l1;
  113. _Iter2 __m2 = __l2;
  114. while (true) {
  115. if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
  116. return __m1;
  117. if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
  118. return __last1;
  119. // if there is a mismatch, restart with a new __l1
  120. if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(__proj2, *--__m2))) {
  121. break;
  122. } // else there is a match, check next elements
  123. }
  124. }
  125. }
  126. template < class _AlgPolicy,
  127. class _Pred,
  128. class _Iter1,
  129. class _Sent1,
  130. class _Iter2,
  131. class _Sent2,
  132. class _Proj1,
  133. class _Proj2>
  134. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter1 __find_end(
  135. _Iter1 __first1,
  136. _Sent1 __sent1,
  137. _Iter2 __first2,
  138. _Sent2 __sent2,
  139. _Pred& __pred,
  140. _Proj1& __proj1,
  141. _Proj2& __proj2,
  142. random_access_iterator_tag,
  143. random_access_iterator_tag) {
  144. typedef typename iterator_traits<_Iter1>::difference_type _D1;
  145. auto __last1 = _IterOps<_AlgPolicy>::next(__first1, __sent1);
  146. auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __sent2);
  147. // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
  148. auto __len2 = __last2 - __first2;
  149. if (__len2 == 0)
  150. return __last1;
  151. auto __len1 = __last1 - __first1;
  152. if (__len1 < __len2)
  153. return __last1;
  154. const _Iter1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
  155. _Iter1 __l1 = __last1;
  156. _Iter2 __l2 = __last2;
  157. --__l2;
  158. while (true) {
  159. while (true) {
  160. if (__s == __l1)
  161. return __last1;
  162. if (std::__invoke(__pred, std::__invoke(__proj1, *--__l1), std::__invoke(__proj2, *__l2)))
  163. break;
  164. }
  165. _Iter1 __m1 = __l1;
  166. _Iter2 __m2 = __l2;
  167. while (true) {
  168. if (__m2 == __first2)
  169. return __m1;
  170. // no need to check range on __m1 because __s guarantees we have enough source
  171. if (!std::__invoke(__pred, std::__invoke(__proj1, *--__m1), std::__invoke(*--__m2))) {
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  178. _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_end_classic(
  179. _ForwardIterator1 __first1,
  180. _ForwardIterator1 __last1,
  181. _ForwardIterator2 __first2,
  182. _ForwardIterator2 __last2,
  183. _BinaryPredicate& __pred) {
  184. auto __proj = __identity();
  185. return std::__find_end_impl<_ClassicAlgPolicy>(
  186. __first1,
  187. __last1,
  188. __first2,
  189. __last2,
  190. __pred,
  191. __proj,
  192. __proj,
  193. typename iterator_traits<_ForwardIterator1>::iterator_category(),
  194. typename iterator_traits<_ForwardIterator2>::iterator_category())
  195. .first;
  196. }
  197. template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
  198. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1 find_end(
  199. _ForwardIterator1 __first1,
  200. _ForwardIterator1 __last1,
  201. _ForwardIterator2 __first2,
  202. _ForwardIterator2 __last2,
  203. _BinaryPredicate __pred) {
  204. return std::__find_end_classic(__first1, __last1, __first2, __last2, __pred);
  205. }
  206. template <class _ForwardIterator1, class _ForwardIterator2>
  207. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator1
  208. find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) {
  209. return std::find_end(__first1, __last1, __first2, __last2, __equal_to());
  210. }
  211. _LIBCPP_END_NAMESPACE_STD
  212. #endif // _LIBCPP___ALGORITHM_FIND_END_OF_H