@@ -132,13 +132,13 @@ fn intersection(left: &mut [u32], right: &[u32]) -> usize {
132
132
}
133
133
134
134
/// Intersect twos sorted arrays `left` and `right` and outputs the
135
- /// resulting array inline in both ` left` and `right` .
135
+ /// resulting array in left.
136
136
///
137
137
/// Condition for match is that the value stored in left is less than or equal to
138
138
/// the value in right and that the distance to the previous token is lte to the slop.
139
139
///
140
140
/// Returns the length of the intersection
141
- fn intersection_with_distance ( left : & mut [ u32 ] , right : & [ u32 ] , slop : u32 ) -> usize {
141
+ fn intersection_with_slop ( left : & mut [ u32 ] , right : & [ u32 ] , slop : u32 ) -> usize {
142
142
let mut left_index = 0 ;
143
143
let mut right_index = 0 ;
144
144
let mut count = 0 ;
@@ -235,80 +235,52 @@ impl<TPostings: Postings> PhraseScorer<TPostings> {
235
235
}
236
236
237
237
fn phrase_exists ( & mut self ) -> bool {
238
- let intersection_len = if self . has_slop ( ) {
239
- self . compute_match_with_slop ( )
240
- } else {
241
- self . compute_match ( )
242
- } ;
238
+ let intersection_len = self . compute_phrase_match ( ) ;
243
239
intersection_exists ( & self . left [ ..intersection_len] , & self . right [ ..] )
244
240
}
245
241
246
242
fn compute_phrase_count ( & mut self ) -> u32 {
247
- let intersection_len = if self . has_slop ( ) {
248
- self . compute_match_with_slop ( )
249
- } else {
250
- self . compute_match ( )
251
- } ;
243
+ let intersection_len = self . compute_phrase_match ( ) ;
252
244
intersection_count ( & self . left [ ..intersection_len] , & self . right [ ..] ) as u32
253
245
}
254
246
255
- /// Computes match without slop.
256
- fn compute_match ( & mut self ) -> usize {
247
+ fn compute_phrase_match ( & mut self ) -> usize {
257
248
{
258
249
self . intersection_docset
259
250
. docset_mut_specialized ( 0 )
260
251
. positions ( & mut self . left ) ;
261
252
}
262
253
let mut intersection_len = self . left . len ( ) ;
263
- for i in 1 ..self . num_terms - 1 {
254
+ let end_term = if self . has_slop ( ) {
255
+ self . num_terms
256
+ } else {
257
+ self . num_terms - 1
258
+ } ;
259
+ for i in 1 ..end_term {
264
260
{
265
261
self . intersection_docset
266
262
. docset_mut_specialized ( i)
267
263
. positions ( & mut self . right ) ;
268
264
}
269
- intersection_len = intersection ( & mut self . left [ ..intersection_len] , & self . right [ ..] ) ;
265
+ intersection_len = if self . has_slop ( ) {
266
+ intersection_with_slop (
267
+ & mut self . left [ ..intersection_len] ,
268
+ & self . right [ ..] ,
269
+ self . slop ,
270
+ )
271
+ } else {
272
+ intersection ( & mut self . left [ ..intersection_len] , & self . right [ ..] )
273
+ } ;
270
274
if intersection_len == 0 {
271
275
return 0 ;
272
276
}
273
277
}
274
-
275
278
self . intersection_docset
276
279
. docset_mut_specialized ( self . num_terms - 1 )
277
280
. positions ( & mut self . right ) ;
278
281
intersection_len
279
282
}
280
283
281
- // Computes match with slop.
282
- fn compute_match_with_slop ( & mut self ) -> usize {
283
- {
284
- self . intersection_docset
285
- . docset_mut_specialized ( 0 )
286
- . positions ( & mut self . left ) ;
287
- }
288
- let mut intersection_len = self . left . len ( ) ;
289
- // We'll increment the values to be equal to the next match in the right array to achieve ordered slop.
290
- for i in 1 ..self . num_terms {
291
- {
292
- self . intersection_docset
293
- . docset_mut_specialized ( i)
294
- . positions ( & mut self . right ) ;
295
- }
296
- intersection_len = intersection_with_distance (
297
- & mut self . left [ ..intersection_len] ,
298
- & self . right [ ..] ,
299
- self . slop ,
300
- ) ;
301
- // Update the left to be equal to the right. Merge the initial left.
302
- if intersection_len == 0 {
303
- return 0 ;
304
- }
305
- }
306
- self . intersection_docset
307
- . docset_mut_specialized ( self . num_terms - 1 )
308
- . positions ( & mut self . left ) ;
309
- intersection_len
310
- }
311
-
312
284
fn has_slop ( & self ) -> bool {
313
285
self . slop > 0
314
286
}
@@ -353,7 +325,7 @@ impl<TPostings: Postings> Scorer for PhraseScorer<TPostings> {
353
325
354
326
#[ cfg( test) ]
355
327
mod tests {
356
- use super :: { intersection, intersection_count, intersection_with_distance } ;
328
+ use super :: { intersection, intersection_count, intersection_with_slop } ;
357
329
358
330
fn test_intersection_sym ( left : & [ u32 ] , right : & [ u32 ] , expected : & [ u32 ] ) {
359
331
test_intersection_aux ( left, right, expected, 0 ) ;
@@ -372,7 +344,7 @@ mod tests {
372
344
}
373
345
let mut right_vec = Vec :: from ( right) ;
374
346
let right_mut = & mut right_vec[ ..] ;
375
- let count = intersection_with_distance ( left_mut, right_mut, slop) ;
347
+ let count = intersection_with_slop ( left_mut, right_mut, slop) ;
376
348
assert_eq ! ( & left_mut[ ..count] , expected) ;
377
349
}
378
350
@@ -403,7 +375,7 @@ mod tests {
403
375
let left_mut = & mut left_vec[ ..] ;
404
376
let mut right_vec = Vec :: from ( right) ;
405
377
let right_mut = & mut right_vec[ ..] ;
406
- let count = intersection_with_distance ( left_mut, right_mut, slop) ;
378
+ let count = intersection_with_slop ( left_mut, right_mut, slop) ;
407
379
assert_eq ! ( & left_mut[ ..count] , expected_left) ;
408
380
}
409
381
0 commit comments