Skip to content

feat: Add boundary edge traversal in SimpleReplacement #2231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions hugr-core/src/hugr/patch/port_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use derive_more::From;

/// A port in either the host or replacement graph.
///
/// This is used to represent boundary edges that will be added between the host and
/// replacement graphs when applying a rewrite.
#[derive(Debug, Clone, Copy)]
/// This is used to represent boundary edges that will be added between the host
/// and replacement graphs when applying a rewrite.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BoundaryPort<HostNode, P> {
/// A port in the host graph.
Host(HostNode, P),
Expand All @@ -19,22 +19,39 @@ pub enum BoundaryPort<HostNode, P> {
}

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

/// A port in the replacement graph.
#[derive(Debug, Clone, Copy, From, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, From, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ReplacementPort<P>(pub Node, pub P);

impl<HostNode: Copy, P> BoundaryPort<HostNode, P> {
/// Maps a boundary port according to the insertion mapping.
/// Host ports are unchanged, while Replacement ports are mapped according to the `index_map`.
/// Host ports are unchanged, while Replacement ports are mapped according
/// to the `index_map`.
pub fn map_replacement(self, index_map: &HashMap<Node, HostNode>) -> (HostNode, P) {
match self {
BoundaryPort::Host(node, port) => (node, port),
BoundaryPort::Replacement(node, port) => (*index_map.get(&node).unwrap(), port),
}
}

/// Returns the replacement port if this is a replacement port.
pub fn as_replacement(self) -> Option<(Node, P)> {
match self {
BoundaryPort::Replacement(node, port) => Some((node, port)),
BoundaryPort::Host(_, _) => None,
}
}

/// Returns the host port if this is a host port.
pub fn as_host(self) -> Option<(HostNode, P)> {
match self {
BoundaryPort::Host(node, port) => Some((node, port)),
BoundaryPort::Replacement(_, _) => None,
}
}
}

impl<N, P> From<HostPort<N, P>> for BoundaryPort<N, P> {
Expand Down
Loading
Loading