Skip to main content

Module csr

Module csr 

Source
Expand description

Access to the arrays in which a graph keeps its edges, for algorithm code that loops over all edges - Java’s class Csr. These arrays are called the CSR (compressed sparse row) layout of the graph.

Each function returns the graph’s own array, not a copy. The out-nodes of node v are out_adjacency[out_adjacency_start[v]..out_adjacency_start[v + 1]], in ascending order, and the position of an out-node in out_adjacency is the edge index of its edge. out_adjacency_start has node_count + 1 values, from 0 to edge_count. A loop loads the two slices into local variables once and reads them without a method call or an argument check per edge. Here, calls holds the calls between the methods of a program, and the loop counts the callers of each method:

let start = csr::out_adjacency_start(&calls);
let callees = csr::out_adjacency(&calls);
let mut caller_count = vec![0; calls.node_count() as usize];
for method in 0..calls.node_count() as usize {
    for edge in start[method] as usize..start[method + 1] as usize {
        caller_count[callees[edge] as usize] += 1;
    }
}
// caller_count: 3 for format, 2 for parse, 0 for main, 1 for every other method

A BiAdjacentGraph also holds the in-nodes of every node, called its in-adjacency. The in-adjacency has the same layout, in in_adjacency_start and in_adjacency. A position in in_adjacency is not an edge index of the graph, though, but of its transposed graph; graphs::transposed_edge_mapping translates all of them in one pass.

The slices cannot be modified. Java’s arrays must not be modified by contract, and here the borrow rules prevent a write. So the graph stays immutable and safe to share between threads, and the out-nodes of each node stay sorted. The algorithms of this library read the arrays the same way. Code that reads the out-nodes of a few nodes is better served by Graph::out_nodes and the edge index methods of Graph, which check their arguments.

Functions§

in_adjacency
Returns the in-nodes of all nodes, node after node: the graph’s own array, of length edge_count.
in_adjacency_start
Returns where the in-nodes of each node start in in_adjacency, followed by edge_count: the graph’s own array, of length node_count + 1.
out_adjacency
Returns the out-nodes of all nodes, node after node, at the positions of their edge indices: the graph’s own array, of length edge_count.
out_adjacency_start
Returns where the out-nodes of each node start in out_adjacency, followed by edge_count: the graph’s own array, of length node_count + 1.