Skip to content

Commit 4ceb9be

Browse files
committed
Added edges iterator
1 parent aa11b7c commit 4ceb9be

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/graph.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,39 @@ impl<T> Graph<T> {
830830
VertexIter(Box::new(neighbors))
831831
}
832832

833+
/// Returns an iterator over all edges that are situated
834+
/// in the graph.
835+
///
836+
/// ## Example
837+
/// ```rust
838+
/// use graphlib::Graph;
839+
///
840+
/// let mut graph: Graph<usize> = Graph::new();
841+
/// let mut edges = vec![];
842+
///
843+
/// let v1 = graph.add_vertex(0);
844+
/// let v2 = graph.add_vertex(1);
845+
/// let v3 = graph.add_vertex(2);
846+
/// let v4 = graph.add_vertex(3);
847+
///
848+
/// graph.add_edge(&v1, &v2).unwrap();
849+
/// graph.add_edge(&v3, &v1).unwrap();
850+
/// graph.add_edge(&v1, &v4).unwrap();
851+
///
852+
/// // Iterate over edges
853+
/// for v in graph.edges() {
854+
/// edges.push(v);
855+
/// }
856+
///
857+
/// assert_eq!(edges.len(), 3);
858+
/// ```
859+
pub fn edges(&self) -> impl Iterator<Item = (&VertexId, &VertexId)> {
860+
self
861+
.edges
862+
.iter()
863+
.map(|(e, _)| (e.inbound(), e.outbound()))
864+
}
865+
833866
/// Returns an iterator over the root vertices
834867
/// of the graph.
835868
///

0 commit comments

Comments
 (0)