Skip to content

Commit 7cd85d7

Browse files
committed
feat: PersistentHugr implements HugrView
1 parent fc61376 commit 7cd85d7

File tree

10 files changed

+1427
-68
lines changed

10 files changed

+1427
-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)]
22+
#[derive(Debug, Clone, Copy, From, Into, PartialEq, Eq, PartialOrd, Ord)]
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
@@ -446,10 +446,8 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
446446
Some(ReplacementPort(rep_output, incoming_port))
447447
}
448448

449-
/// Get the incoming ports in the input boundary of `subgraph` that
450-
/// correspond to the given output port at the input node of `replacement`
451-
///
452-
/// Return ports in `self.subgraph().incoming_ports()`.
449+
/// Get the incoming port in `subgraph` that corresponds to the given
450+
/// replacement input port.
453451
///
454452
/// This panics if self.replacement is not a DFG.
455453
pub fn map_replacement_input(

0 commit comments

Comments
 (0)