Skip to content

Commit

Permalink
Fix isolated nodes (#5)
Browse files Browse the repository at this point in the history
* Don't emit graphs when we already have them

* Add eqsolve example and fix loner nodes
  • Loading branch information
saulshanabrook authored Aug 10, 2023
1 parent e2f8bb0 commit e406ffc
Show file tree
Hide file tree
Showing 6 changed files with 6,368 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ impl EGraph {
// 3. Create mapping from all parents which are updated to the children which are inlined
let mut parents_to_children = std::collections::HashMap::new();
for (_, node_id) in &leaves {
for parent in node_to_parents.get(node_id).unwrap() {
parents_to_children
.entry(parent.clone())
.or_insert_with(Vec::new)
.push(node_id.clone());
let parents = node_to_parents.get(node_id);
// There will be no parents for isolated nodes with no parents or children
if let Some(parents) = parents {
for parent in parents {
parents_to_children
.entry(parent.clone())
.or_insert_with(Vec::new)
.push(node_id.clone());
}
}
}
// 4. Inline leaf nodes into their parents
Expand Down Expand Up @@ -67,6 +71,10 @@ impl EGraph {
}
// 5. Remove leaf nodes from egraph, class data, and root eclasses
for (eclass, node_id) in &leaves {
// If this node has no parents, don't remove it, since it wasn't inlined
if node_to_parents.get(node_id).is_none() {
continue;
}
self.nodes.remove(node_id);
self.class_data.remove(eclass);
self.root_eclasses.retain(|root| root != eclass);
Expand Down
1 change: 1 addition & 0 deletions tests-viz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This is a list of all the tests in the `tests` directory. Each test is a JSON fi
| Test | Image | Inlined Leaves Image |
| ---- | ----- | -------------------- |
| [`diff_power_simple`](../tests/diff_power_simple.json) | ![svg file](./diff_power_simple.svg) | ![inlined leaves svg file](./diff_power_simple-inlined.svg) |
| [`eqsolve`](../tests/eqsolve.json) | ![svg file](./eqsolve.svg) | ![inlined leaves svg file](./eqsolve-inlined.svg) |
| [`fibonacci-demand`](../tests/fibonacci-demand.json) | ![svg file](./fibonacci-demand.svg) | ![inlined leaves svg file](./fibonacci-demand-inlined.svg) |
| [`fibonacci`](../tests/fibonacci.json) | ![svg file](./fibonacci.svg) | ![inlined leaves svg file](./fibonacci-inlined.svg) |
| [`lambda_if`](../tests/lambda_if.json) | ![svg file](./lambda_if.svg) | ![inlined leaves svg file](./lambda_if-inlined.svg) |
Expand Down
Loading

0 comments on commit e406ffc

Please sign in to comment.