From f94b1b6825ea3515b4b16e0832a4844f8342b1ba Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 29 Nov 2023 00:08:45 +0200 Subject: Changed Added pins to AddGate that represents all the and Gate pins and made input and output reference pins ids and modified the evaluate function accordingly --- src/gate.rs | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/gate.rs b/src/gate.rs index 1baee11..e6172df 100644 --- a/src/gate.rs +++ b/src/gate.rs @@ -127,46 +127,55 @@ impl PartialEq for NotGate { #[derive(Debug, Clone)] pub struct AndGate { id: usize, - input: Pins, - output: Pin, + pins: HashMap, + input: Vec, + output: usize, } impl AndGate { fn new(input: Vec, id: usize, kind: PinType) -> Self { if input.is_empty() { - let pin_1 = Pin::new(PinType::GateInput, id, 42); - let pin_2 = Pin::new(PinType::GateInput, id, 42); + let pin_out = Pin::new(PinType::GateOutput, id, 42); + let mut pins = HashMap::new(); + pins.insert(pin_out.id, pin_out); Self { id, - input: vec![pin_1, pin_2], - output: Pin::new(PinType::Undetermined, id, 42), + pins, + input: vec![], + output: pin_out.id, } } else if input.len() == 1 { let pin_1 = Pin::new(kind, id, input[0]); + let pin_out = Pin::new(PinType::GateOutput, id, 42); + let mut pins = HashMap::new(); + pins.insert(pin_1.id, pin_1); + pins.insert(pin_out.id, pin_out); Self { id, - input: vec![pin_1, Pin::new(PinType::Undetermined, id, 42)], - output: Pin::new(PinType::Undetermined, id, 42), + pins, + input: vec![pin_1.id], + output: pin_out.id, } } else { let pin_1 = Pin::new(kind, id, input[0]); let pin_2 = Pin::new(kind, id, input[1]); + let pin_out = Pin::new(PinType::GateOutput, id, 42); + let mut pins = HashMap::new(); + pins.insert(pin_1.id, pin_1); + pins.insert(pin_2.id, pin_2); + pins.insert(pin_out.id, pin_out); Self { id, - input: vec![pin_1, pin_2], - output: Pin::new(PinType::Undetermined, id, 42), + pins, + input: vec![pin_1.id, pin_2.id], + output: pin_out.id, } } } - fn evaluate(&mut self) -> bool { - let res = (self.input[0].val.unwrap() & self.input[1].val.unwrap()) == 1; - self.output.val = Some(res as u8); - self.output.kind = PinType::GateOutput; - println!("{:?}", self); - res + pub fn pin(&self, id: usize) -> &Pin { + self.pins.get(&id).expect("pin should exist") } -} #[derive(Debug, Clone)] pub struct OrGate { -- cgit v1.2.3