use std::fmt;
use std::io;
use std::iter::{self, FromIterator};
use crate::automaton::{AlwaysMatch, Automaton};
use crate::raw;
pub use crate::raw::IndexedValue;
use crate::stream::{IntoStreamer, Streamer};
use crate::Result;
#[derive(Clone)]
pub struct Map<D>(raw::Fst<D>);
impl Map<Vec<u8>> {
pub fn from_iter<K, I>(iter: I) -> Result<Map<Vec<u8>>>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = (K, u64)>,
{
let mut builder = MapBuilder::memory();
builder.extend_iter(iter)?;
Map::new(builder.into_inner()?)
}
}
impl<D: AsRef<[u8]>> Map<D> {
pub fn new(data: D) -> Result<Map<D>> {
raw::Fst::new(data).map(Map)
}
pub fn contains_key<K: AsRef<[u8]>>(&self, key: K) -> bool {
self.0.contains_key(key)
}
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Option<u64> {
self.0.get(key).map(|output| output.value())
}
#[inline]
pub fn stream(&self) -> Stream<'_> {
Stream(self.0.stream())
}
#[inline]
pub fn keys(&self) -> Keys<'_> {
Keys(self.0.stream())
}
#[inline]
pub fn values(&self) -> Values<'_> {
Values(self.0.stream())
}
#[inline]
pub fn range(&self) -> StreamBuilder<'_> {
StreamBuilder(self.0.range())
}
pub fn search<A: Automaton>(&self, aut: A) -> StreamBuilder<'_, A> {
StreamBuilder(self.0.search(aut))
}
#[cfg_attr(
feature = "levenshtein",
doc = r##"
# Example
An implementation of fuzzy search using Levenshtein automata can be used
to search maps:
```rust
use fst::automaton::Levenshtein;
use fst::{IntoStreamer, Streamer, Map};
# fn main() { example().unwrap(); }
fn example() -> Result<(), Box<dyn std::error::Error>> {
let map = Map::from_iter(vec![
("foo", 1),
("foob", 2),
("foobar", 3),
("fozb", 4),
]).unwrap();
let query = Levenshtein::new("foo", 2)?;
let mut stream = map.search_with_state(&query).into_stream();
let mut kvs = vec![];
while let Some((k, v, s)) = stream.next() {
kvs.push((String::from_utf8(k.to_vec())?, v, s));
}
// Currently, there isn't much interesting that you can do with the states.
assert_eq!(kvs, vec![
("foo".to_string(), 1, Some(183)),
("foob".to_string(), 2, Some(123)),
("fozb".to_string(), 4, Some(83)),
]);
Ok(())
}
```
"##
)]
pub fn search_with_state<A: Automaton>(
&self,
aut: A,
) -> StreamWithStateBuilder<'_, A> {
StreamWithStateBuilder(self.0.search_with_state(aut))
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn op(&self) -> OpBuilder<'_> {
OpBuilder::new().add(self)
}
#[inline]
pub fn as_fst(&self) -> &raw::Fst<D> {
&self.0
}
#[inline]
pub fn into_fst(self) -> raw::Fst<D> {
self.0
}
#[inline]
pub fn map_data<F, T>(self, f: F) -> Result<Map<T>>
where
F: FnMut(D) -> T,
T: AsRef<[u8]>,
{
self.into_fst().map_data(f).map(Map::from)
}
}
impl Default for Map<Vec<u8>> {
#[inline]
fn default() -> Map<Vec<u8>> {
Map::from_iter(iter::empty::<(&[u8], u64)>()).unwrap()
}
}
impl<D: AsRef<[u8]>> fmt::Debug for Map<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Map([")?;
let mut stream = self.stream();
let mut first = true;
while let Some((k, v)) = stream.next() {
if !first {
write!(f, ", ")?;
}
first = false;
write!(f, "({}, {})", String::from_utf8_lossy(k), v)?;
}
write!(f, "])")
}
}
impl<D: AsRef<[u8]>> From<raw::Fst<D>> for Map<D> {
#[inline]
fn from(fst: raw::Fst<D>) -> Map<D> {
Map(fst)
}
}
impl<D: AsRef<[u8]>> AsRef<raw::Fst<D>> for Map<D> {
#[inline]
fn as_ref(&self) -> &raw::Fst<D> {
&self.0
}
}
impl<'m, 'a, D: AsRef<[u8]>> IntoStreamer<'a> for &'m Map<D> {
type Item = (&'a [u8], u64);
type Into = Stream<'m>;
#[inline]
fn into_stream(self) -> Stream<'m> {
Stream(self.0.stream())
}
}
pub struct MapBuilder<W>(raw::Builder<W>);
impl MapBuilder<Vec<u8>> {
#[inline]
pub fn memory() -> MapBuilder<Vec<u8>> {
MapBuilder(raw::Builder::memory())
}
#[inline]
pub fn into_map(self) -> Map<Vec<u8>> {
Map(self.0.into_fst())
}
}
impl<W: io::Write> MapBuilder<W> {
pub fn new(wtr: W) -> Result<MapBuilder<W>> {
raw::Builder::new_type(wtr, 0).map(MapBuilder)
}
pub fn insert<K: AsRef<[u8]>>(&mut self, key: K, val: u64) -> Result<()> {
self.0.insert(key, val)
}
pub fn extend_iter<K, I>(&mut self, iter: I) -> Result<()>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = (K, u64)>,
{
self.0.extend_iter(
iter.into_iter().map(|(k, v)| (k, raw::Output::new(v))),
)
}
pub fn extend_stream<'f, I, S>(&mut self, stream: I) -> Result<()>
where
I: for<'a> IntoStreamer<'a, Into = S, Item = (&'a [u8], u64)>,
S: 'f + for<'a> Streamer<'a, Item = (&'a [u8], u64)>,
{
self.0.extend_stream(StreamOutput(stream.into_stream()))
}
pub fn finish(self) -> Result<()> {
self.0.finish()
}
pub fn into_inner(self) -> Result<W> {
self.0.into_inner()
}
pub fn get_ref(&self) -> &W {
self.0.get_ref()
}
pub fn bytes_written(&self) -> u64 {
self.0.bytes_written()
}
}
pub struct Stream<'m, A = AlwaysMatch>(raw::Stream<'m, A>)
where
A: Automaton;
impl<'a, 'm, A: Automaton> Streamer<'a> for Stream<'m, A> {
type Item = (&'a [u8], u64);
fn next(&'a mut self) -> Option<(&'a [u8], u64)> {
self.0.next().map(|(key, out)| (key, out.value()))
}
}
impl<'m, A: Automaton> Stream<'m, A> {
pub fn into_byte_vec(self) -> Vec<(Vec<u8>, u64)> {
self.0.into_byte_vec()
}
pub fn into_str_vec(self) -> Result<Vec<(String, u64)>> {
self.0.into_str_vec()
}
pub fn into_byte_keys(self) -> Vec<Vec<u8>> {
self.0.into_byte_keys()
}
pub fn into_str_keys(self) -> Result<Vec<String>> {
self.0.into_str_keys()
}
pub fn into_values(self) -> Vec<u64> {
self.0.into_values()
}
}
pub struct StreamWithState<'m, A = AlwaysMatch>(raw::StreamWithState<'m, A>)
where
A: Automaton;
impl<'a, 'm, A: 'a + Automaton> Streamer<'a> for StreamWithState<'m, A>
where
A::State: Clone,
{
type Item = (&'a [u8], u64, A::State);
fn next(&'a mut self) -> Option<(&'a [u8], u64, A::State)> {
self.0.next().map(|(key, out, state)| (key, out.value(), state))
}
}
pub struct Keys<'m>(raw::Stream<'m>);
impl<'a, 'm> Streamer<'a> for Keys<'m> {
type Item = &'a [u8];
#[inline]
fn next(&'a mut self) -> Option<&'a [u8]> {
self.0.next().map(|(key, _)| key)
}
}
pub struct Values<'m>(raw::Stream<'m>);
impl<'a, 'm> Streamer<'a> for Values<'m> {
type Item = u64;
#[inline]
fn next(&'a mut self) -> Option<u64> {
self.0.next().map(|(_, out)| out.value())
}
}
pub struct StreamBuilder<'m, A = AlwaysMatch>(raw::StreamBuilder<'m, A>);
impl<'m, A: Automaton> StreamBuilder<'m, A> {
pub fn ge<T: AsRef<[u8]>>(self, bound: T) -> StreamBuilder<'m, A> {
StreamBuilder(self.0.ge(bound))
}
pub fn gt<T: AsRef<[u8]>>(self, bound: T) -> StreamBuilder<'m, A> {
StreamBuilder(self.0.gt(bound))
}
pub fn le<T: AsRef<[u8]>>(self, bound: T) -> StreamBuilder<'m, A> {
StreamBuilder(self.0.le(bound))
}
pub fn lt<T: AsRef<[u8]>>(self, bound: T) -> StreamBuilder<'m, A> {
StreamBuilder(self.0.lt(bound))
}
}
impl<'m, 'a, A: Automaton> IntoStreamer<'a> for StreamBuilder<'m, A> {
type Item = (&'a [u8], u64);
type Into = Stream<'m, A>;
fn into_stream(self) -> Stream<'m, A> {
Stream(self.0.into_stream())
}
}
pub struct StreamWithStateBuilder<'m, A = AlwaysMatch>(
raw::StreamWithStateBuilder<'m, A>,
);
impl<'m, A: Automaton> StreamWithStateBuilder<'m, A> {
pub fn ge<T: AsRef<[u8]>>(
self,
bound: T,
) -> StreamWithStateBuilder<'m, A> {
StreamWithStateBuilder(self.0.ge(bound))
}
pub fn gt<T: AsRef<[u8]>>(
self,
bound: T,
) -> StreamWithStateBuilder<'m, A> {
StreamWithStateBuilder(self.0.gt(bound))
}
pub fn le<T: AsRef<[u8]>>(
self,
bound: T,
) -> StreamWithStateBuilder<'m, A> {
StreamWithStateBuilder(self.0.le(bound))
}
pub fn lt<T: AsRef<[u8]>>(
self,
bound: T,
) -> StreamWithStateBuilder<'m, A> {
StreamWithStateBuilder(self.0.lt(bound))
}
}
impl<'m, 'a, A: 'a + Automaton> IntoStreamer<'a>
for StreamWithStateBuilder<'m, A>
where
A::State: Clone,
{
type Item = (&'a [u8], u64, A::State);
type Into = StreamWithState<'m, A>;
fn into_stream(self) -> StreamWithState<'m, A> {
StreamWithState(self.0.into_stream())
}
}
pub struct OpBuilder<'m>(raw::OpBuilder<'m>);
impl<'m> OpBuilder<'m> {
#[inline]
pub fn new() -> OpBuilder<'m> {
OpBuilder(raw::OpBuilder::new())
}
pub fn add<I, S>(mut self, streamable: I) -> OpBuilder<'m>
where
I: for<'a> IntoStreamer<'a, Into = S, Item = (&'a [u8], u64)>,
S: 'm + for<'a> Streamer<'a, Item = (&'a [u8], u64)>,
{
self.push(streamable);
self
}
pub fn push<I, S>(&mut self, streamable: I)
where
I: for<'a> IntoStreamer<'a, Into = S, Item = (&'a [u8], u64)>,
S: 'm + for<'a> Streamer<'a, Item = (&'a [u8], u64)>,
{
self.0.push(StreamOutput(streamable.into_stream()));
}
#[inline]
pub fn union(self) -> Union<'m> {
Union(self.0.union())
}
#[inline]
pub fn intersection(self) -> Intersection<'m> {
Intersection(self.0.intersection())
}
#[inline]
pub fn difference(self) -> Difference<'m> {
Difference(self.0.difference())
}
#[inline]
pub fn symmetric_difference(self) -> SymmetricDifference<'m> {
SymmetricDifference(self.0.symmetric_difference())
}
}
impl<'f, I, S> Extend<I> for OpBuilder<'f>
where
I: for<'a> IntoStreamer<'a, Into = S, Item = (&'a [u8], u64)>,
S: 'f + for<'a> Streamer<'a, Item = (&'a [u8], u64)>,
{
fn extend<T>(&mut self, it: T)
where
T: IntoIterator<Item = I>,
{
for stream in it {
self.push(stream);
}
}
}
impl<'f, I, S> FromIterator<I> for OpBuilder<'f>
where
I: for<'a> IntoStreamer<'a, Into = S, Item = (&'a [u8], u64)>,
S: 'f + for<'a> Streamer<'a, Item = (&'a [u8], u64)>,
{
fn from_iter<T>(it: T) -> OpBuilder<'f>
where
T: IntoIterator<Item = I>,
{
let mut op = OpBuilder::new();
op.extend(it);
op
}
}
pub struct Union<'m>(raw::Union<'m>);
impl<'a, 'm> Streamer<'a> for Union<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
#[inline]
fn next(&'a mut self) -> Option<(&'a [u8], &'a [IndexedValue])> {
self.0.next()
}
}
pub struct Intersection<'m>(raw::Intersection<'m>);
impl<'a, 'm> Streamer<'a> for Intersection<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
#[inline]
fn next(&'a mut self) -> Option<(&'a [u8], &'a [IndexedValue])> {
self.0.next()
}
}
pub struct Difference<'m>(raw::Difference<'m>);
impl<'a, 'm> Streamer<'a> for Difference<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
#[inline]
fn next(&'a mut self) -> Option<(&'a [u8], &'a [IndexedValue])> {
self.0.next()
}
}
pub struct SymmetricDifference<'m>(raw::SymmetricDifference<'m>);
impl<'a, 'm> Streamer<'a> for SymmetricDifference<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
#[inline]
fn next(&'a mut self) -> Option<(&'a [u8], &'a [IndexedValue])> {
self.0.next()
}
}
struct StreamOutput<S>(S);
impl<'a, S> Streamer<'a> for StreamOutput<S>
where
S: Streamer<'a, Item = (&'a [u8], u64)>,
{
type Item = (&'a [u8], raw::Output);
fn next(&'a mut self) -> Option<(&'a [u8], raw::Output)> {
self.0.next().map(|(k, v)| (k, raw::Output::new(v)))
}
}