From 06150f51675e75d6f217e715b8df8e3ac277bb0f Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Thu, 23 May 2024 20:00:43 +0200 Subject: [PATCH] autorouter: don't cache nodes in `Selection` This solves the problem of having to use stateful deserialization. --- src/autorouter/autorouter.rs | 4 ++- src/autorouter/selection.rs | 49 +++++------------------------------- src/bin/topola-egui/app.rs | 12 ++++++--- 3 files changed, 17 insertions(+), 48 deletions(-) diff --git a/src/autorouter/autorouter.rs b/src/autorouter/autorouter.rs index 0467762..346c034 100644 --- a/src/autorouter/autorouter.rs +++ b/src/autorouter/autorouter.rs @@ -183,7 +183,9 @@ impl Autorouter { .unwrap() .vertex_index(); - selection.contains(source_vertex.into()) && selection.contains(to_vertex.into()) + let layout = self.layout.lock().unwrap(); + selection.contains_node(&layout, source_vertex.into()) + && selection.contains_node(&layout, to_vertex.into()) }) .collect() } diff --git a/src/autorouter/selection.rs b/src/autorouter/selection.rs index b376fe1..cef6698 100644 --- a/src/autorouter/selection.rs +++ b/src/autorouter/selection.rs @@ -10,18 +10,12 @@ use crate::{ #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct Selection { pins: HashSet, - #[serde(skip)] - primitives: HashSet, - #[serde(skip)] - zones: HashSet>, } impl Selection { pub fn new() -> Selection { Self { pins: HashSet::new(), - primitives: HashSet::new(), - zones: HashSet::new(), } } @@ -29,7 +23,7 @@ impl Selection { let maybe_pin = layout.node_pin(node); if let Some(ref pin) = maybe_pin { - if self.contains(node) { + if self.contains_node(layout, node) { self.remove_pin(layout, pin); } else { self.add_pin(layout, pin); @@ -38,49 +32,18 @@ impl Selection { } fn add_pin(&mut self, layout: &Layout, pin: &String) { - for primitive in layout.drawing().primitive_nodes().filter(|primitive| { - layout - .node_pin(NodeIndex::Primitive(*primitive)) - .is_some_and(|p| p == pin) - }) { - self.primitives.insert(primitive); - } - - for zone in layout.zone_nodes().filter(|zone| { - layout - .node_pin(NodeIndex::Compound(*zone)) - .is_some_and(|p| p == pin) - }) { - self.zones.insert(zone); - } - self.pins.insert(pin.clone()); } fn remove_pin(&mut self, layout: &Layout, pin: &String) { - for primitive in layout.drawing().primitive_nodes().filter(|primitive| { - layout - .node_pin(NodeIndex::Primitive(*primitive)) - .is_some_and(|p| p == pin) - }) { - self.primitives.remove(&primitive); - } - - for zone in layout.zone_nodes().filter(|zone| { - layout - .node_pin(NodeIndex::Compound(*zone)) - .is_some_and(|p| p == pin) - }) { - self.zones.remove(&zone); - } - self.pins.remove(pin); } - pub fn contains(&self, node: NodeIndex) -> bool { - match node { - NodeIndex::Primitive(primitive) => self.primitives.contains(&primitive), - NodeIndex::Compound(zone) => self.zones.contains(&zone), + pub fn contains_node(&self, layout: &Layout, node: NodeIndex) -> bool { + if let Some(pin) = layout.node_pin(node) { + self.pins.contains(pin) + } else { + false } } } diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index b1ae749..a5e31eb 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -363,7 +363,7 @@ impl eframe::App for App { let color = if shared_data.highlighteds.contains(&primitive) || overlay .selection() - .contains(GenericNode::Primitive(primitive)) + .contains_node(&layout, GenericNode::Primitive(primitive)) { egui::Color32::from_rgb(100, 100, 255) } else { @@ -373,7 +373,9 @@ impl eframe::App for App { } for zone in layout.layer_zone_nodes(1) { - let color = if overlay.selection().contains(GenericNode::Compound(zone)) + let color = if overlay + .selection() + .contains_node(&layout, GenericNode::Compound(zone)) { egui::Color32::from_rgb(100, 100, 255) } else { @@ -388,7 +390,7 @@ impl eframe::App for App { let color = if shared_data.highlighteds.contains(&primitive) || overlay .selection() - .contains(GenericNode::Primitive(primitive)) + .contains_node(&layout, GenericNode::Primitive(primitive)) { egui::Color32::from_rgb(255, 100, 100) } else { @@ -398,7 +400,9 @@ impl eframe::App for App { } for zone in layout.layer_zone_nodes(0) { - let color = if overlay.selection().contains(GenericNode::Compound(zone)) + let color = if overlay + .selection() + .contains_node(&layout, GenericNode::Compound(zone)) { egui::Color32::from_rgb(255, 100, 100) } else {