feat: Prefer using ranges and not using unreachable!

This commit is contained in:
Clément Renault 2019-01-06 12:33:27 +01:00
parent 0d07af3caf
commit d899b86603
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 23 additions and 33 deletions

View File

@ -3,7 +3,8 @@ mod query_builder;
mod distinct_map; mod distinct_map;
use std::iter::FusedIterator; use std::iter::FusedIterator;
use std::slice::Windows; use std::slice::Iter;
use std::ops::Range;
use sdset::SetBuf; use sdset::SetBuf;
use group_by::GroupBy; use group_by::GroupBy;
@ -43,18 +44,19 @@ impl Document {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Matches { pub struct Matches {
matches: SetBuf<Match>, matches: SetBuf<Match>,
slices: Vec<usize>, slices: Vec<Range<usize>>,
} }
impl Matches { impl Matches {
pub fn new(matches: SetBuf<Match>) -> Matches { pub fn new(matches: SetBuf<Match>) -> Matches {
let mut last = 0; let mut last_end = 0;
let mut slices = vec![0]; let mut slices = Vec::new();
for group in GroupBy::new(&matches, match_query_index) { for group in GroupBy::new(&matches, match_query_index) {
let index = last + group.len(); let start = last_end;
slices.push(index); let end = last_end + group.len();
last = index; slices.push(Range { start, end });
last_end = end;
} }
Matches { matches, slices } Matches { matches, slices }
@ -69,7 +71,7 @@ impl Matches {
pub fn query_index_groups(&self) -> QueryIndexGroups { pub fn query_index_groups(&self) -> QueryIndexGroups {
QueryIndexGroups { QueryIndexGroups {
matches: &self.matches, matches: &self.matches,
windows: self.slices.windows(2), slices: self.slices.iter(),
} }
} }
@ -80,7 +82,7 @@ impl Matches {
pub struct QueryIndexGroups<'a, 'b> { pub struct QueryIndexGroups<'a, 'b> {
matches: &'a [Match], matches: &'a [Match],
windows: Windows<'b, usize>, slices: Iter<'b, Range<usize>>,
} }
impl<'a> Iterator for QueryIndexGroups<'a, '_> { impl<'a> Iterator for QueryIndexGroups<'a, '_> {
@ -88,17 +90,14 @@ impl<'a> Iterator for QueryIndexGroups<'a, '_> {
#[inline] #[inline]
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.windows.next().map(|range| { self.slices.next().cloned().map(|range| {
match *range { unsafe { self.matches.get_unchecked(range) }
[left, right] => &self.matches[left..right],
_ => unreachable!(),
}
}) })
} }
#[inline] #[inline]
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
self.windows.size_hint() self.slices.size_hint()
} }
#[inline] #[inline]
@ -108,22 +107,16 @@ impl<'a> Iterator for QueryIndexGroups<'a, '_> {
#[inline] #[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> { fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.windows.nth(n).map(|range| { self.slices.nth(n).cloned().map(|range| {
match *range { unsafe { self.matches.get_unchecked(range) }
[left, right] => &self.matches[left..right],
_ => unreachable!(),
}
}) })
} }
#[inline] #[inline]
fn last(self) -> Option<Self::Item> { fn last(self) -> Option<Self::Item> {
let (matches, windows) = (self.matches, self.windows); let (matches, slices) = (self.matches, self.slices);
windows.last().map(|range| { slices.last().cloned().map(|range| {
match *range { unsafe { matches.get_unchecked(range) }
[left, right] => &matches[left..right],
_ => unreachable!(),
}
}) })
} }
} }
@ -131,7 +124,7 @@ impl<'a> Iterator for QueryIndexGroups<'a, '_> {
impl ExactSizeIterator for QueryIndexGroups<'_, '_> { impl ExactSizeIterator for QueryIndexGroups<'_, '_> {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
self.windows.len() self.slices.len()
} }
} }
@ -140,11 +133,8 @@ impl FusedIterator for QueryIndexGroups<'_, '_> { }
impl DoubleEndedIterator for QueryIndexGroups<'_, '_> { impl DoubleEndedIterator for QueryIndexGroups<'_, '_> {
#[inline] #[inline]
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
self.windows.next_back().map(|range| { self.slices.next_back().cloned().map(|range| {
match *range { unsafe { self.matches.get_unchecked(range) }
[left, right] => &self.matches[left..right],
_ => unreachable!(),
}
}) })
} }
} }

View File

@ -116,7 +116,7 @@ where D: Deref<Target=DB>,
} }
} }
matches.into_iter().map(|(id, m)| Document::from_unsorted_matches(id, m)).collect() matches.into_iter().map(|(i, m)| Document::from_unsorted_matches(i, m)).collect()
} }
} }