find_end.h 7.6 KB

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