Revert "feat: Prefer using ranges and not using unreachable!"

This reverts commit d899b86603.
This commit is contained in:
Clément Renault 2019-01-06 21:26:26 +01:00
parent 023f62b0ce
commit 34d2850d28
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE

View File

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