use crate::Gate;
pub fn eq(x: &Gate, y: &Gate) -> Gate {
!(x ^ y)
}
pub fn reduce_conjunction(i: impl IntoIterator<Item = Gate>) -> Gate {
reduce_associative(i.into_iter().collect::<Vec<_>>().as_slice(), |x, y| x & y)
.unwrap_or_else(|| true.into())
}
pub fn reduce_disjunction(i: impl IntoIterator<Item = Gate>) -> Gate {
reduce_associative(i.into_iter().collect::<Vec<_>>().as_slice(), |x, y| x | y)
.unwrap_or_else(|| false.into())
}
pub fn reduce_xor(i: impl IntoIterator<Item = Gate>) -> Gate {
reduce_associative(i.into_iter().collect::<Vec<_>>().as_slice(), |x, y| x ^ y).unwrap()
}
fn reduce_associative<T: Clone>(i: &[T], op: fn(&T, &T) -> T) -> Option<T> {
match i.len() {
0 => None,
1 => Some(i[0].clone()),
_ => {
let mid = i.len() / 2;
Some(op(
&reduce_associative(&i[..mid], op).unwrap(),
&reduce_associative(&i[mid..], op).unwrap(),
))
}
}
}