Module weights

Module weights 

Source
Expand description

A graph component that encodes node and port weights. For more complex scenarios, it is recommended to use a SecondaryMap.

This is a simple wrapper around two SecondaryMap containers. It does not keep track of key validity, and returns default values for missing keys. It is intended to be used alongside PortGraph.

§Example

let mut graph: PortGraph = PortGraph::new();
let mut weights = Weights::<usize, isize>::new();

// The weights must be set manually.
let node = graph.add_node(2, 2);
let [in0, in1, ..] = graph.inputs(node).collect::<Vec<_>>()[..] else { unreachable!() };
let [out0, out1, ..] = graph.outputs(node).collect::<Vec<_>>()[..] else { unreachable!() };
weights[node] = 42;
weights[in1] = 2;
weights[out0] = -1;
weights[out1] = -2;

/// Unset weights return the default value.
assert_eq!(weights[in0], 0);

// Graph operations that modify the keys have callbacks to update the weights.
graph.set_num_ports(node, 1, 3, |old, op| {
    op.new_index().map(|new| weights.ports.swap(old, new));
});

// The map does not track item removals, but the user can shrink it manually.
graph.remove_node(node);
weights.nodes.shrink_to(graph.node_count());
weights.ports.shrink_to(graph.port_count());

Structs§

Weights
Graph component that encodes node and port weights. Based on two UnmanagedDenseMap containers.