pub struct NodePartition { /* private fields */ }Expand description
A partition of the nodes of a graph into groups, such as its components.
get(node) returns the group of node, or None if it is in no group, and
nodes_of the nodes of a group in ascending order.
components returns partitions into components, in which every node is in
exactly one group: an Sccs for the strongly connected components, a plain
NodePartition for the weakly connected ones. NodePartition::of takes groups of the caller’s
choice, which may leave nodes out.
A partition holds the graph whose nodes it partitions. condense builds the
condensed graph, with a node for each group and an edge between two groups wherever an edge of the
graph runs between them. graphs::edge_partition builds the
EdgePartition that tells which edges of the graph are behind each edge of the condensed graph.
The functions for groups of any kind, such as group_count,
size_of and reduce, come from IndexPartition.
use graphity_core::{graphs, GraphBuilder, NodePartition, NONE};
let mut b = GraphBuilder::new();
b.add_nodes(4);
b.add_path(&[0, 1, 2, 3]);
let graph = b.build();
let packages = NodePartition::of(&graph, &[0, 0, 1, NONE], 2).unwrap();
assert_eq!(packages.nodes_of(0), [0, 1]);
let condensed = packages.condense(); // node IDs are group IDs
assert_eq!(condensed.out_nodes(0), [1]);
let behind = graphs::edge_partition(&condensed, &packages);
assert_eq!(behind.edges_of(0), [1]); // the edge 1 -> 2 behind 0 -> 1Instances are immutable and safe to share between threads (Send and Sync).
Memory: 4 x (node_count + mapped_count + group_count) bytes, where mapped_count is the number of nodes that belong to a group; the graph is shared, not copied.
Implementations§
Source§impl NodePartition
impl NodePartition
Sourcepub fn of(
graph: &Graph,
group_of: &[u32],
group_count: u32,
) -> Result<NodePartition, GraphError>
pub fn of( graph: &Graph, group_of: &[u32], group_count: u32, ) -> Result<NodePartition, GraphError>
Returns a partition of the nodes of graph into groups of the caller’s choice.
group_of[node] is the group of each node, in 0..group_count, or NONE for a node in no
group; the slice is copied. A group may have no nodes: it becomes a node without edges in
condense, so that group IDs from a numbering of the caller stay valid. Here,
package_of holds the package of each method of calls as a node ID of the package graph
dependencies, and the condensed graph shows which packages call which:
let by_package = NodePartition::of(&calls, &package_of, dependencies.node_count())?;
let package_calls = by_package.condense();
// package_calls: app -> [cli, ui, service], ui -> [model],
// service -> [model, storage]Time: O(node_count + group_count). Memory: 4 x (node_count + mapped_count + group_count) bytes for the result.
§Errors
An IndexOutOfBounds error if a group is neither NONE
nor in 0..group_count; an InvalidArgument error if
group_of.len() != graph.node_count(), or group_count is larger than a graph can have nodes,
2^31 - 10.
Sourcepub fn node_count(&self) -> u32
pub fn node_count(&self) -> u32
Returns the number of nodes of the partitioned graph, in a group or not. Equivalent to
index_count.
Time: O(1).
Sourcepub fn nodes_of(&self, group: u32) -> SortedIntView<'_>
pub fn nodes_of(&self, group: u32) -> SortedIntView<'_>
Returns the nodes in group group, in ascending order. Equivalent to
indices_of(group).
The result is a view of this partition’s own array, not a copy.
Time: O(1).
§Panics
With an index out of bounds message if group is not in 0..group_count().
Sourcepub fn condense(&self) -> Graph
pub fn condense(&self) -> Graph
Returns the condensed graph, with a node for each group and an edge between two groups wherever an edge of the partitioned graph runs between them.
The node IDs of the condensed graph are the group IDs. It has the edge g -> h if a node in group
g has an out-node in group h and g != h. Edges within a group and edges from or to a node in
no group are left out. The edges from one group to another become a single edge.
graphs::edge_partition tells which edges are behind each edge of
the condensed graph. For an Sccs, the condensed graph is acyclic.
The condensed graph is built anew on every call, not cached: keep the result to use it more than once.
Time: O(node_count + edge_count + group_count), plus O(d log d) for each group with d out-nodes in the result. Memory: 4 x (group_count + condensed_edge_count) bytes for the result, where condensed_edge_count is its edge count; while it runs, 1 bit per group and up to 6 x condensed_edge_count bytes more.
Source§impl NodePartition
impl NodePartition
Sourcepub fn group_count(&self) -> u32
pub fn group_count(&self) -> u32
Returns the number of groups.
Time: O(1).
Sourcepub fn indices_of(&self, group: u32) -> SortedIntView<'_>
pub fn indices_of(&self, group: u32) -> SortedIntView<'_>
Returns the indices in group group, in ascending order.
The result is a view of this partition’s own array, not a copy.
Time: O(1).
§Panics
With an index out of bounds message if group is not in 0..group_count().
Sourcepub fn size_of(&self, group: u32) -> u32
pub fn size_of(&self, group: u32) -> u32
Returns the number of indices in group group.
Time: O(1).
§Panics
With an index out of bounds message if group is not in 0..group_count().
Sourcepub fn reduce<T: Copy>(
&self,
values: &[T],
identity: T,
combine: impl FnMut(T, T) -> T,
) -> Vec<T>
pub fn reduce<T: Copy>( &self, values: &[T], identity: T, combine: impl FnMut(T, T) -> T, ) -> Vec<T>
Returns one value per group, combined from the values of its indices.
For each group, starts from identity and combines the result so far with the value of each index
in the group, in ascending order of the indices. A group without indices gets identity, and the
values of indices in no group are left out. The result is indexed by group ID. combine is called
with the result so far and the value of the next index, and returns the two combined. This is one
generic function for every Copy type; Java has one per primitive type.
reduce_into does the same for other types.
Time: O(index_count + group_count).
Memory: size_of::<T>() x group_count bytes for the result.
§Panics
With an invalid argument message if values.len() != index_count().
Sourcepub fn reduce_into<T: Clone>(
&self,
values: &[T],
group_values: &mut [T],
combine: impl FnMut(T, &T) -> T,
)
pub fn reduce_into<T: Clone>( &self, values: &[T], group_values: &mut [T], combine: impl FnMut(T, &T) -> T, )
Writes one value per group into group_values, combined from the values of the indices of that
group. This is the form of reduce for values that are not Copy - Java’s overload
reduce(T[], T[], BinaryOperator).
For each group, starts from a clone of the value of its first index and combines the result so far
with the value of each further index, in ascending order of the indices. That takes one clone per
group; Java folds references. combine is called with the result so far and a reference to the
value of the next index, and returns the two combined. A group without indices keeps its entry in
group_values, and the values of indices in no group are left out. The borrow rules guarantee that
the two slices are not the same; Java checks this.
Time: O(index_count + group_count).
§Panics
With an invalid argument message if values.len() != index_count() or
group_values.len() != group_count().
Source§impl NodePartition
impl NodePartition
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 AsRef<NodePartition> for Sccs
impl AsRef<NodePartition> for Sccs
Source§fn as_ref(&self) -> &NodePartition
fn as_ref(&self) -> &NodePartition
Source§impl Clone for NodePartition
impl Clone for NodePartition
Source§fn clone(&self) -> NodePartition
fn clone(&self) -> NodePartition
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 NodePartition
impl Debug for NodePartition
Source§impl IndexMapping for NodePartition
impl IndexMapping for NodePartition
Source§fn is_partial(&self) -> bool
fn is_partial(&self) -> bool
mapped_count() < index_count(). Read moreSource§fn mapped_count(&self) -> u32
fn mapped_count(&self) -> u32
Source§fn index_count(&self) -> u32
fn index_count(&self) -> u32
Source§impl IndexPartition for NodePartition
impl IndexPartition for NodePartition
Source§fn group_count(&self) -> u32
fn group_count(&self) -> u32
Source§fn indices_of(&self, group: u32) -> SortedIntView<'_>
fn indices_of(&self, group: u32) -> SortedIntView<'_>
group, in ascending order. Read moreSource§fn reduce<T: Copy>(
&self,
values: &[T],
identity: T,
combine: impl FnMut(T, T) -> T,
) -> Vec<T>
fn reduce<T: Copy>( &self, values: &[T], identity: T, combine: impl FnMut(T, T) -> T, ) -> Vec<T>
Source§fn reduce_into<T: Clone>(
&self,
values: &[T],
group_values: &mut [T],
combine: impl FnMut(T, &T) -> T,
)
fn reduce_into<T: Clone>( &self, values: &[T], group_values: &mut [T], combine: impl FnMut(T, &T) -> T, )
group_values, combined from the values of the indices of that
group. This is the form of reduce for values that are not Copy - Java’s overload
reduce(T[], T[], BinaryOperator). Read more