Skip to content

Commit a12058b

Browse files
committed
Ran formatter and bumped version to v0.2.2
1 parent d90ea25 commit a12058b

File tree

4 files changed

+85
-69
lines changed

4 files changed

+85
-69
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "graphlib"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["Octavian Oncescu <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/purpleprotocol/graphlib"

src/graph.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ impl<T> Graph<T> {
522522
/// graph.add_edge(&v3, &v4).unwrap();
523523
///
524524
/// assert!(!graph.is_cyclic());
525-
///
525+
///
526526
/// graph.add_edge(&v3, &v1);
527-
///
527+
///
528528
/// assert!(graph.is_cyclic());
529529
/// ```
530530
pub fn is_cyclic(&self) -> bool {
@@ -667,8 +667,8 @@ impl<T> Graph<T> {
667667
/// ```
668668
pub fn in_neighbors(&self, id: &VertexId) -> VertexIter<'_> {
669669
match self.inbound_table.get(id) {
670-
Some(neighbors) => VertexIter(Box::new(neighbors.iter().map(AsRef::as_ref,),),),
671-
None => VertexIter(Box::new(std::iter::empty(),),),
670+
Some(neighbors) => VertexIter(Box::new(neighbors.iter().map(AsRef::as_ref))),
671+
None => VertexIter(Box::new(std::iter::empty())),
672672
}
673673
}
674674

@@ -701,9 +701,9 @@ impl<T> Graph<T> {
701701
/// assert_eq!(neighbors[1], &v4);
702702
/// ```
703703
pub fn out_neighbors(&self, id: &VertexId) -> VertexIter<'_> {
704-
match self.outbound_table.get(id,) {
705-
Some(iter) => VertexIter(Box::new(iter.iter().map(AsRef::as_ref,),),),
706-
None => VertexIter(Box::new(std::iter::empty(),),),
704+
match self.outbound_table.get(id) {
705+
Some(iter) => VertexIter(Box::new(iter.iter().map(AsRef::as_ref))),
706+
None => VertexIter(Box::new(std::iter::empty())),
707707
}
708708
}
709709

@@ -738,12 +738,13 @@ impl<T> Graph<T> {
738738
/// ```
739739
pub fn neighbors(&self, id: &VertexId) -> VertexIter<'_> {
740740
let mut visited = HashSet::new();
741-
let neighbors = self.out_neighbors(id,)
742-
.chain(self.in_neighbors(id,),)
741+
let neighbors = self
742+
.out_neighbors(id)
743+
.chain(self.in_neighbors(id))
743744
//Remove duplicates.
744-
.filter(move |&&v,| visited.insert(v,),);
745+
.filter(move |&&v| visited.insert(v));
745746

746-
VertexIter(Box::new(neighbors,),)
747+
VertexIter(Box::new(neighbors))
747748
}
748749

749750
/// Returns an iterator over the root vertices
@@ -774,7 +775,7 @@ impl<T> Graph<T> {
774775
/// assert_eq!(roots[0], &v3);
775776
/// ```
776777
pub fn roots(&self) -> VertexIter<'_> {
777-
VertexIter(Box::new(self.roots.iter().map(AsRef::as_ref,),),)
778+
VertexIter(Box::new(self.roots.iter().map(AsRef::as_ref)))
778779
}
779780

780781
/// Returns an iterator over all of the
@@ -800,7 +801,7 @@ impl<T> Graph<T> {
800801
/// assert_eq!(vertices.len(), 4);
801802
/// ```
802803
pub fn vertices(&self) -> VertexIter<'_> {
803-
VertexIter(Box::new(self.vertices.keys().map(AsRef::as_ref,),),)
804+
VertexIter(Box::new(self.vertices.keys().map(AsRef::as_ref)))
804805
}
805806

806807
/// Returns an iterator over the vertices
@@ -811,20 +812,20 @@ impl<T> Graph<T> {
811812
/// # #[macro_use] extern crate graphlib; fn main() {
812813
/// use graphlib::Graph;
813814
/// use std::collections::HashSet;
814-
///
815+
///
815816
/// let mut graph: Graph<usize> = Graph::new();
816-
///
817+
///
817818
/// let v1 = graph.add_vertex(0);
818819
/// let v2 = graph.add_vertex(1);
819820
/// let v3 = graph.add_vertex(2);
820821
/// let v4 = graph.add_vertex(3);
821-
///
822+
///
822823
/// graph.add_edge(&v1, &v2).unwrap();
823824
/// graph.add_edge(&v3, &v1).unwrap();
824825
/// graph.add_edge(&v1, &v4).unwrap();
825-
///
826+
///
826827
/// let mut dfs = graph.dfs();
827-
///
828+
///
828829
/// assert_eq!(dfs.next(), Some(&v3));
829830
/// assert_eq!(dfs.next(), Some(&v1));
830831
/// assert!(set![&v2, &v4] == dfs.collect());
@@ -894,7 +895,7 @@ mod tests {
894895
#[test]
895896
fn dfs() {
896897
let mut graph: Graph<usize> = Graph::new();
897-
898+
898899
let v1 = graph.add_vertex(0);
899900
let v2 = graph.add_vertex(1);
900901
let v3 = graph.add_vertex(2);
@@ -914,7 +915,7 @@ mod tests {
914915
#[test]
915916
fn dfs_mul_roots() {
916917
let mut graph: Graph<usize> = Graph::new();
917-
918+
918919
let v1 = graph.add_vertex(0);
919920
let v2 = graph.add_vertex(1);
920921
let v3 = graph.add_vertex(2);
@@ -931,16 +932,18 @@ mod tests {
931932

932933
// Iterate over vertices
933934
let mut dfs = graph.dfs();
934-
935+
935936
for _ in 0..2 {
936937
let v = dfs.next();
937-
938+
938939
if v == Some(&v3) {
939940
assert_eq!(dfs.next(), Some(&v1));
940941
assert!(set![&v2, &v4] == (&mut dfs).take(2).collect());
941942
} else if v == Some(&v5) {
942943
assert_eq!(dfs.next(), Some(&v6));
943-
} else { panic!("Not a root node") }
944+
} else {
945+
panic!("Not a root node")
946+
}
944947
}
945948

946949
assert_eq!(dfs.count(), 0, "There were remaining nodes");

src/iterators/dfs.rs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2019 Octavian Oncescu
22

33
use crate::graph::Graph;
4-
use crate::vertex_id::VertexId;
54
use crate::iterators::VertexIter;
5+
use crate::vertex_id::VertexId;
66

77
use hashbrown::HashSet;
8-
use std::iter::{Cloned, Chain, Peekable,};
8+
use std::iter::{Chain, Cloned, Peekable};
99

1010
#[derive(Debug)]
1111
/// Depth-First Iterator
@@ -26,9 +26,7 @@ pub struct Dfs<'a, T> {
2626

2727
impl<'a, T> Dfs<'a, T> {
2828
pub fn new(graph: &'a Graph<T>) -> Dfs<'_, T> {
29-
let unchecked = graph.roots()
30-
.chain(graph.vertices())
31-
.cloned().peekable();
29+
let unchecked = graph.roots().chain(graph.vertices()).cloned().peekable();
3230

3331
Dfs {
3432
unchecked,
@@ -41,13 +39,15 @@ impl<'a, T> Dfs<'a, T> {
4139
}
4240

4341
/// Returns true if the iterated graph has a cycle.
44-
///
42+
///
4543
/// # Warning
46-
///
44+
///
4745
/// It is a logic error to use this iterator after calling this function.
48-
pub fn is_cyclic(&mut self,) -> bool {
46+
pub fn is_cyclic(&mut self) -> bool {
4947
//Check for a cached answer.
50-
if self.cached_cyclic { return self.cached_cyclic }
48+
if self.cached_cyclic {
49+
return self.cached_cyclic;
50+
}
5151

5252
//Search until an answer is found.
5353
while self.process_vertex().is_some() {}
@@ -56,50 +56,56 @@ impl<'a, T> Dfs<'a, T> {
5656
}
5757

5858
/// Processes the next vertex.
59-
///
59+
///
6060
/// Will return None if:
61-
///
61+
///
6262
/// * No vertices are left.
6363
/// * The next vertex forms a cycle.
64-
fn process_vertex(&mut self,) -> Option<&'a VertexId> {
64+
fn process_vertex(&mut self) -> Option<&'a VertexId> {
6565
//We have traversed this partition of the graph, move on.
6666
if self.pending_stack.is_empty() {
6767
//Mark all the grey vertices black.
68-
self.black.extend(self.grey.drain(),);
68+
self.black.extend(self.grey.drain());
6969

7070
//Spliting the borrows for the borrow checker.
7171
let unchecked = &mut self.unchecked;
7272
let black = &self.black;
7373

7474
//Search for an unprocessed vertex.
75-
let next = unchecked.find(move |v,| !black.contains(v));
76-
75+
let next = unchecked.find(move |v| !black.contains(v));
76+
7777
//We found a new vertex.
7878
if let Some(v) = next {
7979
self.pending_stack.push(v);
8080
}
8181
}
8282

8383
//Get the next pending vertex.
84-
self.pending_stack.pop().iter()
85-
//Filter cycles.
86-
.filter_map(|v,| {
87-
//If this vertex forms a cycle do not return it.
88-
if !self.grey.insert(*v) {
89-
self.cached_cyclic = true;
90-
91-
return None
92-
}
93-
94-
//Add all of its neighbours to be processed.
95-
for v in self.iterable.out_neighbors(v) {
96-
//This neighbour forms a cycle don't process it.
97-
if self.grey.contains(v) { self.cached_cyclic = true }
98-
else { self.pending_stack.push(*v) }
99-
}
100-
101-
self.iterable.fetch_id_ref(v)
102-
}).next()
84+
self.pending_stack
85+
.pop()
86+
.iter()
87+
//Filter cycles.
88+
.filter_map(|v| {
89+
//If this vertex forms a cycle do not return it.
90+
if !self.grey.insert(*v) {
91+
self.cached_cyclic = true;
92+
93+
return None;
94+
}
95+
96+
//Add all of its neighbours to be processed.
97+
for v in self.iterable.out_neighbors(v) {
98+
//This neighbour forms a cycle don't process it.
99+
if self.grey.contains(v) {
100+
self.cached_cyclic = true
101+
} else {
102+
self.pending_stack.push(*v)
103+
}
104+
}
105+
106+
self.iterable.fetch_id_ref(v)
107+
})
108+
.next()
103109
}
104110
}
105111

@@ -112,7 +118,9 @@ impl<'a, T> Iterator for Dfs<'a, T> {
112118
(0, Some(remaining))
113119
}
114120
fn next(&mut self) -> Option<Self::Item> {
115-
(0..self.size_hint().1.unwrap()).filter_map(move |_,| self.process_vertex()).next()
121+
(0..self.size_hint().1.unwrap())
122+
.filter_map(move |_| self.process_vertex())
123+
.next()
116124
}
117125
}
118126

@@ -136,11 +144,15 @@ mod tests {
136144

137145
assert!(graph.add_edge(&v, &v).is_ok(), "Failed to create cycle");
138146

139-
for _ in 0..100 { graph.add_vertex(0); }
147+
for _ in 0..100 {
148+
graph.add_vertex(0);
149+
}
140150

141151
let mut dfs = graph.dfs();
142152

143-
for _ in 0..99 { dfs.next(); }
153+
for _ in 0..99 {
154+
dfs.next();
155+
}
144156

145157
assert!(dfs.is_cyclic());
146158
}
@@ -153,9 +165,9 @@ mod tests {
153165
let v2 = graph.add_vertex(());
154166
let v3 = graph.add_vertex(());
155167

156-
graph.add_edge(&v1, &v2,);
157-
graph.add_edge(&v3, &v2,);
158-
168+
graph.add_edge(&v1, &v2);
169+
graph.add_edge(&v3, &v2);
170+
159171
graph.add_vertex(());
160172

161173
assert!(graph.is_cyclic() == false,);

src/iterators/vertices.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
use crate::vertex_id::VertexId;
44
use std::fmt::Debug;
55

6-
pub(crate) trait MergedTrait<'a,>: Iterator<Item = &'a VertexId> + Debug {}
6+
pub(crate) trait MergedTrait<'a>: Iterator<Item = &'a VertexId> + Debug {}
77

8-
impl<'a, T,> MergedTrait<'a,> for T
9-
where T: Iterator<Item = &'a VertexId> + Debug {}
8+
impl<'a, T> MergedTrait<'a> for T where T: Iterator<Item = &'a VertexId> + Debug {}
109

1110
/// Generic Vertex Iterator.
1211
#[derive(Debug)]
13-
pub struct VertexIter<'a>(pub(crate) Box<dyn 'a + MergedTrait<'a,>>,);
12+
pub struct VertexIter<'a>(pub(crate) Box<dyn 'a + MergedTrait<'a>>);
1413

1514
impl<'a> Iterator for VertexIter<'a> {
1615
type Item = &'a VertexId;
1716

1817
#[inline]
19-
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
18+
fn next(&mut self) -> Option<Self::Item> {
19+
self.0.next()
20+
}
2021
}

0 commit comments

Comments
 (0)