mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-26 12:05:05 +08:00
feat: Prefer using ranges and not using unreachable!
This commit is contained in:
parent
0d07af3caf
commit
d899b86603
@ -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!(),
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user