pub struct GraphBuilder { /* private fields */ }Expand description
A builder for a Graph, from nodes and edges added in any order.
The nodes added so far have the node IDs 0..node_count, in the order they were added;
add_node and add_nodes return the IDs of new ones. Edges
name their source and target by node ID, in any order and as often as the input has them. Here, the
calls between the methods of a program are built from its call sites, one edge per call site;
names holds the names of the methods:
let mut builder = GraphBuilder::new();
builder.add_nodes(names.len() as u32); // one node per method, numbered as in names
for site in &call_sites {
builder.add_edge(site.caller, site.callee);
}
let calls = builder.build();
// 14 call sites, 12 edges: run -> show and parse -> parse were added twicebuild sorts the edges into edge index order and merges duplicates. The builder
stays usable afterwards: more nodes and edges can be added, and build called again.
Besides single edges, the builder takes edges in batches: the out-edges or in-edges of one node
(add_out_edges, add_in_edges), a path
(add_path), pairs of node IDs (add_edge_pairs), the
edges of an algorithm result (add_edges) or a whole graph
(add_graph). add_child, add_parent
and their plural forms add new nodes together with their edges. The out-edges, in-edges and paths
take their node IDs as a slice: an array, a Vec, or a SortedIntView
such as Graph::out_nodes returns. So a result of this library goes in without being copied into
an array first. Java has an overload for int... and one for IntList. A slice is read during the
call and not kept.
A method that takes the node ID of an existing node panics with an index out of bounds message if
it is not in 0..node_count, so that an edge to a missing node fails at the call. A batch method
that panics adds nothing. The methods that add edges between given nodes each have a twin with the
suffix _and_nodes, such as add_edge_and_nodes, which adds missing
nodes instead. A twin raises the node count to one more than the highest node ID it is given, and
the nodes in between are added too, without edges. So a graph can be built from input numbered
elsewhere without counting its nodes first. However, a node ID that is too high adds nodes instead
of failing.
let mut builder = GraphBuilder::new();
builder.add_edge_and_nodes(7, 3); // 8 nodes, one edge
assert_eq!(builder.build().node_count(), 8);Nodes and edges cannot be removed. To leave nodes out, build the graph and take a subgraph with
graphs::remapped and
NodeMapping::for_subgraph_mask.
A builder is mutable; the borrow rules keep two threads from changing it at once - Java’s instances are not safe to share between threads.
Memory: 8 bytes per added edge, duplicates included, plus room to grow.
A builder holds at most 2^31 - 10 nodes, as many as a graph can have. This number is the node
limit. A method that would add more panics before it changes anything. The panic has an
invalid argument message for a count, an illegal state message for a single node (Java’s
IllegalStateException), and an index out of bounds message for a node ID given to an
_and_nodes method. A builder builds a graph from at most 2^31 - 9 added edges, duplicates
included. This number is the edge limit. A batch method that would add more panics with an
invalid argument message and adds nothing. The single-edge methods add_edge,
add_edge_and_nodes, add_child and
add_parent do not check the edge limit: beyond it, build
panics with an invalid argument message. In Java, the single-edge methods also fail with
OutOfMemoryError once the edge storage cannot grow any further; a Vec keeps growing.
Implementations§
Source§impl GraphBuilder
impl GraphBuilder
Sourcepub fn ensure_edge_capacity(&mut self, total_edges: u32)
pub fn ensure_edge_capacity(&mut self, total_edges: u32)
Makes room for total_edges edges in total, so that adding up to that many allocates nothing
more.
The argument counts all edges the builder will hold, duplicates included, not the ones still to
come. It can be called at any time and does not change the graph that build
returns.
Time: O(total_edges) if the room does not suffice yet, otherwise O(1). Memory: 8 x total_edges bytes if the room does not suffice yet, otherwise none.
§Panics
With an invalid argument message if total_edges is above the edge limit.
Sourcepub fn add_node(&mut self) -> u32
pub fn add_node(&mut self) -> u32
Adds a node and returns its node ID, which is the next in order.
Time: O(1).
§Panics
With an illegal state message if the node count has reached the node limit.
Sourcepub fn add_nodes(&mut self, count: u32) -> u32
pub fn add_nodes(&mut self, count: u32) -> u32
Adds count nodes and returns the node ID of the first; the others follow in order.
Time: O(1).
§Panics
With an invalid argument message if the nodes would exceed the node limit.
Sourcepub fn add_child(&mut self, parent: u32) -> u32
pub fn add_child(&mut self, parent: u32) -> u32
Adds a node with an edge from parent to it, and returns its node ID.
Time: O(1) amortized.
§Panics
With an illegal state message if the node count has reached the node limit; with an
index out of bounds message if parent is not in 0..node_count.
Sourcepub fn add_children(&mut self, parent: u32, count: u32) -> u32
pub fn add_children(&mut self, parent: u32, count: u32) -> u32
Adds count nodes, each with an edge from parent to it, and returns the node ID of the first;
the others follow in order.
Time: O(count) amortized.
§Panics
With an invalid argument message if the nodes or edges would exceed their limit; with an
index out of bounds message if parent is not in 0..node_count.
Sourcepub fn add_parent(&mut self, child: u32) -> u32
pub fn add_parent(&mut self, child: u32) -> u32
Adds a node with an edge from it to child, and returns its node ID.
Time: O(1) amortized.
§Panics
With an illegal state message if the node count has reached the node limit; with an
index out of bounds message if child is not in 0..node_count.
Sourcepub fn add_parents(&mut self, child: u32, count: u32) -> u32
pub fn add_parents(&mut self, child: u32, count: u32) -> u32
Adds count nodes, each with an edge from it to child, and returns the node ID of the first;
the others follow in order.
Time: O(count) amortized.
§Panics
With an invalid argument message if the nodes or edges would exceed their limit; with an
index out of bounds message if child is not in 0..node_count.
Sourcepub fn add_edge_and_nodes(&mut self, source: u32, target: u32)
pub fn add_edge_and_nodes(&mut self, source: u32, target: u32)
Adds the edge source -> target, and the missing nodes up to the higher of the two node IDs.
Time: O(1) amortized.
§Panics
With an index out of bounds message if source or target is not below the node limit.
Sourcepub fn add_out_edges(&mut self, source: u32, targets: &[u32])
pub fn add_out_edges(&mut self, source: u32, targets: &[u32])
Adds an edge from source to each node of targets - Java’s overloads addOutEdges(int, IntList)
and addOutEdges(int, int...).
Time: O(targets.len()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if source or a node of targets is not in 0..node_count.
Sourcepub fn add_out_edges_and_nodes(&mut self, source: u32, targets: &[u32])
pub fn add_out_edges_and_nodes(&mut self, source: u32, targets: &[u32])
Adds an edge from source to each node of targets, and the missing nodes up to the highest of
these node IDs.
Time: O(targets.len()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if a node ID is not below the node limit.
Sourcepub fn add_in_edges(&mut self, target: u32, sources: &[u32])
pub fn add_in_edges(&mut self, target: u32, sources: &[u32])
Adds an edge from each node of sources to target - Java’s overloads addInEdges(int, IntList)
and addInEdges(int, int...).
Time: O(sources.len()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if target or a node of sources is not in 0..node_count.
Sourcepub fn add_in_edges_and_nodes(&mut self, target: u32, sources: &[u32])
pub fn add_in_edges_and_nodes(&mut self, target: u32, sources: &[u32])
Adds an edge from each node of sources to target, and the missing nodes up to the highest of
these node IDs.
Time: O(sources.len()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if a node ID is not below the node limit.
Sourcepub fn add_path(&mut self, nodes: &[u32])
pub fn add_path(&mut self, nodes: &[u32])
Adds a path through nodes: an edge from each node of the slice to the next - Java’s overloads
addPath(IntList) and addPath(int...).
Fewer than two nodes add no edge.
Time: O(nodes.len()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if a node of nodes is not in 0..node_count.
Sourcepub fn add_path_and_nodes(&mut self, nodes: &[u32])
pub fn add_path_and_nodes(&mut self, nodes: &[u32])
Adds a path through nodes, and the missing nodes up to the highest of these node IDs. The path
has an edge from each node of the slice to the next.
Fewer than two nodes add no edge, but still add the nodes.
Time: O(nodes.len()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if a node ID is not below the node limit.
Sourcepub fn add_edge_pairs(&mut self, endpoints: &[u32])
pub fn add_edge_pairs(&mut self, endpoints: &[u32])
Adds an edge for each pair of node IDs in endpoints: endpoints[0] -> endpoints[1],
endpoints[2] -> endpoints[3], and so on.
Time: O(endpoints.len()) amortized.
§Panics
With an invalid argument message if endpoints has an odd length, or the edges would exceed the
edge limit; with an index out of bounds message if a node of endpoints is not in
0..node_count.
Sourcepub fn add_edge_pairs_and_nodes(&mut self, endpoints: &[u32])
pub fn add_edge_pairs_and_nodes(&mut self, endpoints: &[u32])
Adds an edge for each pair of node IDs in endpoints, as add_edge_pairs
does, and the missing nodes up to the highest of these node IDs.
Time: O(endpoints.len()) amortized.
§Panics
With an invalid argument message if endpoints has an odd length, or the edges would exceed the
edge limit; with an index out of bounds message if a node ID is not below the node limit.
Sourcepub fn add_edges(&mut self, edges: &Edges)
pub fn add_edges(&mut self, edges: &Edges)
Adds the edges of an algorithm result, such as back_edges::of or
Biconnectivity::bridges.
Only the sources and targets are read: the edge indices of edges belong to the graph the
algorithm ran on.
Time: O(edges.count()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if a source or target is not in 0..node_count.
Sourcepub fn add_edges_and_nodes(&mut self, edges: &Edges)
pub fn add_edges_and_nodes(&mut self, edges: &Edges)
Adds the edges of an algorithm result, as add_edges does, and the missing
nodes up to the highest of their node IDs.
Time: O(edges.count()) amortized.
§Panics
With an invalid argument message if the edges would exceed the edge limit; with an
index out of bounds message if a source or target is not below the node limit.
Sourcepub fn add_graph(&mut self, graph: &Graph) -> u32
pub fn add_graph(&mut self, graph: &Graph) -> u32
Adds the nodes and edges of graph after the nodes already added, and returns the offset of its
node IDs.
Node v of graph becomes node offset + v of this builder, where the offset is the node count
before the call. Into an empty builder, the node IDs stay the same, which makes this the way to
extend a graph: a Graph is immutable, but a builder that starts with it can add to it and build
a new one. Here, the dependencies between the packages of a program are extended to a next
version, in which ui also uses storage:
let mut builder = GraphBuilder::new();
builder.add_graph(&dependencies); // into an empty builder: the node IDs stay the same
builder.add_edge(ui, storage);
let next_version = builder.build();Time: O(graph.node_count() + graph.edge_count()) amortized.
§Panics
With an invalid argument message if the nodes or edges of graph would exceed their limit.
Sourcepub fn build(&self) -> Graph
pub fn build(&self) -> Graph
Returns a graph with the nodes and edges added so far.
The edges are sorted into edge index order (by source and then by target), and duplicates are
merged, so an edge does not keep the position at which it was added; Graph::edge_index_of finds
where it went. The builder does not change: more nodes and edges can be added and built into
another graph.
Time: O(node_count + k log d), where k is the number of added edges, duplicates included, and d the most of them with the same source. Memory: 4 x (node_count + edge_count) bytes for the result; while it runs, briefly up to 4 x (node_count + k) bytes more.
§Panics
With an invalid argument message if the edges added exceed the edge limit.
Trait Implementations§
Source§impl Clone for GraphBuilder
impl Clone for GraphBuilder
Source§fn clone(&self) -> GraphBuilder
fn clone(&self) -> GraphBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more