use std::fmt;
use std::iter::{self, FromIterator};
use std::io;
#[cfg(feature = "mmap")]
use std::path::Path;
use automaton::{Automaton, AlwaysMatch};
use raw;
use stream::{IntoStreamer, Streamer};
use Result;
pub struct Set(raw::Fst);
impl Set {
#[cfg(feature = "mmap")]
pub unsafe fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
raw::Fst::from_path(path).map(Set)
}
#[inline]
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
raw::Fst::from_bytes(bytes).map(Set)
}
pub fn from_static_slice(bytes: &'static [u8]) -> Result<Self> {
raw::Fst::from_static_slice(bytes).map(Set)
}
pub fn from_iter<T, I>(iter: I) -> Result<Self>
where T: AsRef<[u8]>, I: IntoIterator<Item=T> {
let mut builder = SetBuilder::memory();
builder.extend_iter(iter)?;
Set::from_bytes(builder.into_inner()?)
}
pub fn contains<K: AsRef<[u8]>>(&self, key: K) -> bool {
self.0.contains_key(key)
}
#[inline]
pub fn stream(&self) -> Stream {
Stream(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))
}
#[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)
}
pub fn is_disjoint<'f, I, S>(&self, stream: I) -> bool
where I: for<'a> IntoStreamer<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Streamer<'a, Item=&'a [u8]> {
self.0.is_disjoint(StreamZeroOutput(stream.into_stream()))
}
pub fn is_subset<'f, I, S>(&self, stream: I) -> bool
where I: for<'a> IntoStreamer<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Streamer<'a, Item=&'a [u8]> {
self.0.is_subset(StreamZeroOutput(stream.into_stream()))
}
pub fn is_superset<'f, I, S>(&self, stream: I) -> bool
where I: for<'a> IntoStreamer<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Streamer<'a, Item=&'a [u8]> {
self.0.is_superset(StreamZeroOutput(stream.into_stream()))
}
#[inline]
pub fn as_fst(&self) -> &raw::Fst {
&self.0
}
}
impl Default for Set {
#[inline]
fn default() -> Set {
Set::from_iter(iter::empty::<&[u8]>()).unwrap()
}
}
impl fmt::Debug for Set {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Set([")?;
let mut stream = self.stream();
let mut first = true;
while let Some(key) = stream.next() {
if !first {
write!(f, ", ")?;
}
first = false;
write!(f, "{}", String::from_utf8_lossy(key))?;
}
write!(f, "])")
}
}
impl AsRef<raw::Fst> for Set {
#[inline]
fn as_ref(&self) -> &raw::Fst {
&self.0
}
}
impl<'s, 'a> IntoStreamer<'a> for &'s Set {
type Item = &'a [u8];
type Into = Stream<'s>;
#[inline]
fn into_stream(self) -> Self::Into {
Stream(self.0.stream())
}
}
impl From<raw::Fst> for Set {
#[inline]
fn from(fst: raw::Fst) -> Set {
Set(fst)
}
}
pub struct SetBuilder<W>(raw::Builder<W>);
impl SetBuilder<Vec<u8>> {
#[inline]
pub fn memory() -> Self {
SetBuilder(raw::Builder::memory())
}
}
impl<W: io::Write> SetBuilder<W> {
pub fn new(wtr: W) -> Result<SetBuilder<W>> {
raw::Builder::new_type(wtr, 0).map(SetBuilder)
}
pub fn insert<K: AsRef<[u8]>>(&mut self, key: K) -> Result<()> {
self.0.add(key)
}
pub fn extend_iter<T, I>(&mut self, iter: I) -> Result<()>
where T: AsRef<[u8]>, I: IntoIterator<Item=T> {
for key in iter {
self.0.add(key)?;
}
Ok(())
}
pub fn extend_stream<'f, I, S>(&mut self, stream: I) -> Result<()>
where I: for<'a> IntoStreamer<'a, Into=S, Item=&'a [u8]>,
S: 'f + for<'a> Streamer<'a, Item=&'a [u8]> {
self.0.extend_stream(StreamZeroOutput(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<'s, A=AlwaysMatch>(raw::Stream<'s, A>) where A: Automaton;
impl<'s, A: Automaton> Stream<'s, A> {
#[doc(hidden)]
pub fn new(fst_stream: raw::Stream<'s, A>) -> Self {
Stream(fst_stream)
}
pub fn into_strs(self) -> Result<Vec<String>> {
self.0.into_str_keys()
}
pub fn into_bytes(self) -> Vec<Vec<u8>> {
self.0.into_byte_keys()
}
}
impl<'a, 's, A: Automaton> Streamer<'a> for Stream<'s, A> {
type Item = &'a [u8];
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _)| key)
}
}
pub struct StreamBuilder<'s, A=AlwaysMatch>(raw::StreamBuilder<'s, A>);
impl<'s, A: Automaton> StreamBuilder<'s, 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<'s, 'a, A: Automaton> IntoStreamer<'a> for StreamBuilder<'s, A> {
type Item = &'a [u8];
type Into = Stream<'s, A>;
fn into_stream(self) -> Self::Into {
Stream(self.0.into_stream())
}
}
pub struct OpBuilder<'s>(raw::OpBuilder<'s>);
impl<'s> OpBuilder<'s> {
#[inline]
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]>,
S: 's + for<'a> Streamer<'a, Item=&'a [u8]> {
self.push(streamable);
self
}
pub fn push<I, S>(&mut self, streamable: I)
where I: for<'a> IntoStreamer<'a, Into=S, Item=&'a [u8]>,
S: 's + for<'a> Streamer<'a, Item=&'a [u8]> {
self.0.push(StreamZeroOutput(streamable.into_stream()));
}
#[inline]
pub fn union(self) -> Union<'s> {
Union(self.0.union())
}
#[inline]
pub fn intersection(self) -> Intersection<'s> {
Intersection(self.0.intersection())
}
#[inline]
pub fn difference(self) -> Difference<'s> {
Difference(self.0.difference())
}
#[inline]
pub fn symmetric_difference(self) -> SymmetricDifference<'s> {
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]>,
S: 'f + for<'a> Streamer<'a, Item=&'a [u8]> {
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]>,
S: 'f + for<'a> Streamer<'a, Item=&'a [u8]> {
fn from_iter<T>(it: T) -> Self where T: IntoIterator<Item=I> {
let mut op = OpBuilder::new();
op.extend(it);
op
}
}
pub struct Union<'s>(raw::Union<'s>);
impl<'a, 's> Streamer<'a> for Union<'s> {
type Item = &'a [u8];
#[inline]
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _)| key)
}
}
pub struct Intersection<'s>(raw::Intersection<'s>);
impl<'a, 's> Streamer<'a> for Intersection<'s> {
type Item = &'a [u8];
#[inline]
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _)| key)
}
}
pub struct Difference<'s>(raw::Difference<'s>);
impl<'a, 's> Streamer<'a> for Difference<'s> {
type Item = &'a [u8];
#[inline]
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _)| key)
}
}
pub struct SymmetricDifference<'s>(raw::SymmetricDifference<'s>);
impl<'a, 's> Streamer<'a> for SymmetricDifference<'s> {
type Item = &'a [u8];
#[inline]
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _)| key)
}
}
struct StreamZeroOutput<S>(S);
impl<'a, S: Streamer<'a>> Streamer<'a> for StreamZeroOutput<S> {
type Item = (S::Item, raw::Output);
fn next(&'a mut self) -> Option<Self::Item> {
self.0.next().map(|key| (key, raw::Output::zero()))
}
}
#[cfg(test)]
mod tests {
use Streamer;
use super::OpBuilder;
#[test]
fn no_fsts() {
struct Iter<'a> {
i: usize,
xs: Vec<&'a [u8]>,
}
impl<'a> Iter<'a> {
fn new(xs: Vec<&'a [u8]>) -> Iter<'a> {
Iter { i: 0, xs: xs }
}
}
impl<'a, 's> Streamer<'a> for Iter<'s> {
type Item = &'a [u8];
fn next(&'a mut self) -> Option<&'a [u8]> {
if self.i >= self.xs.len() {
None
} else {
let i = self.i;
self.i += 1;
Some(self.xs[i])
}
}
}
let mut stream = OpBuilder::new()
.add(Iter::new(vec![
&b"bar"[..],
&b"baz"[..],
&b"foo"[..],
&b"fubar"[..],
&b"quux"[..],
]))
.add(Iter::new(vec![
&b"bar"[..],
&b"foofoo"[..],
&b"fubar"[..],
]))
.intersection();
let mut got = vec![];
while let Some(x) = stream.next() {
got.push(x.to_vec());
}
assert_eq!(got, vec![&b"bar"[..], &b"fubar"[..]]);
}
}