diff --git a/src/rank/criterion/sum_of_typos.rs b/src/rank/criterion/sum_of_typos.rs index 400650ad2..18a199113 100644 --- a/src/rank/criterion/sum_of_typos.rs +++ b/src/rank/criterion/sum_of_typos.rs @@ -66,7 +66,10 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document::from_unsorted_matches(DocumentId(0), matches) + Document { + id: DocumentId(0), + matches: matches, + } }; let doc1 = { @@ -86,7 +89,10 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document::from_unsorted_matches(DocumentId(1), matches) + Document { + id: DocumentId(1), + matches: matches, + } }; let lhs = sum_matches_typos(&doc0.matches); @@ -117,7 +123,10 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document::from_unsorted_matches(DocumentId(0), matches) + Document { + id: DocumentId(0), + matches: matches, + } }; let doc1 = { @@ -130,7 +139,10 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document::from_unsorted_matches(DocumentId(1), matches) + Document { + id: DocumentId(1), + matches: matches, + } }; let lhs = sum_matches_typos(&doc0.matches); @@ -161,7 +173,10 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document::from_unsorted_matches(DocumentId(0), matches) + Document { + id: DocumentId(0), + matches: matches, + } }; let doc1 = { @@ -174,7 +189,10 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document::from_unsorted_matches(DocumentId(1), matches) + Document { + id: DocumentId(1), + matches: matches, + } }; let lhs = sum_matches_typos(&doc0.matches); diff --git a/src/rank/criterion/words_proximity.rs b/src/rank/criterion/words_proximity.rs index 7fe3102d3..41f4b49b8 100644 --- a/src/rank/criterion/words_proximity.rs +++ b/src/rank/criterion/words_proximity.rs @@ -90,8 +90,8 @@ mod tests { // soup -> of = 8 // + of -> the = 1 // + the -> day = 8 (not 1) - let matches = Matches::from_unsorted(matches.to_vec()); - assert_eq!(matches_proximity(&matches), 17); + let matches = Matches::from_unsorted_matches(matches.to_vec()); + assert_eq!(matches_proximity(matches), 17); } #[test] @@ -118,8 +118,7 @@ mod tests { // soup -> of = 1 // + of -> the = 1 // + the -> day = 1 - let matches = Matches::from_unsorted(matches.to_vec()); - assert_eq!(matches_proximity(&matches), 3); + assert_eq!(matches_proximity(matches), 3); } } @@ -153,8 +152,6 @@ mod bench { matches.push(match_); } - let matches = Matches::from_unsorted(matches.to_vec()); - bench.iter(|| { let proximity = matches_proximity(&matches); test::black_box(move || proximity) diff --git a/src/rank/mod.rs b/src/rank/mod.rs index ad91830bf..0d3f538d0 100644 --- a/src/rank/mod.rs +++ b/src/rank/mod.rs @@ -23,19 +23,28 @@ pub struct Document { } impl Document { - pub fn new(id: DocumentId, match_: Match) -> Self { + pub fn new(doc: DocumentId, match_: Match) -> Self { let matches = SetBuf::new_unchecked(vec![match_]); - Self::from_matches(id, matches) + Self::from_matches(doc, matches) } pub fn from_matches(id: DocumentId, matches: SetBuf) -> Self { - let matches = Matches::new(matches); + let mut last = 0; + let mut slices = vec![0]; + for group in GroupBy::new(&matches, match_query_index) { + let index = last + group.len(); + slices.push(index); + last = index; + } + + let matches = Matches { matches, slices }; Self { id, matches } } - pub fn from_unsorted_matches(id: DocumentId, matches: Vec) -> Self { - let matches = Matches::from_unsorted(matches); - Self { id, matches } + pub fn from_unsorted_matches(doc: DocumentId, mut matches: Vec) -> Self { + matches.sort_unstable(); + let matches = SetBuf::new_unchecked(matches); + Self::from_matches(doc, matches) } } @@ -46,25 +55,6 @@ pub struct Matches { } impl Matches { - pub fn new(matches: SetBuf) -> Matches { - let mut last = 0; - let mut slices = vec![0]; - - for group in GroupBy::new(&matches, match_query_index) { - let index = last + group.len(); - slices.push(index); - last = index; - } - - Matches { matches, slices } - } - - pub fn from_unsorted(mut matches: Vec) -> Matches { - matches.sort_unstable(); - let matches = SetBuf::new_unchecked(matches); - Matches::new(matches) - } - pub fn query_index_groups(&self) -> QueryIndexGroups { QueryIndexGroups { matches: &self.matches, @@ -85,7 +75,7 @@ impl<'a, 'b> Iterator for QueryIndexGroups<'a, 'b> { self.windows.next().map(|range| { match *range { [left, right] => &self.matches[left..right], - _ => unreachable!(), + _ => unreachable!() } }) }