-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkmp.hpp
275 lines (245 loc) · 8.17 KB
/
kmp.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* @file kmp.hpp
* @brief
* @date 2019-5-11
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_ALGORITHM_KMP_HPP
#define KERBAL_ALGORITHM_KMP_HPP
#include <kerbal/compare/basic_compare.hpp>
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/container/vector.hpp>
#include <kerbal/iterator/iterator.hpp>
#include <kerbal/iterator/iterator_traits.hpp>
#include <kerbal/iterator/general_back_inserter.hpp>
#include <cstring>
#include <cstddef>
namespace kerbal
{
namespace algorithm
{
namespace detail
{
/**
* @brief Generate the next array.
*
* @param pattern_first Begin iterator direct to pattern string.
* @param pattern_last End iterator direct to pattern string.
* @param next_container Array, vector, etc. stored the result.
* @param pattern_first General back inserter of next_container
* (std::back_inserter(container) for STL compatible container,
* pointer direct to first element for C-array).
*
*/
template <
typename ForwardIterator,
typename NextContainer,
typename EqualPredict,
typename BackInsertIterator
>
KERBAL_CONSTEXPR14
ForwardIterator
longest_matched_suffix_prefix_helper(
ForwardIterator pattern_first, ForwardIterator pattern_last,
const NextContainer & next_container, EqualPredict equal_predict, BackInsertIterator back_inserter
)
{
typedef ForwardIterator pattern_iterator;
pattern_iterator i_pattern(pattern_first);
*back_inserter = 0; ++back_inserter;
if (i_pattern == pattern_last) {
return pattern_last;
}
std::size_t k = 0; pattern_iterator k_pattern(pattern_first);
*back_inserter = k; ++back_inserter;
++i_pattern;
while (i_pattern != pattern_last) {
while (true) {
if (equal_predict(*k_pattern, *i_pattern)) {
++k; ++k_pattern;
break;
}
if (k_pattern == pattern_first) {
break;
}
k = next_container[k]; k_pattern = kerbal::iterator::next(pattern_first, k);
}
*back_inserter = k; ++back_inserter;
++i_pattern;
}
return k_pattern;
}
} // namespace detail
template <
typename ForwardIterator,
typename NextContainer,
typename EqualPredict
>
KERBAL_CONSTEXPR14
ForwardIterator
longest_matched_suffix_prefix(
ForwardIterator pattern_first, ForwardIterator pattern_last,
NextContainer & next_container, EqualPredict equal_predict
)
{
return kerbal::algorithm::detail::longest_matched_suffix_prefix_helper(
pattern_first, pattern_last, next_container, equal_predict,
kerbal::iterator::general_inserter(next_container)
);
}
template <typename ForwardIterator, typename NextContainer>
KERBAL_CONSTEXPR14
ForwardIterator
longest_matched_suffix_prefix(
ForwardIterator pattern_first, ForwardIterator pattern_last,
NextContainer & next_container
)
{
typedef ForwardIterator pattern_iterator;
typedef typename kerbal::iterator::iterator_traits<pattern_iterator>::value_type T;
return kerbal::algorithm::longest_matched_suffix_prefix(
pattern_first, pattern_last, next_container, kerbal::compare::equal_to<T>()
);
}
namespace detail
{
template <
typename BidirectionalHostIterator,
typename ForwardPatternIterator,
typename EqualPredict,
typename NextContainer
>
KERBAL_CONSTEXPR14
BidirectionalHostIterator
kmp_helper(
BidirectionalHostIterator host_first, const BidirectionalHostIterator host_last,
const ForwardPatternIterator pattern_first, const ForwardPatternIterator pattern_last,
EqualPredict equal_predict, const NextContainer & lsp,
std::bidirectional_iterator_tag /*host_iterator_type*/, std::forward_iterator_tag /*pattern_iterator_type*/
)
{
typedef BidirectionalHostIterator host_iterator;
typedef ForwardPatternIterator pattern_iterator;
host_iterator it_host(host_first);
pattern_iterator it_pattern(pattern_first); std::size_t j = 0;
while (it_pattern != pattern_last) {
if (it_host == host_last) {
break;
}
if (equal_predict(*it_host, *it_pattern)) { // *i == *j
++it_host;
++it_pattern; ++j;
continue;
}
if (it_pattern == pattern_first) {
while (it_host != host_last) { // *i != *j
if (equal_predict(*it_host, *it_pattern)) {
break;
}
++it_host;
}
host_first = it_host;
} else {
kerbal::iterator::advance_at_most(host_first, j, host_last);
j = lsp[j]; it_pattern = kerbal::iterator::next(pattern_first, j);
host_first = kerbal::iterator::prev(it_host, j);
}
}
return (it_pattern == pattern_last) ? host_first : host_last;
}
} // namespace detail
template <
typename BidirectionalHostIterator,
typename ForwardPatternIterator,
typename EqualPredict,
typename NextContainer
>
KERBAL_CONSTEXPR14
BidirectionalHostIterator
kmp(
BidirectionalHostIterator host_first, BidirectionalHostIterator host_last,
ForwardPatternIterator pattern_first, ForwardPatternIterator pattern_last,
EqualPredict equal_predict, const NextContainer & lsp
)
{
return kerbal::algorithm::detail::kmp_helper(
host_first, host_last, pattern_first, pattern_last, equal_predict, lsp,
kerbal::iterator::iterator_category(host_first),
kerbal::iterator::iterator_category(pattern_first)
);
}
namespace detail
{
template <
typename BidirectionalHostIterator,
typename ForwardPatternIterator,
typename EqualPredict
>
BidirectionalHostIterator
kmp_lsp_buffer_agent(
BidirectionalHostIterator host_first, BidirectionalHostIterator host_last,
ForwardPatternIterator pattern_first, ForwardPatternIterator pattern_last, EqualPredict equal_predict,
std::forward_iterator_tag /*pattern_iterator_type*/
)
{
typedef ForwardPatternIterator pattern_iterator;
typedef typename kerbal::iterator::iterator_traits<pattern_iterator>::difference_type pattern_difference_type;
pattern_difference_type pattern_length(kerbal::iterator::distance(pattern_first, pattern_last));
if (pattern_length < 32) {
std::size_t lsp[32];
kerbal::algorithm::longest_matched_suffix_prefix(pattern_first, pattern_last, lsp, equal_predict);
return kerbal::algorithm::kmp(host_first, host_last, pattern_first, pattern_last, equal_predict, lsp);
} else {
kerbal::container::vector<std::size_t> lsp;
lsp.reserve(pattern_length);
kerbal::algorithm::longest_matched_suffix_prefix(pattern_first, pattern_last, lsp, equal_predict);
return kerbal::algorithm::kmp(host_first, host_last, pattern_first, pattern_last, equal_predict, lsp);
}
}
} // namespace detail
template <
typename BidirectionalHostIterator,
typename ForwardPatternIterator,
typename EqualPredict
>
BidirectionalHostIterator
kmp(
BidirectionalHostIterator host_first, BidirectionalHostIterator host_last,
ForwardPatternIterator pattern_first, ForwardPatternIterator pattern_last, EqualPredict equal_predict
)
{
return kerbal::algorithm::detail::kmp_lsp_buffer_agent(
host_first, host_last,
pattern_first, pattern_last,
equal_predict, kerbal::iterator::iterator_category(pattern_first)
);
}
template <typename BidirectionalHostIterator, typename ForwardPatternIterator>
BidirectionalHostIterator
kmp(
BidirectionalHostIterator host_first, BidirectionalHostIterator host_last,
ForwardPatternIterator pattern_first, ForwardPatternIterator pattern_last
)
{
typedef typename kerbal::iterator::iterator_traits<BidirectionalHostIterator>::value_type host_value_type;
return kerbal::algorithm::kmp(
host_first, host_last,
pattern_first, pattern_last,
kerbal::compare::equal_to<host_value_type>()
);
}
inline
const char * kmp(const char * host, const char * pattern)
{
return kerbal::algorithm::kmp(
host, host + std::strlen(host),
pattern, pattern + std::strlen(pattern)
);
}
} // namespace algorithm
} // namespace kerbal
#endif // KERBAL_ALGORITHM_KMP_HPP