File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1255,6 +1255,33 @@ impl<T> Graph<T> {
12551255 }
12561256 }
12571257
1258+ /// Returns an iterator over the values of the vertices
1259+ /// placed in the graph.
1260+ ///
1261+ /// ## Example
1262+ /// ```rust
1263+ /// #[macro_use] extern crate graphlib;
1264+ /// use graphlib::Graph;
1265+ /// use std::collections::HashSet;
1266+ ///
1267+ /// let mut graph: Graph<usize> = Graph::new();
1268+ ///
1269+ /// let v1 = graph.add_vertex(1);
1270+ /// let v2 = graph.add_vertex(2);
1271+ /// let v3 = graph.add_vertex(3);
1272+ ///
1273+ /// let mut values = graph.values();
1274+ ///
1275+ /// assert!(set![&1, &2, &3] == values.collect());
1276+ /// ```
1277+ pub fn values ( & self ) -> ValuesIter < ' _ , T > {
1278+ let iter = self . vertices
1279+ . values ( )
1280+ . map ( |( v, _) | v) ;
1281+
1282+ ValuesIter ( Box :: new ( iter) )
1283+ }
1284+
12581285 #[ cfg( feature = "dot" ) ]
12591286 /// Creates a file with the dot representation of the graph.
12601287 /// This method requires the `dot` feature.
Original file line number Diff line number Diff line change @@ -6,9 +6,11 @@ mod dijkstra;
66mod topo;
77mod vertices;
88mod owning_iterator;
9+ mod values;
910
1011pub use bfs:: * ;
1112pub use dfs:: * ;
1213pub use dijkstra:: * ;
1314pub use topo:: * ;
1415pub use vertices:: * ;
16+ pub use values:: * ;
Original file line number Diff line number Diff line change 1+ // Copyright 2019 Octavian Oncescu
2+
3+ #[ cfg( feature = "no_std" ) ]
4+ extern crate alloc;
5+ #[ cfg( feature = "no_std" ) ]
6+ use alloc:: boxed:: Box ;
7+
8+ /// Generic values Iterator.
9+ pub struct ValuesIter < ' a , T > ( pub ( crate ) Box < dyn ' a + Iterator < Item = & ' a T > > ) ;
10+
11+ impl < ' a , T > Iterator for ValuesIter < ' a , T > {
12+ type Item = & ' a T ;
13+
14+ #[ inline]
15+ fn next ( & mut self ) -> Option < Self :: Item > {
16+ self . 0 . next ( )
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments