Skip to main content

flow_gate_core/gate/
boolean.rs

1use crate::error::FlowGateError;
2use crate::traits::{Gate, GateId, ParameterName};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum BooleanOp {
6    And,
7    Or,
8    Not,
9}
10
11#[derive(Debug, Clone)]
12pub struct BooleanOperand {
13    pub gate_id: GateId,
14    pub complement: bool,
15}
16
17#[derive(Debug, Clone)]
18pub struct BooleanGate {
19    id: GateId,
20    parent_id: Option<GateId>,
21    op: BooleanOp,
22    operands: Vec<BooleanOperand>,
23    dim_names: Vec<ParameterName>,
24}
25
26impl BooleanGate {
27    pub fn new(
28        id: GateId,
29        parent_id: Option<GateId>,
30        op: BooleanOp,
31        operands: Vec<BooleanOperand>,
32    ) -> Result<Self, FlowGateError> {
33        if operands.is_empty() {
34            return Err(FlowGateError::BooleanEmptyOperands(id));
35        }
36        if op == BooleanOp::Not && operands.len() != 1 {
37            return Err(FlowGateError::BooleanNotArity(id, operands.len()));
38        }
39        Ok(Self {
40            id,
41            parent_id,
42            op,
43            operands,
44            dim_names: Vec::new(),
45        })
46    }
47
48    pub fn op(&self) -> BooleanOp {
49        self.op
50    }
51
52    pub fn operands(&self) -> &[BooleanOperand] {
53        &self.operands
54    }
55}
56
57impl Gate for BooleanGate {
58    fn dimensions(&self) -> &[ParameterName] {
59        &self.dim_names
60    }
61
62    fn contains(&self, _coords: &[f64]) -> bool {
63        false
64    }
65
66    fn gate_id(&self) -> &GateId {
67        &self.id
68    }
69
70    fn parent_id(&self) -> Option<&GateId> {
71        self.parent_id.as_ref()
72    }
73}