2018-05-05 16:59:03 +08:00
|
|
|
#[macro_use] extern crate serde_derive;
|
2018-04-22 23:34:41 +08:00
|
|
|
extern crate bincode;
|
2018-04-22 21:54:34 +08:00
|
|
|
extern crate fst;
|
2018-05-05 16:59:03 +08:00
|
|
|
extern crate serde;
|
2018-04-22 21:54:34 +08:00
|
|
|
|
2018-05-05 16:59:03 +08:00
|
|
|
mod fst_map;
|
|
|
|
|
|
|
|
use fst::Automaton;
|
2018-04-22 21:54:34 +08:00
|
|
|
|
2018-05-05 16:59:03 +08:00
|
|
|
pub use self::fst_map::{FstMap, FstMapBuilder};
|
2018-05-06 18:23:42 +08:00
|
|
|
pub use self::fst_map::{
|
|
|
|
OpBuilder, IndexedValues,
|
|
|
|
OpWithStateBuilder, IndexedValuesWithState,
|
|
|
|
};
|
2018-05-05 16:59:03 +08:00
|
|
|
use self::fst_map::Values;
|
2018-04-22 23:34:41 +08:00
|
|
|
|
2018-05-06 04:13:00 +08:00
|
|
|
pub struct StreamBuilder<'m, 'v, T: 'v, A> {
|
|
|
|
inner: fst::map::StreamBuilder<'m, A>,
|
|
|
|
values: &'v Values<T>,
|
2018-04-23 02:06:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 04:13:00 +08:00
|
|
|
impl<'m, 'v, T: 'v, A> StreamBuilder<'m, 'v, T, A> {
|
|
|
|
pub fn with_state(self) -> StreamWithStateBuilder<'m, 'v, T, A> {
|
|
|
|
StreamWithStateBuilder {
|
|
|
|
inner: self.inner.with_state(),
|
|
|
|
values: self.values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 02:06:56 +08:00
|
|
|
|
2018-05-06 04:13:00 +08:00
|
|
|
impl<'m, 'v, 'a, T: 'v + 'a, A: Automaton> fst::IntoStreamer<'a> for StreamBuilder<'m, 'v, T, A> {
|
2018-05-06 05:02:51 +08:00
|
|
|
type Item = <Self::Into as fst::Streamer<'a>>::Item;
|
2018-05-06 04:13:00 +08:00
|
|
|
type Into = Stream<'m, 'v, T, A>;
|
2018-04-23 02:06:56 +08:00
|
|
|
|
|
|
|
fn into_stream(self) -> Self::Into {
|
|
|
|
Stream {
|
|
|
|
inner: self.inner.into_stream(),
|
|
|
|
values: self.values,
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 21:54:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 04:13:00 +08:00
|
|
|
pub struct Stream<'m, 'v, T: 'v, A: Automaton = fst::automaton::AlwaysMatch> {
|
|
|
|
inner: fst::map::Stream<'m, A>,
|
|
|
|
values: &'v Values<T>,
|
2018-04-22 23:34:41 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 04:13:00 +08:00
|
|
|
impl<'m, 'v, 'a, T: 'v + 'a, A: Automaton> fst::Streamer<'a> for Stream<'m, 'v, T, A> {
|
2018-05-06 05:02:51 +08:00
|
|
|
type Item = (&'a [u8], &'a [T]);
|
2018-04-22 23:34:41 +08:00
|
|
|
|
|
|
|
fn next(&'a mut self) -> Option<Self::Item> {
|
|
|
|
// Here we can't just `map` because of some borrow rules
|
|
|
|
match self.inner.next() {
|
|
|
|
Some((key, i)) => {
|
2018-05-05 16:59:03 +08:00
|
|
|
let values = unsafe { self.values.get_unchecked(i as usize) };
|
|
|
|
Some((key, values))
|
2018-04-22 23:34:41 +08:00
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 04:13:00 +08:00
|
|
|
|
|
|
|
pub struct StreamWithStateBuilder<'m, 'v, T: 'v, A> {
|
|
|
|
inner: fst::map::StreamWithStateBuilder<'m, A>,
|
|
|
|
values: &'v Values<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m, 'v, 'a, T: 'v + 'a, A: 'a> fst::IntoStreamer<'a> for StreamWithStateBuilder<'m, 'v, T, A>
|
|
|
|
where
|
|
|
|
A: Automaton,
|
|
|
|
A::State: Clone,
|
|
|
|
{
|
2018-05-06 05:02:51 +08:00
|
|
|
type Item = <Self::Into as fst::Streamer<'a>>::Item;
|
2018-05-06 04:13:00 +08:00
|
|
|
type Into = StreamWithState<'m, 'v, T, A>;
|
|
|
|
|
|
|
|
fn into_stream(self) -> Self::Into {
|
|
|
|
StreamWithState {
|
|
|
|
inner: self.inner.into_stream(),
|
|
|
|
values: self.values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StreamWithState<'m, 'v, T: 'v, A: Automaton = fst::automaton::AlwaysMatch> {
|
|
|
|
inner: fst::map::StreamWithState<'m, A>,
|
|
|
|
values: &'v Values<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m, 'v, 'a, T: 'v + 'a, A: 'a> fst::Streamer<'a> for StreamWithState<'m, 'v, T, A>
|
|
|
|
where
|
|
|
|
A: Automaton,
|
|
|
|
A::State: Clone,
|
|
|
|
{
|
2018-05-06 05:02:51 +08:00
|
|
|
type Item = (&'a [u8], &'a [T], A::State);
|
2018-05-06 04:13:00 +08:00
|
|
|
|
|
|
|
fn next(&'a mut self) -> Option<Self::Item> {
|
|
|
|
match self.inner.next() {
|
2018-05-06 05:02:51 +08:00
|
|
|
Some((key, i, state)) => {
|
2018-05-06 04:13:00 +08:00
|
|
|
let values = unsafe { self.values.get_unchecked(i as usize) };
|
|
|
|
Some((key, values, state))
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|