feat: Allow Matches to be constructed

This commit is contained in:
Clément Renault 2019-01-06 11:23:21 +01:00
parent 039a9a4cc7
commit d21406a939
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
3 changed files with 38 additions and 43 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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<Match>) -> 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<Match>) -> Self {
matches.sort_unstable();
let matches = SetBuf::new_unchecked(matches);
Self::from_matches(doc, matches)
pub fn from_unsorted_matches(id: DocumentId, matches: Vec<Match>) -> Self {
let matches = Matches::from_unsorted(matches);
Self { id, matches }
}
}
@ -55,6 +46,25 @@ pub struct Matches {
}
impl Matches {
pub fn new(matches: SetBuf<Match>) -> 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<Match>) -> 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!(),
}
})
}