Skip to content

Commit

Permalink
#20: fix collection algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
UARTman committed Jan 6, 2023
1 parent 663dfa4 commit 85cd211
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Sodg {
.get(&v)
.context(format!("Failed to get v{v}"))?
.clone();
if vtx.parents.is_empty() {
if vtx.parents.is_empty() && vtx.taken {
for edge in &vtx.edges {
queue.push_back(edge.to);
self.vertices
Expand Down Expand Up @@ -70,7 +70,10 @@ fn collects_simple_graph() -> Result<()> {
g.bind(1, 2, "x")?;
g.bind(1, 3, "y")?;
g.bind(2, 4, "z")?;
g.collect(1)?;
g.data(4)?;
g.data(2)?;
g.data(1)?;
g.data(3)?;
assert!(g.is_empty());
Ok(())
}
Expand All @@ -86,7 +89,10 @@ fn collects_complicated_graph() -> Result<()> {
g.bind(2, 4, "z")?;
g.bind(3, 5, "a")?;
g.bind(4, 3, "b")?;
g.collect(1)?;
for i in 1..=5 {
g.data(i)?;
}
// g.collect(1)?;
assert!(g.is_empty());
Ok(())
}
3 changes: 2 additions & 1 deletion src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ impl Sodg {
pub fn data(&mut self, v: u32) -> Result<Hex> {
let vtx = self
.vertices
.get(&v)
.get_mut(&v)
.context(format!("Can't find ν{}", v))?;
let data = vtx.data.clone();
vtx.taken = true;
self.collect(v)?;
Ok(data)
}
Expand Down
2 changes: 2 additions & 0 deletions src/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) struct Vertex {
pub edges: Vec<Edge>,
pub data: Hex,
pub parents: HashSet<u32>,
pub taken: bool,
}

impl Vertex {
Expand All @@ -42,6 +43,7 @@ impl Vertex {
edges: vec![],
data: Hex::empty(),
parents: HashSet::new(),
taken: false,
}
}
}
Expand Down

0 comments on commit 85cd211

Please sign in to comment.