Struct portgraph::PortGraph

source ·
pub struct PortGraph { /* private fields */ }
Expand description

An unlabelled port graph.

A port graph consists of a collection of nodes identified by a NodeIndex. Each node has an ordered sequence of input and output ports, identified by a PortIndex that is unique within the graph. To optimize for the most common use case, the number of input and output ports of a node must be specified when the node is created.

When a node and its associated ports are removed their indices will be reused on a best effort basis when a new node is added. The indices of unaffected nodes and ports remain stable. PortGraph::compact_nodes and PortGraph::compact_ports to eliminate fragmentation in the index space.

Implementations§

source§

impl PortGraph

source

pub fn new() -> Self

Create a new empty PortGraph.

source

pub fn with_capacity(nodes: usize, ports: usize) -> Self

Create a new empty PortGraph with preallocated capacity.

source

pub fn add_node(&mut self, incoming: usize, outgoing: usize) -> NodeIndex

Adds a node to the portgraph with a given number of input and output ports.

Panics

Panics if the total number of ports exceeds u16::MAX.

Example
let mut g = PortGraph::new();
let node = g.add_node(4, 3);
assert_eq!(g.inputs(node).count(), 4);
assert_eq!(g.outputs(node).count(), 3);
assert!(g.contains_node(node));
source

pub fn remove_node(&mut self, node: NodeIndex)

Remove a node from the port graph. All ports of the node will be unlinked and removed as well. Does nothing if the node does not exist.

Example
let mut g = PortGraph::new();
let node0 = g.add_node(1, 1);
let node1 = g.add_node(1, 1);
g.link_ports(g.outputs(node0).nth(0).unwrap(), g.inputs(node1).nth(0).unwrap());
g.link_ports(g.outputs(node1).nth(0).unwrap(), g.inputs(node0).nth(0).unwrap());
g.remove_node(node0);
assert!(!g.contains_node(node0));
assert!(g.port_link(g.outputs(node1).nth(0).unwrap()).is_none());
assert!(g.port_link(g.inputs(node1).nth(0).unwrap()).is_none());

Link an output and an input port.

Example
let mut g = PortGraph::new();
let node0 = g.add_node(0, 1);
let node1 = g.add_node(1, 0);
let node0_output = g.output(node0, 0).unwrap();
let node1_input = g.input(node1, 0).unwrap();
g.link_ports(node0_output, node1_input).unwrap();
assert_eq!(g.port_link(node0_output), Some(node1_input));
assert_eq!(g.port_link(node1_input), Some(node0_output));
Errors
  • If port_a or port_b does not exist.
  • If port_a and port_b have the same direction.
  • If port_a or port_b is already linked.

Unlinks the port and returns the port it was linked to. Returns None when the port was not linked.

source

pub fn get_connections( &self, from: NodeIndex, to: NodeIndex ) -> NodeConnections<'_>

Returns an iterator over every pair of matching ports connecting from with to.

Example
let mut g = PortGraph::new();
let a = g.add_node(0, 2);
let b = g.add_node(2, 0);

g.link_nodes(a, 0, b, 0).unwrap();
g.link_nodes(a, 1, b, 1).unwrap();

let mut connections = g.get_connections(a, b);
assert_eq!(connections.next(), Some((g.output(a,0).unwrap(), g.input(b,0).unwrap())));
assert_eq!(connections.next(), Some((g.output(a,1).unwrap(), g.input(b,1).unwrap())));
assert_eq!(connections.next(), None);
source

pub fn get_connection( &self, from: NodeIndex, to: NodeIndex ) -> Option<(PortIndex, PortIndex)>

Checks whether there is a directed link between the two nodes and returns the first matching pair of ports.

Example
let mut g = PortGraph::new();
let a = g.add_node(0, 2);
let b = g.add_node(2, 0);

g.link_nodes(a, 0, b, 0).unwrap();
g.link_nodes(a, 1, b, 1).unwrap();

assert_eq!(g.get_connection(a, b), Some((g.output(a,0).unwrap(), g.input(b,0).unwrap())));
source

pub fn connected(&self, from: NodeIndex, to: NodeIndex) -> bool

Checks whether there is a directed link between the two nodes.

Example
let mut g = PortGraph::new();
let a = g.add_node(0, 2);
let b = g.add_node(2, 0);

g.link_nodes(a, 0, b, 0).unwrap();

assert!(g.connected(a, b));

Links two nodes at an input and output port offsets.

Errors
  • If the ports and nodes do not exist.
  • If the ports are already linked.

Links two nodes at an input and output port offsets.

Errors
  • If the ports and nodes do not exist.
  • If the ports have the same direction.
  • If the ports are already linked.
