@@ -2,9 +2,9 @@ use num_traits::ToPrimitive;
2
2
use std:: { cmp:: min, fmt} ;
3
3
4
4
pub struct LongestCommonSubstring < ' a > {
5
- text : & ' a [ u8 ] ,
6
- start : usize ,
7
- len : usize ,
5
+ pub text : & ' a [ u8 ] ,
6
+ pub start : usize ,
7
+ pub len : usize ,
8
8
}
9
9
10
10
impl < ' a > fmt:: Debug for LongestCommonSubstring < ' a > {
@@ -18,16 +18,6 @@ impl<'a> LongestCommonSubstring<'a> {
18
18
pub fn as_bytes ( & self ) -> & [ u8 ] {
19
19
& self . text [ self . start ..self . start + self . len ]
20
20
}
21
-
22
- #[ inline( always) ]
23
- pub fn start ( & self ) -> usize {
24
- self . start
25
- }
26
-
27
- #[ inline( always) ]
28
- pub fn len ( & self ) -> usize {
29
- self . len
30
- }
31
21
}
32
22
33
23
/// Returns the number of bytes `a` and `b` have in common.
@@ -52,7 +42,7 @@ pub fn longest_substring_match<'a, Index>(
52
42
needle : & [ u8 ] ,
53
43
) -> LongestCommonSubstring < ' a >
54
44
where
55
- Index : num_traits :: ToPrimitive ,
45
+ Index : ToPrimitive ,
56
46
{
57
47
macro_rules! sa {
58
48
( $x: expr) => {
@@ -167,6 +157,11 @@ where
167
157
text : & ' a [ u8 ] ,
168
158
}
169
159
160
+ pub trait StringIndex < ' a > {
161
+ /// Returns the longest substring that matches `needle` in text
162
+ fn longest_substring_match ( & self , needle : & [ u8 ] ) -> LongestCommonSubstring < ' a > ;
163
+ }
164
+
170
165
impl < ' a , Index > SuffixArray < ' a , Index >
171
166
where
172
167
Index : ToPrimitive ,
@@ -176,19 +171,29 @@ where
176
171
Self { sa, text }
177
172
}
178
173
179
- /// Returns the longest
180
- pub fn longest_substring_match ( & self , needle : & [ u8 ] ) -> LongestCommonSubstring < ' a > {
181
- longest_substring_match ( self . text , & self . sa [ ..] , needle)
182
- }
183
-
184
174
/// Return (text, sa), giving back ownership of `sa`
185
175
pub fn into_parts ( self ) -> ( & ' a [ u8 ] , Vec < Index > ) {
186
176
( self . text , self . sa )
187
177
}
188
178
179
+ /// Verifies that this suffix array is sorted.
189
180
pub fn verify ( & self ) -> Result < ( ) , NotSorted > {
190
181
verify ( self . text , & self . sa [ ..] )
191
182
}
183
+
184
+ /// Returns a reference to the text
185
+ pub fn text ( & self ) -> & [ u8 ] {
186
+ return self . text ;
187
+ }
188
+ }
189
+
190
+ impl < ' a , Index > StringIndex < ' a > for SuffixArray < ' a , Index >
191
+ where
192
+ Index : ToPrimitive ,
193
+ {
194
+ fn longest_substring_match ( & self , needle : & [ u8 ] ) -> LongestCommonSubstring < ' a > {
195
+ longest_substring_match ( self . text , & self . sa [ ..] , needle)
196
+ }
192
197
}
193
198
194
199
#[ cfg( test) ]
0 commit comments