use std::fmt;
use std::iter::FromIterator;
use std::io;
use std::path::Path;
use automaton::{Automaton, AlwaysMatch};
use raw;
pub use raw::IndexedValue as IndexedValue;
use stream::{IntoStreamer, Streamer};
use Result;
pub struct Map(raw::Fst);
impl Map {
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self> {
raw::Fst::from_file_path(path).map(Map)
}
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
raw::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) -> Stream {
Stream(self.0.stream())
}
pub fn keys(&self) -> Keys {
Keys(self.0.stream())
}
pub fn values(&self) -> Values {
Values(self.0.stream())
}
pub fn range(&self) -> StreamBuilder {
StreamBuilder(self.0.range())
}
pub fn contains_key<K: AsRef<[u8]>>(&self, key: K) -> bool {
self.0.find(key).is_some()
}
pub fn search<A: Automaton>(&self, aut: A) -> StreamBuilder<A> {
StreamBuilder(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) -> OpBuilder {
OpBuilder::new().add(self)
}
}
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<raw::Fst> for Map {
fn as_ref(&self) -> &raw::Fst {
&self.0
}
}
impl<'m, 'a> IntoStreamer<'a> for &'m Map {
type Item = (&'a [u8], u64);
type Into = Stream<'m>;
fn into_stream(self) -> Self::Into {
Stream(self.0.stream())
}
}
pub struct MapBuilder<W>(raw::Builder<W>);
impl MapBuilder<Vec<u8>> {
pub fn memory() -> Self {
MapBuilder(raw::Builder::memory())
}
}
impl<W: io::Write> MapBuilder<W> {
pub fn new(wtr: W) -> Result<MapBuilder<W>> {
raw::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, 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 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<Self::Item> {
self.0.next().map(|(key, out)| (key, out.value()))
}
}
pub struct Keys<'m>(raw::Stream<'m>);
impl<'a, 'm> Streamer<'a> for Keys<'m> {
type Item = &'a [u8];
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _)| key)
}
}
pub struct Values<'m>(raw::Stream<'m>);
impl<'a, 'm> Streamer<'a> for Values<'m> {
type Item = u64;
fn next(&'a mut self) -> Option<Self::Item> {
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) -> Self {
StreamBuilder(self.0.ge(bound))
}
pub fn gt<T: AsRef<[u8]>>(self, bound: T) -> Self {
StreamBuilder(self.0.gt(bound))
}
pub fn le<T: AsRef<[u8]>>(self, bound: T) -> Self {
StreamBuilder(self.0.le(bound))
}
pub fn lt<T: AsRef<[u8]>>(self, bound: T) -> Self {
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) -> Self::Into {
Stream(self.0.into_stream())
}
}
pub struct OpBuilder<'m>(raw::OpBuilder<'m>);
impl<'m> OpBuilder<'m> {
pub fn new() -> Self {
OpBuilder(raw::OpBuilder::new())
}
pub fn add<I, S>(mut self, streamable: I) -> Self
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()));
}
pub fn union(self) -> Union<'m> {
Union(self.0.union())
}
pub fn intersection(self) -> Intersection<'m> {
Intersection(self.0.intersection())
}
pub fn difference(self) -> Difference<'m> {
Difference(self.0.difference())
}
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) -> Self 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]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct Intersection<'m>(raw::Intersection<'m>);
impl<'a, 'm> Streamer<'a> for Intersection<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct Difference<'m>(raw::Difference<'m>);
impl<'a, 'm> Streamer<'a> for Difference<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct SymmetricDifference<'m>(raw::SymmetricDifference<'m>);
impl<'a, 'm> Streamer<'a> for SymmetricDifference<'m> {
type Item = (&'a [u8], &'a [IndexedValue]);
fn next(&'a mut self) -> Option<Self::Item> {
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<Self::Item> {
self.0.next().map(|(k, v)| (k, raw::Output::new(v)))
}
}