use std::fmt;
use std::iter::FromIterator;
use std::io;
use std::path::Path;
use automaton::{Automaton, AlwaysMatch};
use raw::{
Builder, Fst, FstStream, FstStreamBuilder, Output, FstOpBuilder,
FstUnion, FstIntersection, FstDifference, FstSymmetricDifference,
};
pub use raw::IndexedValue as IndexedValue;
use set::SetStream;
use stream::{IntoStream, Stream};
use Result;
pub struct Map(Fst);
impl Map {
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self> {
Fst::from_file_path(path).map(Map)
}
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
Fst::from_bytes(bytes).map(Map)
}
pub fn from_iter<K, I>(iter: I) -> Result<Self>
where K: AsRef<[u8]>, I: IntoIterator<Item=(K, u64)> {
let mut builder = MapBuilder::memory();
try!(builder.extend_iter(iter));
Map::from_bytes(try!(builder.into_inner()))
}
pub fn stream(&self) -> MapStream {
MapStream(self.0.stream())
}
pub fn stream_keys(&self) -> SetStream {
SetStream::new(self.0.stream())
}
pub fn range(&self) -> MapStreamBuilder {
MapStreamBuilder(self.0.range())
}
pub fn contains<K: AsRef<[u8]>>(&self, key: K) -> bool {
self.0.find(key).is_some()
}
pub fn search<A: Automaton>(&self, aut: A) -> MapStreamBuilder<A> {
MapStreamBuilder(self.0.search(aut))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn op(&self) -> MapOpBuilder {
MapOpBuilder::new().add(self)
}
pub fn is_disjoint<'f, I, S>(&self, stream: I) -> bool
where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Stream<'a, Item=&'a [u8]> {
self.0.is_disjoint(SetStreamZeroOutput(stream.into_stream()))
}
pub fn is_subset<'f, I, S>(&self, stream: I) -> bool
where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Stream<'a, Item=&'a [u8]> {
self.0.is_subset(SetStreamZeroOutput(stream.into_stream()))
}
pub fn is_superset<'f, I, S>(&self, stream: I) -> bool
where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Stream<'a, Item=&'a [u8]> {
self.0.is_superset(SetStreamZeroOutput(stream.into_stream()))
}
}
impl fmt::Debug for Map {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Map(["));
let mut stream = self.stream();
let mut first = true;
while let Some((k, v)) = stream.next() {
if !first {
try!(write!(f, ", "));
}
first = false;
try!(write!(f, "({}, {})", String::from_utf8_lossy(k), v));
}
write!(f, "])")
}
}
impl AsRef<Fst> for Map {
fn as_ref(&self) -> &Fst {
&self.0
}
}
impl<'s, 'a> IntoStream<'a> for &'s Map {
type Item = (&'a [u8], u64);
type Into = MapStream<'s>;
fn into_stream(self) -> Self::Into {
MapStream(self.0.stream())
}
}
pub struct MapBuilder<W>(Builder<W>);
impl MapBuilder<Vec<u8>> {
pub fn memory() -> Self {
MapBuilder(Builder::memory())
}
}
impl<W: io::Write> MapBuilder<W> {
pub fn new(wtr: W) -> Result<MapBuilder<W>> {
Builder::new_type(wtr, 1).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, Output::new(v))))
}
pub fn extend_stream<'f, I, S>(&mut self, stream: I) -> Result<()>
where I: for<'a> IntoStream<'a, Into=S, Item=(&'a [u8], u64)>,
S: 'f + for<'a> Stream<'a, Item=(&'a [u8], u64)> {
self.0.extend_stream(MapStreamOutput(stream.into_stream()))
}
pub fn finish(self) -> Result<()> {
self.0.finish()
}
pub fn into_inner(self) -> Result<W> {
self.0.into_inner()
}
}
pub struct MapStream<'s, A=AlwaysMatch>(FstStream<'s, A>) where A: Automaton;
impl<'a, 's, A: Automaton> Stream<'a> for MapStream<'s, A> {
type Item = (&'a [u8], u64);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, out)| (key, out.value()))
}
}
pub struct MapStreamBuilder<'s, A=AlwaysMatch>(FstStreamBuilder<'s, A>);
impl<'s, A: Automaton> MapStreamBuilder<'s, A> {
pub fn ge<T: AsRef<[u8]>>(self, bound: T) -> Self {
MapStreamBuilder(self.0.ge(bound))
}
pub fn gt<T: AsRef<[u8]>>(self, bound: T) -> Self {
MapStreamBuilder(self.0.gt(bound))
}
pub fn le<T: AsRef<[u8]>>(self, bound: T) -> Self {
MapStreamBuilder(self.0.le(bound))
}
pub fn lt<T: AsRef<[u8]>>(self, bound: T) -> Self {
MapStreamBuilder(self.0.lt(bound))
}
}
impl<'s, 'a, A: Automaton> IntoStream<'a> for MapStreamBuilder<'s, A> {
type Item = (&'a [u8], u64);
type Into = MapStream<'s, A>;
fn into_stream(self) -> Self::Into {
MapStream(self.0.into_stream())
}
}
pub struct MapOpBuilder<'s>(FstOpBuilder<'s>);
impl<'s> MapOpBuilder<'s> {
pub fn new() -> Self {
MapOpBuilder(FstOpBuilder::new())
}
pub fn add<I, S>(mut self, streamable: I) -> Self
where I: for<'a> IntoStream<'a, Into=S, Item=(&'a [u8], u64)>,
S: 's + for<'a> Stream<'a, Item=(&'a [u8], u64)> {
self.push(streamable);
self
}
pub fn push<I, S>(&mut self, streamable: I)
where I: for<'a> IntoStream<'a, Into=S, Item=(&'a [u8], u64)>,
S: 's + for<'a> Stream<'a, Item=(&'a [u8], u64)> {
self.0.push(MapStreamOutput(streamable.into_stream()));
}
pub fn union(self) -> MapUnion<'s> {
MapUnion(self.0.union())
}
pub fn intersection(self) -> MapIntersection<'s> {
MapIntersection(self.0.intersection())
}
pub fn difference(self) -> MapDifference<'s> {
MapDifference(self.0.difference())
}
pub fn symmetric_difference(self) -> MapSymmetricDifference<'s> {
MapSymmetricDifference(self.0.symmetric_difference())
}
}
impl<'f, I, S> Extend<I> for MapOpBuilder<'f>
where I: for<'a> IntoStream<'a, Into=S, Item=(&'a [u8], u64)>,
S: 'f + for<'a> Stream<'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 MapOpBuilder<'f>
where I: for<'a> IntoStream<'a, Into=S, Item=(&'a [u8], u64)>,
S: 'f + for<'a> Stream<'a, Item=(&'a [u8], u64)> {
fn from_iter<T>(it: T) -> Self where T: IntoIterator<Item=I> {
let mut op = MapOpBuilder::new();
op.extend(it);
op
}
}
pub struct MapUnion<'s>(FstUnion<'s>);
impl<'a, 's> Stream<'a> for MapUnion<'s> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct MapIntersection<'s>(FstIntersection<'s>);
impl<'a, 's> Stream<'a> for MapIntersection<'s> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct MapDifference<'s>(FstDifference<'s>);
impl<'a, 's> Stream<'a> for MapDifference<'s> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct MapSymmetricDifference<'s>(FstSymmetricDifference<'s>);
impl<'a, 's> Stream<'a> for MapSymmetricDifference<'s> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
struct MapStreamOutput<S>(S);
impl<'a, S> Stream<'a> for MapStreamOutput<S>
where S: Stream<'a, Item=(&'a [u8], u64)> {
type Item = (&'a [u8], Output);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(k, v)| (k, Output::new(v)))
}
}
struct SetStreamZeroOutput<S>(S);
impl<'a, S: Stream<'a>> Stream<'a> for SetStreamZeroOutput<S> {
type Item = (S::Item, Output);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|key| (key, Output::zero()))
}
}