2018-10-10 16:57:21 +02:00
|
|
|
pub mod criterion;
|
2018-12-07 11:53:17 +01:00
|
|
|
mod query_builder;
|
2018-11-28 17:12:24 +01:00
|
|
|
mod distinct_map;
|
2018-07-07 14:43:29 +02:00
|
|
|
|
2019-01-06 11:29:43 +01:00
|
|
|
use std::iter::FusedIterator;
|
2019-01-06 21:26:26 +01:00
|
|
|
use std::slice::Windows;
|
2019-01-06 11:11:55 +01:00
|
|
|
|
|
|
|
use sdset::SetBuf;
|
|
|
|
use group_by::GroupBy;
|
|
|
|
|
2018-09-09 11:13:58 +02:00
|
|
|
use crate::{Match, DocumentId};
|
2018-05-27 15:23:43 +02:00
|
|
|
|
2018-12-29 20:16:29 +01:00
|
|
|
pub use self::query_builder::{FilterFunc, QueryBuilder, DistinctQueryBuilder};
|
2018-05-27 15:23:43 +02:00
|
|
|
|
2018-06-25 22:26:49 +02:00
|
|
|
#[inline]
|
|
|
|
fn match_query_index(a: &Match, b: &Match) -> bool {
|
|
|
|
a.query_index == b.query_index
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2018-05-27 15:23:43 +02:00
|
|
|
pub struct Document {
|
2018-10-10 16:57:21 +02:00
|
|
|
pub id: DocumentId,
|
2019-01-06 11:11:55 +01:00
|
|
|
pub matches: Matches,
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Document {
|
2019-01-06 11:23:21 +01:00
|
|
|
pub fn new(id: DocumentId, match_: Match) -> Self {
|
2019-01-06 11:11:55 +01:00
|
|
|
let matches = SetBuf::new_unchecked(vec![match_]);
|
2019-01-06 11:23:21 +01:00
|
|
|
Self::from_matches(id, matches)
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
|
|
|
|
2019-01-06 11:11:55 +01:00
|
|
|
pub fn from_matches(id: DocumentId, matches: SetBuf<Match>) -> Self {
|
2019-01-06 11:23:21 +01:00
|
|
|
let matches = Matches::new(matches);
|
|
|
|
Self { id, matches }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_unsorted_matches(id: DocumentId, matches: Vec<Match>) -> Self {
|
|
|
|
let matches = Matches::from_unsorted(matches);
|
|
|
|
Self { id, matches }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Matches {
|
|
|
|
matches: SetBuf<Match>,
|
2019-01-06 21:26:26 +01:00
|
|
|
slices: Vec<usize>,
|
2019-01-06 11:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Matches {
|
|
|
|
pub fn new(matches: SetBuf<Match>) -> Matches {
|
2019-01-06 21:26:26 +01:00
|
|
|
let mut last = 0;
|
|
|
|
let mut slices = vec![0];
|
2019-01-06 11:23:21 +01:00
|
|
|
|
2019-01-06 11:11:55 +01:00
|
|
|
for group in GroupBy::new(&matches, match_query_index) {
|
2019-01-06 21:26:26 +01:00
|
|
|
let index = last + group.len();
|
|
|
|
slices.push(index);
|
|
|
|
last = index;
|
2019-01-06 11:11:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 11:23:21 +01:00
|
|
|
Matches { matches, slices }
|
2019-01-06 11:11:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 11:23:21 +01:00
|
|
|
pub fn from_unsorted(mut matches: Vec<Match>) -> Matches {
|
2018-10-21 16:42:19 +02:00
|
|
|
matches.sort_unstable();
|
2019-01-06 11:11:55 +01:00
|
|
|
let matches = SetBuf::new_unchecked(matches);
|
2019-01-06 11:23:21 +01:00
|
|
|
Matches::new(matches)
|
2018-10-21 16:42:19 +02:00
|
|
|
}
|
2019-01-06 11:11:55 +01:00
|
|
|
|
|
|
|
pub fn query_index_groups(&self) -> QueryIndexGroups {
|
|
|
|
QueryIndexGroups {
|
|
|
|
matches: &self.matches,
|
2019-01-06 21:26:26 +01:00
|
|
|
windows: self.slices.windows(2),
|
2019-01-06 11:11:55 +01:00
|
|
|
}
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
2019-01-06 11:23:42 +01:00
|
|
|
|
|
|
|
pub fn as_matches(&self) -> &[Match] {
|
|
|
|
&self.matches
|
|
|
|
}
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
2019-01-06 11:11:55 +01:00
|
|
|
|
|
|
|
pub struct QueryIndexGroups<'a, 'b> {
|
|
|
|
matches: &'a [Match],
|
2019-01-06 21:26:26 +01:00
|
|
|
windows: Windows<'b, usize>,
|
2019-01-06 11:11:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 11:29:43 +01:00
|
|
|
impl<'a> Iterator for QueryIndexGroups<'a, '_> {
|
2019-01-06 11:11:55 +01:00
|
|
|
type Item = &'a [Match];
|
|
|
|
|
2019-01-06 11:29:43 +01:00
|
|
|
#[inline]
|
2019-01-06 11:11:55 +01:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2019-01-06 21:26:26 +01:00
|
|
|
self.windows.next().map(|range| {
|
|
|
|
match *range {
|
|
|
|
[left, right] => &self.matches[left..right],
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2019-01-06 11:11:55 +01:00
|
|
|
})
|
|
|
|
}
|
2019-01-06 11:29:43 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2019-01-06 21:26:26 +01:00
|
|
|
self.windows.size_hint()
|
2019-01-06 11:29:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
2019-01-06 21:26:26 +01:00
|
|
|
self.windows.nth(n).map(|range| {
|
|
|
|
match *range {
|
|
|
|
[left, right] => &self.matches[left..right],
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2019-01-06 11:29:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn last(self) -> Option<Self::Item> {
|
2019-01-06 21:26:26 +01:00
|
|
|
let (matches, windows) = (self.matches, self.windows);
|
|
|
|
windows.last().map(|range| {
|
|
|
|
match *range {
|
|
|
|
[left, right] => &matches[left..right],
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2019-01-06 11:29:43 +01:00
|
|
|
})
|
|
|
|
}
|
2019-01-06 11:11:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 11:29:43 +01:00
|
|
|
impl ExactSizeIterator for QueryIndexGroups<'_, '_> {
|
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
2019-01-06 21:26:26 +01:00
|
|
|
self.windows.len()
|
2019-01-06 11:29:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FusedIterator for QueryIndexGroups<'_, '_> { }
|
|
|
|
|
|
|
|
impl DoubleEndedIterator for QueryIndexGroups<'_, '_> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
2019-01-06 21:26:26 +01:00
|
|
|
self.windows.next_back().map(|range| {
|
|
|
|
match *range {
|
|
|
|
[left, right] => &self.matches[left..right],
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2019-01-06 11:29:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|