From 7bcca553fc73fea28ae8d4cd76614d51b0289d9d Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Sun, 24 Jul 2022 19:04:08 +0200 Subject: [PATCH] Added find_unconnected_ports --- src/matrix.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/matrix.rs b/src/matrix.rs index 5519680..6dd169e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -379,7 +379,34 @@ impl Cell { } } - free_ports.to_vec() + free_ports + } + + /// Finds all dangling ports in the specified direction. + /// With `dir` you can specify input with `CellDir::T`, output with `CellDir::B` + /// and any with `CellDir::C`. + pub fn find_unconnected_ports( + &self, + m: &Matrix, + dir: CellDir, + ) -> Vec<(CellDir, (usize, usize))> { + let mut unused_ports = vec![]; + + let options: &[CellDir] = if dir == CellDir::C { + &[CellDir::T, CellDir::TL, CellDir::BL, CellDir::TR, CellDir::BR, CellDir::B] + } else if dir.is_input() { + &[CellDir::T, CellDir::TL, CellDir::BL] + } else { + &[CellDir::TR, CellDir::BR, CellDir::B] + }; + + for dir in options { + if let Some(pos) = self.is_port_dir_connected(m, *dir) { + unused_ports.push((*dir, pos)); + } + } + + unused_ports } /// If the port is connected, it will return the position of the other cell.