Skip to main content

NodeMapping

Struct NodeMapping 

Source
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:

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 -> parse

graphs::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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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().

Source

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.

Source

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.

Source

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

Source

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().

Source

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().

Source

pub fn is_partial(&self) -> bool

Returns whether some index is not mapped. Equivalent to mapped_count() < index_count().

Time: O(1).

Source

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).

Source

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

Source§

fn clone(&self) -> NodeMapping

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for NodeMapping

Source§

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

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

impl Eq for NodeMapping

Source§

impl Hash for NodeMapping

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IndexMapping for NodeMapping

Source§

fn get(&self, id: u32) -> Option<u32>

Returns the index that id maps to, or None if id is not mapped - Java’s -1. Read more
Source§

fn is_mapped(&self, id: u32) -> bool

Returns whether id is mapped. Equivalent to get(id).is_some(). Read more
Source§

fn is_partial(&self) -> bool

Returns whether some index is not mapped. Equivalent to mapped_count() < index_count(). Read more
Source§

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. Read more
Source§

fn index_count(&self) -> u32

Returns the number of indices this mapping maps from, mapped or not. Read more
Source§

impl PartialEq for NodeMapping

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for NodeMapping

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.