From d21406a93908b2b3ed8cd57b2182996b43cf7806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 6 Jan 2019 11:23:21 +0100 Subject: [PATCH] feat: Allow Matches to be constructed --- src/rank/criterion/sum_of_typos.rs | 30 ++++--------------- src/rank/criterion/words_proximity.rs | 9 ++++-- src/rank/mod.rs | 42 +++++++++++++++++---------- 3 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/rank/criterion/sum_of_typos.rs b/src/rank/criterion/sum_of_typos.rs index 18a199113..400650ad2 100644 --- a/src/rank/criterion/sum_of_typos.rs +++ b/src/rank/criterion/sum_of_typos.rs @@ -66,10 +66,7 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document { - id: DocumentId(0), - matches: matches, - } + Document::from_unsorted_matches(DocumentId(0), matches) }; let doc1 = { @@ -89,10 +86,7 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document { - id: DocumentId(1), - matches: matches, - } + Document::from_unsorted_matches(DocumentId(1), matches) }; let lhs = sum_matches_typos(&doc0.matches); @@ -123,10 +117,7 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document { - id: DocumentId(0), - matches: matches, - } + Document::from_unsorted_matches(DocumentId(0), matches) }; let doc1 = { @@ -139,10 +130,7 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document { - id: DocumentId(1), - matches: matches, - } + Document::from_unsorted_matches(DocumentId(1), matches) }; let lhs = sum_matches_typos(&doc0.matches); @@ -173,10 +161,7 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document { - id: DocumentId(0), - matches: matches, - } + Document::from_unsorted_matches(DocumentId(0), matches) }; let doc1 = { @@ -189,10 +174,7 @@ mod tests { word_area: WordArea::new_faillible(0, 6) }, ]; - Document { - id: DocumentId(1), - matches: matches, - } + Document::from_unsorted_matches(DocumentId(1), 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 41f4b49b8..7fe3102d3 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(matches.to_vec()); - assert_eq!(matches_proximity(matches), 17); + let matches = Matches::from_unsorted(matches.to_vec()); + assert_eq!(matches_proximity(&matches), 17); } #[test] @@ -118,7 +118,8 @@ mod tests { // soup -> of = 1 // + of -> the = 1 // + the -> day = 1 - assert_eq!(matches_proximity(matches), 3); + let matches = Matches::from_unsorted(matches.to_vec()); + assert_eq!(matches_proximity(&matches), 3); } } @@ -152,6 +153,8 @@ 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 0d3f538d0..ad91830bf 100644 --- a/src/rank/mod.rs +++ b/src/rank/mod.rs @@ -23,28 +23,19 @@ pub struct Document { } impl Document { - pub fn new(doc: DocumentId, match_: Match) -> Self { + pub fn new(id: DocumentId, match_: Match) -> Self { let matches = SetBuf::new_unchecked(vec![match_]); - Self::from_matches(doc, matches) + Self::from_matches(id, matches) } pub fn from_matches(id: DocumentId, matches: SetBuf) -> Self { - 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 }; + let matches = Matches::new(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) + pub fn from_unsorted_matches(id: DocumentId, matches: Vec) -> Self { + let matches = Matches::from_unsorted(matches); + Self { id, matches } } } @@ -55,6 +46,25 @@ 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, @@ -75,7 +85,7 @@ impl<'a, 'b> Iterator for QueryIndexGroups<'a, 'b> { self.windows.next().map(|range| { match *range { [left, right] => &self.matches[left..right], - _ => unreachable!() + _ => unreachable!(), } }) }