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