pub struct NodeMapping { /* private fields */ }Expand description
A mapping from the node IDs of one graph to those of another, to extract a subgraph or to renumber the nodes of a graph.
get(node) is the node ID that node maps to, or None if node is not mapped. The
factories return mappings from a new graph to an old one. These map each new node to the node it
comes from:
for_subgraph,for_subgraph_sortedandfor_subgraph_mask- the selected nodes, numbered in ascending order;for_permutation- every node, in an order of the caller’s choice;reindex- every node, in depth-first preorder;of- any mapping, from an array.
graphs::remapped builds the new graph from the old one, and
gather carries data on the old nodes, such as their names, over to the new
ones. Here, calls holds the calls between the methods of a program, and the methods that load
reaches become a graph of their own, with their names from names:
let new_to_old =
NodeMapping::for_subgraph_sorted(&graphs::reachable_from(&calls, load))?;
let loading = graphs::remapped(&calls, &new_to_old);
let loading_names = new_to_old.gather(names);
// loading_names: ["load", "parse", "read"]
// loading: load -> parse, load -> read, parse -> parsegraphs::edge_mapping derives the EdgeMapping that carries
data on edges. inverted returns the mapping from old to new, and
compose chains two mappings.
Instances are immutable and safe to share between threads (Send and Sync).
Memory: 4 x index_count bytes.
Implementations§
Source§impl NodeMapping
impl NodeMapping
Sourcepub fn of(mapping: &[u32]) -> Result<NodeMapping, GraphError>
pub fn of(mapping: &[u32]) -> Result<NodeMapping, GraphError>
Returns the mapping that maps each node i to mapping[i], and leaves it unmapped where that is
NONE - Java’s -1.
Use it for mappings that the other factories do not cover, from new to old or from old to new. The
slice is copied. Each value must be NONE or a node ID, and no two nodes may map to the same
node; without a graph, this function can only reject a value at or above 2^31, which is negative
in Java’s 32-bit IDs. graphs::remapped rejects a value that is not a
node of its graph, and both it and inverted reject two nodes that map to the
same node.
Time: O(mapping.len()). Memory: 4 x mapping.len() bytes for the copy, which the result keeps.
§Errors
An IndexOutOfBounds error if a value is neither NONE
nor below 2^31; an InvalidArgument error if the slice is
longer than the largest array, 2^31 - 9.
Sourcepub fn for_subgraph_mask(selected: &[bool]) -> Result<NodeMapping, GraphError>
pub fn for_subgraph_mask(selected: &[bool]) -> Result<NodeMapping, GraphError>
Returns a mapping from the nodes of a subgraph to the selected nodes, given as a mask: node v
is selected if selected[v] is true - Java’s overload forSubgraph(BitSet).
The selected nodes get the new node IDs 0..k, where k is the number of selected nodes, in
ascending order of their node IDs. The selected node IDs are checked against a graph only when the
mapping is used: graphs::remapped rejects one that is not a node of
its graph.
Time: O(selected.len()). Memory: 4 x k bytes for the result.
§Errors
An IndexOutOfBounds error if an entry at or above 2^31
is selected: that ID is negative in Java’s 32-bit IDs, which a BitSet cannot hold.
Sourcepub fn for_subgraph(selected: &[u32]) -> Result<NodeMapping, GraphError>
pub fn for_subgraph(selected: &[u32]) -> Result<NodeMapping, GraphError>
Returns a mapping from the nodes of a subgraph to the selected nodes, given as node IDs - Java’s
overloads forSubgraph(IntList) and forSubgraph(int...).
The selected nodes get the new node IDs 0..selected.len(), in ascending order of their node IDs,
whatever the order of the slice; the slice is sorted in a copy. For a SortedIntView, as
graphs::reachable_from and
NodePartition::nodes_of return it,
for_subgraph_sorted skips the sort. As with
for_subgraph_mask, the node IDs are checked against a graph only when
the mapping is used.
Time: O(k log k), where k is selected.len().
Memory: 4 x selected.len() bytes for the result.
§Errors
An IndexOutOfBounds error if a node ID is at or above
2^31, negative in Java’s 32-bit IDs; an InvalidArgument
error if a node ID occurs more than once.
Sourcepub fn for_subgraph_sorted(
selected: &SortedIntView<'_>,
) -> Result<NodeMapping, GraphError>
pub fn for_subgraph_sorted( selected: &SortedIntView<'_>, ) -> Result<NodeMapping, GraphError>
Returns a mapping from the nodes of a subgraph to the selected nodes, given as a
SortedIntView, which is taken as it is - the SortedIntView case of Java’s
forSubgraph(IntList).
The selected nodes get the new node IDs 0..selected.len(), in ascending order of their node IDs.
As with for_subgraph_mask, the node IDs are checked against a graph
only when the mapping is used.
Time: O(k), where k is selected.len().
Memory: 4 x selected.len() bytes for the result.
§Errors
An IndexOutOfBounds error if a node ID is at or above
2^31, negative in Java’s 32-bit IDs.
Sourcepub fn for_permutation(order: &[u32]) -> Result<NodeMapping, GraphError>
pub fn for_permutation(order: &[u32]) -> Result<NodeMapping, GraphError>
Returns a mapping that numbers the nodes in the given order, from new to old node IDs.
order[new_node] is the node that gets the new node ID new_node. order holds every node ID in
0..order.len() once, so the mapping maps every node. For example,
NodeMapping::for_permutation(&top_order::of(&dag)?) numbers the nodes of a DAG (directed acyclic
graph) in topological order, so that every edge of the remapped graph goes from a lower to a higher
node ID.
Time: O(order.len()). Memory: 4 x order.len() bytes for the result; while it runs, 1 bit per node.
§Errors
An IndexOutOfBounds error if a node ID is not in
0..order.len(); an InvalidArgument error if a node ID
occurs more than once or the slice is longer than the largest array, 2^31 - 9.
Sourcepub fn inverted(&self, target_count: u32) -> NodeMapping
pub fn inverted(&self, target_count: u32) -> NodeMapping
Returns the inverse mapping, which maps get(node) back to node.
The inverse maps the target_count nodes of the graph that this mapping maps to; a node that no
node maps to is not mapped, and the inverse is then partial. For a mapping from new to old node
IDs, as the factories return it, target_count is the node count of the old graph, and the
inverse gives the new node ID of an old node:
new_to_old.inverted(old_graph.node_count()).get(old_node).
Time: O(index_count + target_count). Memory: 4 x target_count bytes for the result.
§Panics
With an invalid argument message if target_count is larger than 2^31 - 9, or two nodes map
to the same node; with an index out of bounds message if a node maps to target_count or more.
Sourcepub fn compose(&self, inner: &NodeMapping) -> NodeMapping
pub fn compose(&self, inner: &NodeMapping) -> NodeMapping
Returns the mapping that applies inner first and then this mapping.
If inner maps the nodes of a graph A to those of B, and this mapping maps B to C, the result
maps A to C. A node is not mapped in the result if inner does not map it, or if this mapping
does not map the node that inner maps it to. For mappings from new to old node IDs,
first_to_original.compose(&second_to_first) maps the nodes of a graph remapped twice back to
those of the original one.
Time: O(inner.index_count()). Memory: 4 x inner.index_count() bytes for the result.
§Panics
With an index out of bounds message if inner maps a node to a node ID outside
0..index_count().
Sourcepub fn gather<T: Copy + Default>(&self, values: &[T]) -> Vec<T>
pub fn gather<T: Copy + Default>(&self, values: &[T]) -> Vec<T>
Returns values renumbered by this mapping: the value at position i of the result is
values[get(i)], or T::default() - Java’s 0 - if i is not mapped.
For a mapping from new to old node IDs, as the factories return it, this carries data on the nodes
of the old graph over to the new graph. This is one generic function for every Copy type; Java
has one per primitive type. gather_into does the same for other types, such
as String.
Time: O(index_count).
Memory: size_of::<T>() x index_count bytes for the result.
§Panics
With an index out of bounds message if a node maps to a node ID outside values.
Sourcepub fn gather_into<T: Clone>(&self, values: &[T], new_values: &mut [T])
pub fn gather_into<T: Clone>(&self, values: &[T], new_values: &mut [T])
Writes values renumbered by this mapping into new_values, for values that are not Copy,
such as names - Java’s overload gather(T[], T[]).
new_values[i] becomes a clone of values[get(i)], and stays as it is if i is not mapped. If it
panics, new_values may be partly written. The borrow rules guarantee that the two slices are not
the same; Java checks this.
Time: O(index_count).
§Panics
With an invalid argument message if new_values.len() != index_count(); with an
index out of bounds message if a node maps to a node ID outside values.
Sourcepub fn reindex(graph: &Graph) -> NodeMapping
pub fn reindex(graph: &Graph) -> NodeMapping
Returns a mapping that numbers the nodes of graph in depth-first preorder, from new to old node
IDs.
The search starts at node 0, follows the out-nodes of each node in ascending order, and starts again at the smallest node not yet visited until it has visited every node; each node gets the next new node ID when the search reaches it. Nodes that the search visits one after another thus get adjacent node IDs. Traversals of the remapped graph then read its arrays more in order, which can make them faster on large graphs.
Time: O(node_count + edge_count). Memory: 4 x node_count bytes for the result; while it runs, 1 bit per node and up to 8 x node_count bytes for the stack, which grows with the deepest path.
§Panics
With an invalid argument message if a depth-first path is longer than 2^30 - 5 nodes.
Source§impl NodeMapping
impl NodeMapping
Sourcepub fn get(&self, id: u32) -> Option<u32>
pub fn get(&self, id: u32) -> Option<u32>
Returns the index that id maps to, or None if id is not mapped - Java’s -1.
In a partition, it is the group of id.
Time: O(1).
§Panics
With an index out of bounds message if id is not in 0..index_count().
Sourcepub fn is_mapped(&self, id: u32) -> bool
pub fn is_mapped(&self, id: u32) -> bool
Returns whether id is mapped. Equivalent to get(id).is_some().
Time: O(1).
§Panics
With an index out of bounds message if id is not in 0..index_count().
Sourcepub fn is_partial(&self) -> bool
pub fn is_partial(&self) -> bool
Returns whether some index is not mapped. Equivalent to mapped_count() < index_count().
Time: O(1).
Sourcepub fn mapped_count(&self) -> u32
pub fn mapped_count(&self) -> u32
Returns the number of indices that are mapped; in a partition, the number of indices that belong to a group.
Time: O(1).
Sourcepub fn index_count(&self) -> u32
pub fn index_count(&self) -> u32
Returns the number of indices this mapping maps from, mapped or not.
It is the length of the arrays that gather returns and that reduce takes.
Time: O(1).
Trait Implementations§
Source§impl Clone for NodeMapping
impl Clone for NodeMapping
Source§fn clone(&self) -> NodeMapping
fn clone(&self) -> NodeMapping
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for NodeMapping
impl Debug for NodeMapping
impl Eq for NodeMapping
Source§impl Hash for NodeMapping
impl Hash for NodeMapping
Source§impl IndexMapping for NodeMapping
impl IndexMapping for NodeMapping
Source§fn is_partial(&self) -> bool
fn is_partial(&self) -> bool
mapped_count() < index_count(). Read more