Skip to content

Commit cb812be

Browse files
committed
Squashed commit of feat/persistenthugr changes
1 parent cb2384b commit cb812be

File tree

10 files changed

+1219
-5
lines changed

10 files changed

+1219
-5
lines changed

Cargo.lock

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

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pest_derive = "2.8.0"
8383
pretty = "0.12.4"
8484
pretty_assertions = "1.4.1"
8585
zstd = "0.13.2"
86+
relrc = "0.4.1"
8687

8788
# These public dependencies usually require breaking changes downstream, so we
8889
# try to be as permissive as possible.

hugr-core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ thiserror = { workspace = true }
5454
typetag = { workspace = true }
5555
semver = { workspace = true, features = ["serde"] }
5656
zstd = { workspace = true, optional = true }
57+
relrc = { workspace = true, features = ["petgraph"] }
5758

5859
[dev-dependencies]
5960
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

+47
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ impl<N: HugrNode> OutputBoundaryMap<N> {
147147
}
148148
}
149149
}
150+
151+
fn map_nodes<M: HugrNode>(&self, node_map: impl Fn(N) -> M) -> OutputBoundaryMap<M> {
152+
match self {
153+
OutputBoundaryMap::ByIncoming(map) => OutputBoundaryMap::ByIncoming(
154+
map.iter()
155+
.map(|(&(node, port), &val)| ((node_map(node), port), val))
156+
.collect(),
157+
),
158+
OutputBoundaryMap::ByOutgoing(map) => OutputBoundaryMap::ByOutgoing(
159+
map.iter()
160+
.map(|(&(node, port), &val)| ((node_map(node), port), val))
161+
.collect(),
162+
),
163+
}
164+
}
150165
}
151166

152167
impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
@@ -392,6 +407,38 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
392407
.chain(outgoing_boundary)
393408
.chain(host_to_host_boundary)
394409
}
410+
411+
/// Map the host nodes in `self` according to `node_map`.
412+
///
413+
/// `node_map` must map nodes in the current HUGR of the subgraph to
414+
/// its equivalent nodes in some `new_hugr`.
415+
///
416+
/// This converts a replacement that acts on nodes of type `HostNode` to
417+
/// a replacement that acts on `new_hugr`, with nodes of type `N`.
418+
///
419+
/// This does not check convexity. It is up to the caller to ensure that
420+
/// the mapped replacement obtained from this applies on a convex subgraph
421+
/// of the new HUGR.
422+
pub(crate) fn map_host_nodes<N: HugrNode>(
423+
&self,
424+
node_map: impl Fn(HostNode) -> N,
425+
) -> SimpleReplacement<N> {
426+
let Self {
427+
subgraph,
428+
replacement,
429+
nu_inp,
430+
nu_out,
431+
} = self;
432+
let nu_inp = nu_inp
433+
.iter()
434+
.map(|(&repl_node_port, &(host_node, host_port))| {
435+
(repl_node_port, (node_map(host_node), host_port))
436+
})
437+
.collect();
438+
let nu_out = nu_out.map_nodes(&node_map);
439+
let subgraph = subgraph.map_nodes(node_map);
440+
SimpleReplacement::new(subgraph, replacement.clone(), nu_inp, nu_out)
441+
}
395442
}
396443

397444
impl<HostNode: HugrNode> PatchVerification for SimpleReplacement<HostNode> {

0 commit comments

Comments
 (0)