source

pub fn port_direction(&self, port: PortIndex) -> Option<Direction>

Returns the direction of the port.

source

pub fn port_node(&self, port: PortIndex) -> Option<NodeIndex>

Returns the node that the port belongs to.

source

pub fn port_offset(&self, port: PortIndex) -> Option<PortOffset>

Returns the index of a port within its node’s port list.

source

pub fn port_index( &self, node: NodeIndex, offset: PortOffset ) -> Option<PortIndex>

Returns the port index for a given node, direction, and offset.

Returns the port that the given port is linked to.

source

pub fn ports(&self, node: NodeIndex, direction: Direction) -> NodePorts

Iterates over all the ports of the node in the given direction.

source

pub fn all_ports(&self, node: NodeIndex) -> NodePorts

Iterates over the input and output ports of the node in sequence.

source

pub fn input(&self, node: NodeIndex, offset: usize) -> Option<PortIndex>

Returns the input port at the given offset in the node.

Shorthand for PortGraph::port_index.

source

pub fn output(&self, node: NodeIndex, offset: usize) -> Option<PortIndex>

Returns the output port at the given offset in the node.

Shorthand for PortGraph::ports.

source

pub fn inputs(&self, node: NodeIndex) -> NodePorts

Iterates over all the input ports of the node.

Shorthand for PortGraph::ports.

source

pub fn outputs(&self, node: NodeIndex) -> NodePorts

Iterates over all the output ports of the node.

Shorthand for PortGraph::ports.

source

pub fn num_inputs(&self, node: NodeIndex) -> usize

Returns the number of input ports of the node.

Shorthand for PortGraph::num_ports.

source

pub fn num_outputs(&self, node: NodeIndex) -> usize

Returns the number of output ports of the node.

Shorthand for PortGraph::num_ports.

source

pub fn num_ports(&self, node: NodeIndex, direction: Direction) -> usize

Returns the number of ports of the node in the given direction.

source

pub fn port_offsets( &self, node: NodeIndex, direction: Direction ) -> NodePortOffsets

Iterates over all the port offsets of the node in the given direction.

Example
let mut graph = PortGraph::new();
let node = graph.add_node(0, 2);

assert!(graph.links(node, Direction::Incoming).eq([]));
assert!(graph.port_offsets(node, Direction::Outgoing).eq([PortOffset::new_outgoing(0), PortOffset::new_outgoing(1)]));
source

pub fn input_offsets(&self, node: NodeIndex) -> NodePortOffsets

Iterates over all the input port offsets of the node.

Shorthand for PortGraph::port_offsets.

source

pub fn output_offsets(&self, node: NodeIndex) -> NodePortOffsets

Iterates over all the output port offsets of the node.

Shorthand for PortGraph::port_offsets.

source

pub fn all_port_offsets(&self, node: NodeIndex) -> NodePortOffsets

Iterates over the input and output port offsets of the node in sequence.

Iterates over the links of the node in the given direction. When the corresponding node port is linked to another one, the Option contains the index of the other port.

Examples

let mut graph = PortGraph::new();

let node_a = graph.add_node(0, 2);
let node_b = graph.add_node(1, 0);

let port_a = graph.outputs(node_a).next().unwrap();
let port_b = graph.inputs(node_b).next().unwrap();

graph.link_ports(port_a, port_b).unwrap();

assert!(graph.links(node_a, Direction::Outgoing).eq([Some(port_b), None]));
assert!(graph.links(node_b, Direction::Incoming).eq([Some(port_a)]));

Iterates over the input links of the node. Shorthand for PortGraph::links.

Iterates over the output links of the node. Shorthand for PortGraph::links.

Iterates over the input and output links of the node in sequence.

source

pub fn neighbours( &self, node: NodeIndex, direction: Direction ) -> Neighbours<'_>

Iterates over neighbour nodes in the given direction. May contain duplicates if the graph has multiple links between nodes.

Examples

let mut graph = PortGraph::new();

let a = graph.add_node(0, 1);
let b = graph.add_node(2, 1);

graph.link_nodes(a, 0, b, 0).unwrap();
graph.link_nodes(b, 0, b, 1).unwrap();

assert!(graph.neighbours(a, Direction::Outgoing).eq([b]));
assert!(graph.neighbours(b, Direction::Incoming).eq([a,b]));
source

pub fn input_neighbours(&self, node: NodeIndex) -> Neighbours<'_>

Iterates over the input neighbours of the node. Shorthand for PortGraph::neighbours.

source

pub fn output_neighbours(&self, node: NodeIndex) -> Neighbours<'_>

Iterates over the output neighbours of the node. Shorthand for PortGraph::neighbours.

source

