Skip to content

Commit

Permalink
Fix IntoEdgesDirected implementation for NodeFiltered
Browse files Browse the repository at this point in the history
Calling it with Direction::Incoming would not filter the edges at all.
Existing tests didn't catch it because they did not have an edge going
from an excluded node to an included one.
  • Loading branch information
jkeljo committed Dec 14, 2021
1 parent 9ff6888 commit da8db3e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/visit/filter.rs
Expand Up @@ -263,6 +263,7 @@ where
include_source: self.1.include_node(a),
iter: self.0.edges(a),
f: &self.1,
dir: Direction::Outgoing,
}
}
}
Expand All @@ -279,6 +280,7 @@ where
include_source: self.1.include_node(a),
iter: self.0.edges_directed(a, dir),
f: &self.1,
dir,
}
}
}
Expand All @@ -290,6 +292,7 @@ pub struct NodeFilteredEdges<'a, G, I, F: 'a> {
include_source: bool,
iter: I,
f: &'a F,
dir: Direction,
}

impl<'a, G, I, F> Iterator for NodeFilteredEdges<'a, G, I, F>
Expand All @@ -303,8 +306,14 @@ where
if !self.include_source {
None
} else {
let dir = self.dir;
let f = self.f;
self.iter.find(move |&edge| f.include_node(edge.target()))
self.iter.find(move |&edge| {
f.include_node(match dir {
Direction::Outgoing => edge.target(),
Direction::Incoming => edge.source(),
})
})
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down
2 changes: 1 addition & 1 deletion tests/graph.rs
Expand Up @@ -1416,7 +1416,7 @@ fn test_node_filtered_iterators_directed() {
};

let gr = make_edge_iterator_graph::<Directed>();
let filter = |node: NodeIndex<u32>| node.index() < 4;
let filter = |node: NodeIndex<u32>| node.index() < 5; // < 5 makes sure there are edges going both ways between included and excluded nodes (e -> g, f -> e)
let filtered = NodeFiltered::from_fn(&gr, filter);

for i in gr.node_indices() {
Expand Down

0 comments on commit da8db3e

Please sign in to comment.