Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement StableGraph::edges_connecting #521

Merged
merged 1 commit into from Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/graph_impl/stable_graph/mod.rs
Expand Up @@ -574,6 +574,24 @@ where
}
}

/// Return an iterator over all the edges connecting `a` and `b`.
///
/// - `Directed`: Outgoing edges from `a`.
/// - `Undirected`: All edges connected to `a`.
///
/// Iterator element type is `EdgeReference<E, Ix>`.
pub fn edges_connecting(
&self,
a: NodeIndex<Ix>,
b: NodeIndex<Ix>,
) -> EdgesConnecting<E, Ty, Ix> {
EdgesConnecting {
target_node: b,
edges: self.edges_directed(a, Direction::Outgoing),
ty: PhantomData,
}
}

/// Lookup if there is an edge from `a` to `b`.
///
/// Computes in **O(e')** time, where **e'** is the number of edges
Expand Down Expand Up @@ -1439,6 +1457,37 @@ where
}
}

/// Iterator over the multiple directed edges connecting a source node to a target node
#[derive(Debug, Clone)]
pub struct EdgesConnecting<'a, E: 'a, Ty, Ix: 'a = DefaultIx>
where
Ty: EdgeType,
Ix: IndexType,
{
target_node: NodeIndex<Ix>,
edges: Edges<'a, E, Ty, Ix>,
ty: PhantomData<Ty>,
}

impl<'a, E, Ty, Ix> Iterator for EdgesConnecting<'a, E, Ty, Ix>
where
Ty: EdgeType,
Ix: IndexType,
{
type Item = EdgeReference<'a, E, Ix>;

fn next(&mut self) -> Option<EdgeReference<'a, E, Ix>> {
let target_node = self.target_node;
self.edges
.by_ref()
.find(|&edge| edge.node[1] == target_node)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.edges.size_hint();
(0, upper)
}
}

fn swap_pair<T>(mut x: [T; 2]) -> [T; 2] {
x.swap(0, 1);
x
Expand Down
66 changes: 66 additions & 0 deletions tests/stable_graph.rs
Expand Up @@ -5,6 +5,8 @@ extern crate petgraph;
#[macro_use]
extern crate defmac;

use std::collections::HashSet;

use itertools::assert_equal;
use petgraph::algo::{kosaraju_scc, min_spanning_tree, tarjan_scc};
use petgraph::dot::Dot;
Expand Down Expand Up @@ -312,6 +314,70 @@ fn iterators_undir() {
itertools::assert_equal(g.neighbors(c), vec![]);
}

#[test]
fn iter_multi_edges() {
let mut gr = StableGraph::new();
let a = gr.add_node("a");
let b = gr.add_node("b");
let c = gr.add_node("c");

let mut connecting_edges = HashSet::new();

gr.add_edge(a, a, ());
connecting_edges.insert(gr.add_edge(a, b, ()));
gr.add_edge(a, c, ());
gr.add_edge(c, b, ());
connecting_edges.insert(gr.add_edge(a, b, ()));
gr.add_edge(b, a, ());

let mut iter = gr.edges_connecting(a, b);

let edge_id = iter.next().unwrap().id();
assert!(connecting_edges.contains(&edge_id));
connecting_edges.remove(&edge_id);

let edge_id = iter.next().unwrap().id();
assert!(connecting_edges.contains(&edge_id));
connecting_edges.remove(&edge_id);

assert_eq!(None, iter.next());
assert!(connecting_edges.is_empty());
}

#[test]
fn iter_multi_undirected_edges() {
let mut gr: StableUnGraph<_, _> = Default::default();
let a = gr.add_node("a");
let b = gr.add_node("b");
let c = gr.add_node("c");

let mut connecting_edges = HashSet::new();

gr.add_edge(a, a, ());
connecting_edges.insert(gr.add_edge(a, b, ()));
gr.add_edge(a, c, ());
gr.add_edge(c, b, ());
connecting_edges.insert(gr.add_edge(a, b, ()));
connecting_edges.insert(gr.add_edge(b, a, ()));

let mut iter = gr.edges_connecting(a, b);

let edge_id = iter.next().unwrap().id();
assert!(connecting_edges.contains(&edge_id));
connecting_edges.remove(&edge_id);

let edge_id = iter.next().unwrap().id();
assert!(connecting_edges.contains(&edge_id));
connecting_edges.remove(&edge_id);

let edge_id = iter.next().unwrap().id();
assert!(connecting_edges.contains(&edge_id));
connecting_edges.remove(&edge_id);

assert_eq!(None, iter.next());
assert!(connecting_edges.is_empty());
}

#[test]
fn dot() {
let mut gr = StableGraph::new();
Expand Down