Skip to main content

graphity_core/
csr.rs

1//! Access to the arrays in which a graph keeps its edges, for algorithm code that loops over all
2//! edges - Java's class `Csr`. These arrays are called the CSR (compressed sparse row) layout of the
3//! graph.
4//!
5//! Each function returns the graph's own array, not a copy. The out-nodes of node `v` are
6//! `out_adjacency[out_adjacency_start[v]..out_adjacency_start[v + 1]]`, in ascending order, and the
7//! position of an out-node in `out_adjacency` is the edge index of its edge. `out_adjacency_start` has
8//! `node_count + 1` values, from 0 to `edge_count`. A loop loads the two slices into local variables
9//! once and reads them without a method call or an argument check per edge. Here, `calls` holds the
10//! calls between the methods of a program, and the loop counts the callers of each method:
11//!
12//! <!-- example: building_and_reading.rs#loop -->
13//! ```
14//! # use graphity_core::csr;
15//! # let calls = graphity_core::Graph::of(
16//! #     &[0, 2, 2, 5, 7, 9, 10, 11, 12, 12, 12],
17//! #     &[1, 2, 3, 4, 5, 6, 8, 7, 9, 7, 6, 7],
18//! # ).unwrap();
19//! let start = csr::out_adjacency_start(&calls);
20//! let callees = csr::out_adjacency(&calls);
21//! let mut caller_count = vec![0; calls.node_count() as usize];
22//! for method in 0..calls.node_count() as usize {
23//!     for edge in start[method] as usize..start[method + 1] as usize {
24//!         caller_count[callees[edge] as usize] += 1;
25//!     }
26//! }
27//! // caller_count: 3 for format, 2 for parse, 0 for main, 1 for every other method
28//! # assert_eq!(caller_count, [0, 1, 1, 1, 1, 1, 2, 3, 1, 1]);
29//! ```
30//!
31//! A [`BiAdjacentGraph`] also holds the in-nodes of every node, called its in-adjacency. The
32//! in-adjacency has the same layout, in [`in_adjacency_start`] and [`in_adjacency`]. A position in
33//! `in_adjacency` is not an edge index of the graph, though, but of its transposed graph;
34//! [`graphs::transposed_edge_mapping`](crate::graphs::transposed_edge_mapping) translates all of them
35//! in one pass.
36//!
37//! The slices cannot be modified. Java's arrays must not be modified by contract, and here the borrow
38//! rules prevent a write. So the graph stays immutable and safe to share between threads, and the
39//! out-nodes of each node stay sorted. The algorithms of this library read the arrays the same way.
40//! Code that reads the out-nodes of a few nodes is better served by [`Graph::out_nodes`] and the edge
41//! index methods of [`Graph`], which check their arguments.
42
43use crate::{BiAdjacentGraph, Graph};
44
45/// Returns where the out-nodes of each node start in [`out_adjacency`], followed by `edge_count`:
46/// the graph's own array, of length `node_count + 1`.
47///
48/// **Time:** O(1).
49#[inline]
50pub fn out_adjacency_start(graph: &Graph) -> &[u32] {
51    graph.out_adjacency_start()
52}
53
54/// Returns the out-nodes of all nodes, node after node, at the positions of their edge indices: the
55/// graph's own array, of length `edge_count`.
56///
57/// **Time:** O(1).
58#[inline]
59pub fn out_adjacency(graph: &Graph) -> &[u32] {
60    graph.out_adjacency()
61}
62
63/// Returns where the in-nodes of each node start in [`in_adjacency`], followed by `edge_count`: the
64/// graph's own array, of length `node_count + 1`.
65///
66/// **Time:** O(1).
67#[inline]
68pub fn in_adjacency_start(graph: &BiAdjacentGraph) -> &[u32] {
69    graph.in_adjacency_start()
70}
71
72/// Returns the in-nodes of all nodes, node after node: the graph's own array, of length
73/// `edge_count`.
74///
75/// A position in this array is the edge index of the edge in the transposed graph, not in `graph`.
76///
77/// **Time:** O(1).
78#[inline]
79pub fn in_adjacency(graph: &BiAdjacentGraph) -> &[u32] {
80    graph.in_adjacency()
81}