use std::collections::HashSet;
use crate::formulas::{EncodedFormula, Formula, FormulaFactory};
pub fn contains_var_name(formula: EncodedFormula, variable: &str, f: &FormulaFactory) -> bool {
let mut seen = HashSet::new();
let mut stack = Vec::new();
stack.push(formula);
seen.insert(formula);
while let Some(current) = stack.pop() {
use Formula::{Cc, Lit, Pbc};
let result = match current.unpack(f) {
Lit(lit) => lit.name(f) == variable,
Cc(cc) => cc.variables.iter().any(|v| v.name(f) == variable),
Pbc(pbc) => pbc.literals.iter().any(|l| l.name(f) == variable),
_ => {
for &op in &*current.operands(f) {
if seen.insert(op) {
stack.push(op);
}
}
false
}
};
if result {
return true;
}
}
false
}
pub fn contains_node(formula: EncodedFormula, node: EncodedFormula, f: &FormulaFactory) -> bool {
use Formula::{Cc, Pbc};
let mut seen = HashSet::new();
let mut stack = Vec::new();
stack.push(formula);
seen.insert(formula);
while let Some(current) = stack.pop() {
if current == node {
return true;
}
let result = match current.unpack(f) {
Cc(cc) => cc.variables.iter().any(|v| EncodedFormula::from(*v) == node),
Pbc(pbc) => pbc.literals.iter().any(|l| EncodedFormula::from(*l) == node || EncodedFormula::from(l.variable()) == node),
_ => {
for &op in &*current.operands(f) {
if seen.insert(op) {
stack.push(op);
}
}
false
}
};
if result {
return true;
}
}
false
}