Skip to content

Commit

Permalink
Make graphviz colors consistent and fix color bug (#9)
Browse files Browse the repository at this point in the history
* Make colors consistent regardless of expr ordering

Currently, if you have two graphs which use the same types, the colors
per type might be different, depending on which type comes first.

This makes comparing similar graphs harder visually.

This resolves that by using a sorted map for mapping types to values,
so that if the same set of types are used by two graphs, they will
have the same colors.

* Fix which collection needs to be sorted

* Fix color wraparound when number of types exceed the max colors
  • Loading branch information
saulshanabrook authored Nov 9, 2023
1 parent 6f16222 commit 3ca6da9
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl EGraph {
}

fn to_graphviz(&self) -> Graph {
// 1. Group nodes by typ and class
let mut class_nodes = std::collections::HashMap::new();
// 1. Group nodes by type and class (use BTreeMap to keep sorted so colors are consistent)
let mut class_nodes = std::collections::BTreeMap::new();
// and create mapping from each node ID to its class
let mut node_to_class = std::collections::HashMap::new();
for (node_id, node) in &self.nodes {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl EGraph {
let mut typ_colors = std::collections::HashMap::new();

for (typ, class_to_node) in class_nodes {
let next_color = typ_colors.len() % N_COLORS + INITIAL_COLOR;
let next_color = (typ_colors.len() + INITIAL_COLOR) % N_COLORS;
let color = typ_colors.entry(typ).or_insert(next_color);
stmts.push(stmt!(attr!("fillcolor", color)));
for (class_id, nodes) in class_to_node {
Expand Down

0 comments on commit 3ca6da9

Please sign in to comment.