1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
use std::iter::FromIterator; use std::io; use std::path::Path; use automaton::{Automaton, AlwaysMatch}; use raw::{ Builder, Fst, FstStream, FstStreamBuilder, Output, StreamOp, StreamUnion, StreamIntersection, StreamDifference, StreamSymmetricDifference, }; use stream::{IntoStream, Stream}; use Result; pub struct Set(Fst); impl Set { pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self> { Fst::from_file_path(path).map(Set) } pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> { Fst::from_bytes(bytes).map(Set) } pub fn stream(&self) -> SetStream { SetStream(self.0.stream()) } pub fn range(&self) -> SetStreamBuilder { SetStreamBuilder(self.0.range()) } pub fn contains<B: AsRef<[u8]>>(&self, key: B) -> bool { self.0.find(key).is_some() } pub fn search<A: Automaton>(&self, aut: A) -> SetStreamBuilder<A> { SetStreamBuilder(self.0.search(aut)) } pub fn len(&self) -> usize { self.0.len() } pub fn is_empty(&self) -> bool { self.0.is_empty() } pub fn as_fst(&self) -> &Fst { &self.0 } } impl<'s, 'a> IntoStream<'a> for &'s Set { type Item = &'a [u8]; type Into = SetStream<'s>; fn into_stream(self) -> Self::Into { SetStream(self.0.stream()) } } pub struct SetBuilder<W>(Builder<W>); impl SetBuilder<Vec<u8>> { pub fn memory() -> Self { SetBuilder(Builder::memory()) } } impl<W: io::Write> SetBuilder<W> { pub fn new(wtr: W) -> Result<SetBuilder<W>> { Builder::new_type(wtr, 1).map(SetBuilder) } pub fn insert<B: AsRef<[u8]>>(&mut self, bytes: B) -> Result<()> { self.0.add(bytes) } pub fn finish(self) -> Result<()> { self.0.finish() } pub fn into_inner(self) -> Result<W> { self.0.into_inner() } } pub struct SetStream<'s, A=AlwaysMatch>(FstStream<'s, A>) where A: Automaton; impl<'a, 's, A: Automaton> Stream<'a> for SetStream<'s, A> { type Item = &'a [u8]; fn next(&'a mut self) -> Option<Self::Item> { self.0.next().map(|(key, _)| key) } } pub struct SetStreamBuilder<'s, A=AlwaysMatch>(FstStreamBuilder<'s, A>); impl<'s, A: Automaton> SetStreamBuilder<'s, A> { pub fn ge<T: AsRef<[u8]>>(mut self, bound: T) -> Self { SetStreamBuilder(self.0.ge(bound)) } pub fn gt<T: AsRef<[u8]>>(mut self, bound: T) -> Self { SetStreamBuilder(self.0.gt(bound)) } pub fn le<T: AsRef<[u8]>>(mut self, bound: T) -> Self { SetStreamBuilder(self.0.le(bound)) } pub fn lt<T: AsRef<[u8]>>(mut self, bound: T) -> Self { SetStreamBuilder(self.0.lt(bound)) } } impl<'s, 'a, A: Automaton> IntoStream<'a> for SetStreamBuilder<'s, A> { type Item = &'a [u8]; type Into = SetStream<'s, A>; fn into_stream(self) -> Self::Into { SetStream(self.0.into_stream()) } } pub struct SetOp<'s>(StreamOp<'s>); impl<'s> SetOp<'s> { pub fn new() -> Self { SetOp(StreamOp::new()) } pub fn add<I, S>(mut self, streamable: I) -> Self where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>, S: 's + for<'a> Stream<'a, Item=&'a [u8]> { self.push(streamable); self } pub fn push<I, S>(&mut self, streamable: I) where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>, S: 's + for<'a> Stream<'a, Item=&'a [u8]> { self.0.push(SetStreamZeroOutput(streamable.into_stream())); } pub fn union(self) -> SetUnion<'s> { SetUnion(self.0.union()) } pub fn intersection(self) -> SetIntersection<'s> { SetIntersection(self.0.intersection()) } pub fn difference(self) -> SetDifference<'s> { SetDifference(self.0.difference()) } pub fn symmetric_difference(self) -> SetSymmetricDifference<'s> { SetSymmetricDifference(self.0.symmetric_difference()) } } impl<'f, I, S> Extend<I> for SetOp<'f> where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>, S: 'f + for<'a> Stream<'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 SetOp<'f> where I: for<'a> IntoStream<'a, Into=S, Item=&'a [u8]>, S: 'f + for<'a> Stream<'a, Item=&'a [u8]> { fn from_iter<T>(it: T) -> Self where T: IntoIterator<Item=I> { let mut op = SetOp::new(); op.extend(it); op } } pub struct SetUnion<'s>(StreamUnion<'s>); impl<'a, 's> Stream<'a> for SetUnion<'s> { type Item = &'a [u8]; fn next(&'a mut self) -> Option<Self::Item> { self.0.next().map(|(key, _)| key) } } pub struct SetIntersection<'s>(StreamIntersection<'s>); impl<'a, 's> Stream<'a> for SetIntersection<'s> { type Item = &'a [u8]; fn next(&'a mut self) -> Option<Self::Item> { self.0.next().map(|(key, _)| key) } } pub struct SetDifference<'s>(StreamDifference<'s>); impl<'a, 's> Stream<'a> for SetDifference<'s> { type Item = &'a [u8]; fn next(&'a mut self) -> Option<Self::Item> { self.0.next().map(|(key, _)| key) } } pub struct SetSymmetricDifference<'s>(StreamSymmetricDifference<'s>); impl<'a, 's> Stream<'a> for SetSymmetricDifference<'s> { type Item = &'a [u8]; fn next(&'a mut self) -> Option<Self::Item> { self.0.next().map(|(key, _)| key) } } 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())) } }