From 34d2850d284ac3677060d02913af0bc07c3a83d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 6 Jan 2019 21:26:26 +0100 Subject: [PATCH] Revert "feat: Prefer using ranges and not using unreachable!" This reverts commit d899b8660382ea5a4c211761d28ea31e885a46ce. --- src/rank/mod.rs | 54 +++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/rank/mod.rs b/src/rank/mod.rs index 91392cbee..2023cb266 100644 --- a/src/rank/mod.rs +++ b/src/rank/mod.rs @@ -3,8 +3,7 @@ mod query_builder; mod distinct_map; use std::iter::FusedIterator; -use std::slice::Iter; -use std::ops::Range; +use std::slice::Windows; use sdset::SetBuf; use group_by::GroupBy; @@ -44,19 +43,18 @@ impl Document { #[derive(Debug, Clone)] pub struct Matches { matches: SetBuf, - slices: Vec>, + slices: Vec, } impl Matches { pub fn new(matches: SetBuf) -> Matches { - let mut last_end = 0; - let mut slices = Vec::new(); + let mut last = 0; + let mut slices = vec![0]; for group in GroupBy::new(&matches, match_query_index) { - let start = last_end; - let end = last_end + group.len(); - slices.push(Range { start, end }); - last_end = end; + let index = last + group.len(); + slices.push(index); + last = index; } Matches { matches, slices } @@ -71,7 +69,7 @@ impl Matches { pub fn query_index_groups(&self) -> QueryIndexGroups { QueryIndexGroups { matches: &self.matches, - slices: self.slices.iter(), + windows: self.slices.windows(2), } } @@ -82,7 +80,7 @@ impl Matches { pub struct QueryIndexGroups<'a, 'b> { matches: &'a [Match], - slices: Iter<'b, Range>, + windows: Windows<'b, usize>, } impl<'a> Iterator for QueryIndexGroups<'a, '_> { @@ -90,14 +88,17 @@ impl<'a> Iterator for QueryIndexGroups<'a, '_> { #[inline] fn next(&mut self) -> Option { - self.slices.next().cloned().map(|range| { - unsafe { self.matches.get_unchecked(range) } + self.windows.next().map(|range| { + match *range { + [left, right] => &self.matches[left..right], + _ => unreachable!(), + } }) } #[inline] fn size_hint(&self) -> (usize, Option) { - self.slices.size_hint() + self.windows.size_hint() } #[inline] @@ -107,16 +108,22 @@ impl<'a> Iterator for QueryIndexGroups<'a, '_> { #[inline] fn nth(&mut self, n: usize) -> Option { - self.slices.nth(n).cloned().map(|range| { - unsafe { self.matches.get_unchecked(range) } + self.windows.nth(n).map(|range| { + match *range { + [left, right] => &self.matches[left..right], + _ => unreachable!(), + } }) } #[inline] fn last(self) -> Option { - let (matches, slices) = (self.matches, self.slices); - slices.last().cloned().map(|range| { - unsafe { matches.get_unchecked(range) } + let (matches, windows) = (self.matches, self.windows); + windows.last().map(|range| { + match *range { + [left, right] => &matches[left..right], + _ => unreachable!(), + } }) } } @@ -124,7 +131,7 @@ impl<'a> Iterator for QueryIndexGroups<'a, '_> { impl ExactSizeIterator for QueryIndexGroups<'_, '_> { #[inline] fn len(&self) -> usize { - self.slices.len() + self.windows.len() } } @@ -133,8 +140,11 @@ impl FusedIterator for QueryIndexGroups<'_, '_> { } impl DoubleEndedIterator for QueryIndexGroups<'_, '_> { #[inline] fn next_back(&mut self) -> Option { - self.slices.next_back().cloned().map(|range| { - unsafe { self.matches.get_unchecked(range) } + self.windows.next_back().map(|range| { + match *range { + [left, right] => &self.matches[left..right], + _ => unreachable!(), + } }) } }