Skip to content

Commit 6659c40

Browse files
committed
feat: Add PersistentHugr
1 parent 553350c commit 6659c40

File tree

10 files changed

+1325
-59
lines changed

10 files changed

+1325
-59
lines changed

Cargo.lock

+69-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hugr-core/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ thiserror = { workspace = true }
5757
typetag = { workspace = true }
5858
semver = { workspace = true, features = ["serde"] }
5959
zstd = { workspace = true, optional = true }
60+
relrc = { git = "https://github.com/lmondada/relrc", rev = "f0e96e8", features = [
61+
"petgraph",
62+
] }
6063

6164
[dev-dependencies]
6265
rstest = { workspace = true }

hugr-core/src/hugr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod hugrmut;
55
pub(crate) mod ident;
66
pub mod internal;
77
pub mod patch;
8+
pub mod persistent;
89
pub mod serialize;
910
pub mod validate;
1011
pub mod views;

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

+35
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,41 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
246246
self.nu_inp.get(&(node, port)).copied().map(Into::into)
247247
}
248248

249+
/// Map the host nodes in `self` according to `node_map`.
250+
///
251+
/// `node_map` must map nodes in the current hugr of the subgraph to
252+
/// its equivalent nodes in `new_hugr`.
253+
///
254+
/// This converts a replacement that acts on nodes of type `HostNode` to
255+
/// a replacement that acts on `new_hugr`, with nodes of type `N`.
256+
///
257+
/// This does not check convexity. It is up to the caller to ensure that
258+
/// the mapped replacement obtained from this applies on a convex subgraph
259+
/// of the new Hugr.
260+
pub(crate) fn map_host_nodes<N: HugrNode>(
261+
&self,
262+
node_map: impl Fn(HostNode) -> N,
263+
) -> SimpleReplacement<N> {
264+
let Self {
265+
subgraph,
266+
replacement,
267+
nu_inp,
268+
nu_out,
269+
} = self;
270+
let nu_inp = nu_inp
271+
.iter()
272+
.map(|(&(node, port), &(host_node, host_port))| {
273+
((node, port), (node_map(host_node), host_port))
274+
})
275+
.collect();
276+
let nu_out = nu_out
277+
.into_iter()
278+
.map(|(&(host_node, host_port), &port)| ((node_map(host_node), host_port), port))
279+
.collect();
280+
let subgraph = subgraph.map_nodes(node_map);
281+
SimpleReplacement::new(subgraph, replacement.clone(), nu_inp, nu_out)
282+
}
283+
249284
/// Get all edges that the replacement would add between `host` and
250285
/// `self.replacement`.
251286
///

0 commit comments

Comments
 (0)