Skip to content

Commit e3375cb

Browse files
authored
Merge pull request #45 from purpleprotocol/feature/30/values-iterator
Values iterator
2 parents faf64e9 + 72f083b commit e3375cb

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/graph.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

src/iterators/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ mod dijkstra;
66
mod topo;
77
mod vertices;
88
mod owning_iterator;
9+
mod values;
910

1011
pub use bfs::*;
1112
pub use dfs::*;
1213
pub use dijkstra::*;
1314
pub use topo::*;
1415
pub use vertices::*;
16+
pub use values::*;

src/iterators/values.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)