Skip to content
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

Add trait impls for refs #94

Merged
merged 3 commits into from
Jul 26, 2023
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
1 change: 1 addition & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Abstractions over portgraph representations.

pub mod filter;
pub mod refs;
pub mod region;

#[cfg(feature = "petgraph")]
Expand Down
88 changes: 88 additions & 0 deletions src/view/refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//! Trait implementations for references
//!
//! Note: The `auto_impl` crate would do all of this for us,
//! but it does not support GATs at the moment.

use crate::{Direction, NodeIndex, PortIndex, PortOffset};

use delegate::delegate;

use super::{LinkView, PortView};

impl<'g, G: PortView> PortView for &'g G {
type Nodes<'a> = G::Nodes<'a>
where
Self: 'a;

type Ports<'a> = G::Ports<'a>
where
Self: 'a;

type NodePorts<'a> = G::NodePorts<'a>
where
Self: 'a;

type NodePortOffsets<'a> = G::NodePortOffsets<'a>
where
Self: 'a;

delegate! {
to (*self) {
fn port_direction(&self, port: impl Into<PortIndex>) -> Option<Direction>;
fn port_node(&self, port: impl Into<PortIndex>) -> Option<NodeIndex>;
fn port_offset(&self, port: impl Into<PortIndex>) -> Option<PortOffset>;
fn port_index(&self, node: NodeIndex, offset: PortOffset) -> Option<PortIndex>;
fn ports(&self, node: NodeIndex, direction: Direction) -> Self::NodePorts<'_>;
fn all_ports(&self, node: NodeIndex) -> Self::NodePorts<'_>;
fn input(&self, node: NodeIndex, offset: usize) -> Option<PortIndex>;
fn output(&self, node: NodeIndex, offset: usize) -> Option<PortIndex>;
fn num_ports(&self, node: NodeIndex, direction: Direction) -> usize;
fn port_offsets(&self, node: NodeIndex, direction: Direction) -> Self::NodePortOffsets<'_>;
fn all_port_offsets(&self, node: NodeIndex) -> Self::NodePortOffsets<'_>;
fn contains_node(&self, node: NodeIndex) -> bool;
fn contains_port(&self, port: PortIndex) -> bool;
fn is_empty(&self) -> bool;
fn node_count(&self) -> usize;
fn port_count(&self) -> usize;
fn nodes_iter(&self) -> Self::Nodes<'_>;
fn ports_iter(&self) -> Self::Ports<'_>;
fn node_capacity(&self) -> usize;
fn port_capacity(&self) -> usize;
fn node_port_capacity(&self, node: NodeIndex) -> usize;
}
}
}

impl<'g, G: LinkView> LinkView for &'g G {
type LinkEndpoint = G::LinkEndpoint;

type Neighbours<'a> = G::Neighbours<'a>
where
Self: 'a;

type NodeConnections<'a> = G::NodeConnections<'a>
where
Self: 'a;

type NodeLinks<'a> = G::NodeLinks<'a>
where
Self: 'a;

type PortLinks<'a> = G::PortLinks<'a>
where
Self: 'a;

delegate! {
to (*self) {
fn get_connections(&self, from: NodeIndex, to: NodeIndex) -> Self::NodeConnections<'_>;
fn port_links(&self, port: PortIndex) -> Self::PortLinks<'_>;
fn links(&self, node: NodeIndex, direction: Direction) -> Self::NodeLinks<'_>;
fn all_links(&self, node: NodeIndex) -> Self::NodeLinks<'_>;
fn neighbours(&self, node: NodeIndex, direction: Direction) -> Self::Neighbours<'_>;
fn all_neighbours(&self, node: NodeIndex) -> Self::Neighbours<'_>;
fn link_count(&self) -> usize;
}
}
}

// TODO: PortMut, LinkMut, MultiView, MultiMut