Skip to content

Commit 70bec95

Browse files
lmondadaaborgna-q
andauthored
feat: PersistentHugr implements HugrView (#2202)
Closes #2095 --------- Co-authored-by: Agustín Borgna <121866228+aborgna-q@users.noreply.github.com>
1 parent 95da129 commit 70bec95

File tree

10 files changed

+1423
-68
lines changed

10 files changed

+1423
-68
lines changed

hugr-core/src/hugr/internal.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::sync::OnceLock;
66
use itertools::Itertools;
77
use portgraph::{LinkMut, LinkView, MultiPortGraph, PortMut, PortOffset, PortView};
88

9+
use crate::core::HugrNode;
910
use crate::extension::ExtensionRegistry;
1011
use crate::{Direction, Hugr, Node};
1112

@@ -91,6 +92,21 @@ impl PortgraphNodeMap<Node> for DefaultPGNodeMap {
9192
}
9293
}
9394

95+
impl<N: HugrNode> PortgraphNodeMap<N> for std::collections::HashMap<N, Node> {
96+
#[inline]
97+
fn to_portgraph(&self, node: N) -> portgraph::NodeIndex {
98+
self[&node].into_portgraph()
99+
}
100+
101+
#[inline]
102+
fn from_portgraph(&self, node: portgraph::NodeIndex) -> N {
103+
let node = node.into();
104+
self.iter()
105+
.find_map(|(&k, &v)| (v == node).then_some(k))
106+
.expect("Portgraph node not found in map")
107+
}
108+
}
109+
94110
impl HugrInternals for Hugr {
95111
type RegionPortgraph<'p>
96112
= &'p MultiPortGraph

hugr-core/src/hugr/patch/port_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44

55
use crate::{IncomingPort, Node, OutgoingPort, Port};
66

7-
use derive_more::From;
7+
use derive_more::{From, derive::Into};
88

99
/// A port in either the host or replacement graph.
1010
///
@@ -19,7 +19,7 @@ pub enum BoundaryPort<HostNode, P> {
1919
}
2020

2121
/// A port in the host graph.
22-
#[derive(Debug, Clone, Copy, From, PartialEq, Eq, PartialOrd, Ord, Hash)]
22+
#[derive(Debug, Clone, Copy, From, Into, PartialEq, Eq, PartialOrd, Ord, Hash)]
2323
pub struct HostPort<N, P>(pub N, pub P);
2424

2525
/// A port in the replacement graph.

hugr-core/src/hugr/patch/simple_replace.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,8 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
442442
Some(ReplacementPort(rep_output, incoming_port))
443443
}
444444

445-
/// Get the incoming ports in the input boundary of `subgraph` that
446-
/// correspond to the given output port at the input node of `replacement`
447-
///
448-
/// Return ports in `self.subgraph().incoming_ports()`.
445+
/// Get the incoming port in `subgraph` that corresponds to the given
446+
/// replacement input port.
449447
///
450448
/// This panics if self.replacement is not a DFG.
451449
pub fn map_replacement_input(

0 commit comments

Comments
 (0)