pub fn all_neighbours(&self, node: NodeIndex) -> Neighbours<'_>

Iterates over the input and output neighbours of the node in sequence.

source

pub fn contains_node(&self, node: NodeIndex) -> bool

Returns whether the port graph contains the node.

source

pub fn contains_port(&self, port: PortIndex) -> bool

Returns whether the port graph contains the port.

source

pub fn is_empty(&self) -> bool

Returns whether the port graph has no nodes nor ports.

source

pub fn node_count(&self) -> usize

Returns the number of nodes in the port graph.

source

pub fn port_count(&self) -> usize

Returns the number of ports in the port graph.

Returns the number of links between ports.

source

pub fn nodes_iter(&self) -> Nodes<'_>

Iterates over the nodes in the port graph.

source

pub fn ports_iter(&self) -> Ports<'_>

Iterates over the ports in the port graph.

source

pub fn clear(&mut self)

Removes all nodes and ports from the port graph.

source

pub fn node_capacity(&self) -> usize

Returns the capacity of the underlying buffer for nodes.

source

pub fn port_capacity(&self) -> usize

Returns the capacity of the underlying buffer for ports.

source

pub fn node_port_capacity(&self, node: NodeIndex) -> usize

Returns the allocated port capacity for a specific node.

Changes to the number of ports of the node will not reallocate until the number of ports exceeds this capacity.

source

pub fn reserve(&mut self, nodes: usize, ports: usize)

Reserves enough capacity to insert at least the given number of additional nodes and ports.

This method does not take into account the length of the free list and might overallocate speculatively.

source

pub fn set_num_ports<F>( &mut self, node: NodeIndex, incoming: usize, outgoing: usize, rekey: F )where F: FnMut(PortIndex, PortOperation),

Changes the number of ports of the node to the given incoming and outgoing counts.

Invalidates the indices of the node’s ports. If the number of incoming or outgoing ports is reduced, the ports are removed from the end of the port list.

Every time a port is moved, the rekey function will be called with its old and new index. If the port is removed, the new index will be None.

This operation is O(n) where n is the number of ports of the node.

source

pub fn compact_nodes<F>(&mut self, rekey: F)where F: FnMut(NodeIndex, NodeIndex),

Compacts the storage of nodes in the portgraph so that all nodes are stored consecutively.

Every time a node is moved, the rekey function will be called with its old and new index.

source

pub fn compact_ports<F>(&mut self, rekey: F)where F: FnMut(PortIndex, PortIndex),

Compacts the storage of ports in the portgraph so that all ports are stored consecutively.

Shrinks the port capacity of every node to match the number of ports it contains.

Every time a port is moved, the rekey function will be called with is old and new index.

source

pub fn shrink_to_fit(&mut self)

Shrinks the underlying buffers to the fit the data.

This does not move nodes or ports, which might prevent freeing up more capacity. To shrink the buffers as much as possible, call PortGraph::compact_nodes and PortGraph::compact_ports first.

source§

impl PortGraph

source

pub fn apply_rewrite(&mut self, rewrite: Rewrite) -> Result<(), RewriteError>

Applies a rewrite to the graph.

source

pub fn apply_weighted_rewrite<N: Clone, P: Clone>( &mut self, rewrite: WeightedRewrite<N, P>, weights: &mut Weights<N, P> ) -> Result<(), RewriteError>

Applies a rewrite to the graph and associated weights.

source

pub fn apply_rewrite_with_callbacks( &mut self, rewrite: Rewrite, node_removed: impl FnMut(NodeIndex), port_removed: impl FnMut(PortIndex), node_inserted: impl FnMut(NodeIndex, NodeIndex), port_inserted: impl FnMut(PortIndex, PortIndex), ports_connected: impl FnMut(PortIndex, PortIndex) ) -> Result<(), RewriteError>

Applies a rewrite to the graph, calling the provided callbacks as the rewrite is applied.

Parameters
  • rewrite: The rewrite to apply.
  • node_removed: A callback that is called with the index of each node that is removed.
  • port_removed: A callback that is called with the index of each port that is removed.
  • node_inserted: A callback that is called with the old node index in the rewrite’s OpenGraph, and the new node index in the graph.
  • port_inserted: A callback that is called with the old port index in the rewrite’s OpenGraph, and the new port index in the graph.
  • ports_connected: A callback that is called when two ports are connected.

Trait Implementations§

source§

impl Clone for PortGraph

source§

fn clone(&self) -> PortGraph

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PortGraph

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for PortGraph

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl PartialEq<PortGraph> for PortGraph

source§

fn eq(&self, other: &PortGraph) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for PortGraph

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> Twhere Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pipe for Twhere T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> Rwhere Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.