use crate::formulas::{EncodedFormula, FormulaFactory};
use super::Substitution;
pub struct Anonymizer<'a> {
pub prefix: String,
substitution: Substitution,
counter: usize,
pub factory: &'a FormulaFactory,
}
impl<'a> Anonymizer<'a> {
pub fn new(f: &'a FormulaFactory) -> Self {
Self::with_prefix("v", f)
}
pub fn with_prefix(prefix: &str, f: &'a FormulaFactory) -> Self {
Self { prefix: prefix.to_owned(), substitution: Substitution::new(), counter: 0, factory: f }
}
pub fn anonymize(&mut self, formula: EncodedFormula) -> EncodedFormula {
let variables = formula.variables(self.factory);
for variable in &*variables {
self.substitution.entry(*variable).or_insert_with(|| {
let c = self.counter;
self.counter += 1;
self.factory.variable(&format!("{}{}", self.prefix, c))
});
}
self.factory.substitute(formula, &self.substitution)
}
pub const fn substitution(&self) -> &Substitution {
&self.substitution
}
}