parent
9a7234806f
commit
6aa033ebbb
@ -1,9 +1,55 @@
|
||||
//! Container for chain
|
||||
use super::*;
|
||||
use chain::{Chain, Chainable};
|
||||
use dedup::HashRefSet;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ChainContainer<T: Chainable>
|
||||
{
|
||||
chain: Chain<T>,
|
||||
inputs_exact: dedup::HashRefSet<[T]>,
|
||||
inputs_exact: HashRefSet<[T]>,
|
||||
}
|
||||
|
||||
impl<T: Chainable> ChainContainer<T>
|
||||
{
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self {
|
||||
chain: Chain::new(),
|
||||
inputs_exact: HashRefSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn force_feed<U: AsRef<[T]>>(&mut self, tokens: U)
|
||||
{
|
||||
self.inputs_exact.insert(&tokens.as_ref());
|
||||
self.chain.feed(tokens);
|
||||
}
|
||||
pub fn feed<U: AsRef<[T]>>(&mut self, tokens: U) -> bool
|
||||
{
|
||||
let tokens=tokens.as_ref();
|
||||
if self.inputs_exact.insert(&tokens) {
|
||||
self.chain.feed(tokens);
|
||||
true
|
||||
} else {
|
||||
trace!("Exact duplication input disallowed");
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn chain(&self) -> &Chain<T>
|
||||
{
|
||||
&self.chain
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Chainable> From<Chain<T>> for ChainContainer<T>
|
||||
{
|
||||
fn from(chain: Chain<T>) -> Self
|
||||
{
|
||||
Self {
|
||||
chain,
|
||||
inputs_exact: HashRefSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue