diff options
| author | Zalathar <Zalathar@users.noreply.github.com> | 2025-01-26 14:00:46 +1100 |
|---|---|---|
| committer | Zalathar <Zalathar@users.noreply.github.com> | 2025-01-26 14:08:42 +1100 |
| commit | d36e2b88d643b3d712c6e2d22b4b0d77fea07209 (patch) | |
| tree | db44d88d640f6bbed5d1767125f2baa489790d38 /compiler/rustc_data_structures/src/graph/mod.rs | |
| parent | 6fb03584cf6d915cc5527f45037ca009f4273c4c (diff) | |
| download | rust-d36e2b88d643b3d712c6e2d22b4b0d77fea07209.tar.gz rust-d36e2b88d643b3d712c6e2d22b4b0d77fea07209.zip | |
Incorporate `iter_nodes` into `graph::DirectedGraph`
This assumes that the set of valid node IDs is exactly `0..num_nodes`. In practice, we have a lot of graph-algorithm code that already assumes that nodes are densely numbered, by using `num_nodes` to allocate per-node indexed data structures.
Diffstat (limited to 'compiler/rustc_data_structures/src/graph/mod.rs')
| -rw-r--r-- | compiler/rustc_data_structures/src/graph/mod.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/graph/mod.rs b/compiler/rustc_data_structures/src/graph/mod.rs index 92035e8bc48..4a1e5db6768 100644 --- a/compiler/rustc_data_structures/src/graph/mod.rs +++ b/compiler/rustc_data_structures/src/graph/mod.rs @@ -14,7 +14,23 @@ mod tests; pub trait DirectedGraph { type Node: Idx; + /// Returns the total number of nodes in this graph. + /// + /// Several graph algorithm implementations assume that every node ID is + /// strictly less than the number of nodes, i.e. nodes are densely numbered. + /// That assumption allows them to use `num_nodes` to allocate per-node + /// data structures, indexed by node. fn num_nodes(&self) -> usize; + + /// Iterates over all nodes of a graph in ascending numeric order. + /// + /// Assumes that nodes are densely numbered, i.e. every index in + /// `0..num_nodes` is a valid node. + fn iter_nodes( + &self, + ) -> impl Iterator<Item = Self::Node> + DoubleEndedIterator + ExactSizeIterator { + (0..self.num_nodes()).map(<Self::Node as Idx>::new) + } } pub trait NumEdges: DirectedGraph { |
