dag: Add "dot" format output

This commit is contained in:
cloudhead 2023-10-09 18:01:46 +02:00
parent 8657cd8fc8
commit bb3c1b5684
No known key found for this signature in database
1 changed files with 18 additions and 0 deletions

View File

@ -5,6 +5,7 @@ use std::{
cmp::Ordering,
collections::{BTreeMap, BTreeSet, VecDeque},
fmt,
fmt::Write,
ops::{ControlFlow, Deref, Index},
};
@ -313,6 +314,23 @@ impl<K: Ord + Copy, V> Dag<K, V> {
}
}
impl<K: Ord + Copy + fmt::Display, V> Dag<K, V> {
/// Return the graph in "dot" format.
pub fn to_dot(&self) -> String {
let mut output = String::new();
writeln!(output, "digraph G {{").ok();
for (k, v) in self.graph.iter() {
for d in &v.dependencies {
writeln!(output, "\t\"{k}\" -> \"{d}\";").ok();
}
}
writeln!(output, "}}").ok();
output
}
}
impl<K: Ord + Copy + fmt::Debug, V> Index<&K> for Dag<K, V> {
type Output = Node<K, V>;