Skip to content

Commit

Permalink
Implement StableGraph::edges_connecting (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickHu committed Jan 24, 2023
1 parent fdc632e commit 5300d88
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
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

0 comments on commit 5300d88

Please sign in to comment.