rustworkx_core/graph_ext/
multigraph.rs1use hashbrown::HashSet;
16use petgraph::visit::{EdgeCount, EdgeRef, GraphBase, GraphProp, IntoEdgeReferences, Visitable};
17use petgraph::{Directed, Undirected};
18use std::hash::Hash;
19
20pub trait HasParallelEdgesUndirected: GraphBase {
21 fn has_parallel_edges(&self) -> bool;
22}
23
24impl<G> HasParallelEdgesUndirected for G
25where
26 G: GraphProp<EdgeType = Undirected> + Visitable + EdgeCount,
27 G::NodeId: Eq + Hash,
28 for<'b> &'b G: GraphBase<NodeId = G::NodeId> + IntoEdgeReferences,
29{
30 fn has_parallel_edges(&self) -> bool {
31 let mut edges: HashSet<[Self::NodeId; 2]> = HashSet::with_capacity(2 * self.edge_count());
32 for edge in self.edge_references() {
33 let endpoints = [edge.source(), edge.target()];
34 let endpoints_rev = [edge.target(), edge.source()];
35 if edges.contains(&endpoints) || edges.contains(&endpoints_rev) {
36 return true;
37 }
38 edges.insert(endpoints);
39 edges.insert(endpoints_rev);
40 }
41 false
42 }
43}
44
45pub trait HasParallelEdgesDirected: GraphBase {
46 fn has_parallel_edges(&self) -> bool;
47}
48
49impl<G> HasParallelEdgesDirected for G
50where
51 G: GraphProp<EdgeType = Directed> + Visitable + EdgeCount,
52 G::NodeId: Eq + Hash,
53 for<'b> &'b G: GraphBase<NodeId = G::NodeId> + IntoEdgeReferences,
54{
55 fn has_parallel_edges(&self) -> bool {
56 let mut edges: HashSet<[Self::NodeId; 2]> = HashSet::with_capacity(self.edge_count());
57 for edge in self.edge_references() {
58 let endpoints = [edge.source(), edge.target()];
59 if edges.contains(&endpoints) {
60 return true;
61 }
62 edges.insert(endpoints);
63 }
64 false
65 }
66}