Skip to content

Commit 553bff1

Browse files
LarmbsIvanIsCoding
andauthored
Cleaned up some control flow and optimized some parts. (#1241)
* Tweaks to mostly control flow, as well as some grammar corrections. * Formated parts --------- Co-authored-by: Ivan Carvalho <[email protected]>
1 parent b18e494 commit 553bff1

File tree

12 files changed

+41
-51
lines changed

12 files changed

+41
-51
lines changed

rustworkx-core/src/bipartite_coloring.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,18 +340,18 @@ fn rbmg_edge_color(g0: &RegularBipartiteMultiGraph) -> Vec<Matching> {
340340
let mut g: RegularBipartiteMultiGraph = RegularBipartiteMultiGraph::clone(g0);
341341
let mut coloring: Vec<Matching> = Vec::with_capacity(g.degree);
342342

343-
if g.degree == 0 {
344-
return coloring;
345-
}
346-
347-
if g.degree == 1 {
348-
let mut matching: Matching = Vec::with_capacity(g.l_nodes.len());
349-
for edge in g.graph.edge_references() {
350-
matching.push((edge.source(), edge.target()));
343+
match g.degree {
344+
0 => return coloring,
345+
1 => {
346+
let mut matching: Matching = Vec::with_capacity(g.l_nodes.len());
347+
for edge in g.graph.edge_references() {
348+
matching.push((edge.source(), edge.target()));
349+
}
350+
coloring.push(matching);
351+
return coloring;
351352
}
352-
coloring.push(matching);
353-
return coloring;
354-
}
353+
_ => (),
354+
};
355355

356356
let mut odd_degree_matching: Option<Matching> = None;
357357

rustworkx-core/src/connectivity/biconnected.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ where
140140
}
141141
}
142142
}
143-
_ => {}
143+
_ => (),
144144
});
145145

146146
if let Some(x) = components {

rustworkx-core/src/dag_algo.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -595,16 +595,17 @@ where
595595
// Remove null edges from color_fn
596596
let colors = colors.into_iter().flatten().collect::<Vec<usize>>();
597597

598-
if colors.len() <= 2 && is_match {
599-
if colors.len() == 1 {
598+
match (colors.len(), is_match) {
599+
(1, true) => {
600600
let c0 = colors[0];
601601
ensure_vector_has_index!(pending_list, block_id, c0);
602602
if let Some(c0_block_id) = block_id[c0] {
603603
block_list[c0_block_id].push(node);
604604
} else {
605605
pending_list[c0].push(node);
606606
}
607-
} else if colors.len() == 2 {
607+
}
608+
(2, true) => {
608609
let c0 = colors[0];
609610
let c1 = colors[1];
610611
ensure_vector_has_index!(pending_list, block_id, c0);
@@ -619,7 +620,7 @@ where
619620
let mut new_block: Vec<G::NodeId> =
620621
Vec::with_capacity(pending_list[c0].len() + pending_list[c1].len() + 1);
621622

622-
// Clears pending lits and add to new block
623+
// Clears pending list and add to new block
623624
new_block.append(&mut pending_list[c0]);
624625
new_block.append(&mut pending_list[c1]);
625626

@@ -631,14 +632,15 @@ where
631632
block_list.push(new_block);
632633
}
633634
}
634-
} else {
635-
for color in colors {
636-
ensure_vector_has_index!(pending_list, block_id, color);
637-
if let Some(color_block_id) = block_id[color] {
638-
block_list[color_block_id].append(&mut pending_list[color]);
635+
_ => {
636+
for color in colors {
637+
ensure_vector_has_index!(pending_list, block_id, color);
638+
if let Some(color_block_id) = block_id[color] {
639+
block_list[color_block_id].append(&mut pending_list[color]);
640+
}
641+
block_id[color] = None;
642+
pending_list[color].clear();
639643
}
640-
block_id[color] = None;
641-
pending_list[color].clear();
642644
}
643645
}
644646
}

rustworkx-core/src/generators/barbell_graph.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ where
151151
default_edge_weight(),
152152
);
153153
for (node_a, node_b) in pairwise(path_nodes) {
154-
match node_a {
155-
Some(node_a) => graph.add_edge(node_a, node_b, default_edge_weight()),
156-
None => continue,
157-
};
154+
if let Some(node_a) = node_a {
155+
graph.add_edge(node_a, node_b, default_edge_weight());
156+
}
158157
}
159158
}
160159

rustworkx-core/src/generators/heavy_hex_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ where
170170
default_edge_weight(),
171171
);
172172
}
173-
} else if i % 2 == 1 {
173+
} else {
174174
graph.add_edge(
175175
nodes_data[i * d + (d - 1)],
176176
syndrome_chunk[syndrome_chunk.len() - 1],
@@ -225,7 +225,7 @@ where
225225
}
226226
}
227227
}
228-
} else if i % 2 == 1 {
228+
} else {
229229
for (j, syndrome) in syndrome_chunk.iter().enumerate() {
230230
if j != syndrome_chunk.len() - 1 {
231231
graph.add_edge(

rustworkx-core/src/generators/heavy_square_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ where
171171
default_edge_weight(),
172172
);
173173
}
174-
} else if i % 2 == 1 {
174+
} else {
175175
graph.add_edge(nodes_data[i * d], syndrome_chunk[0], default_edge_weight());
176176
graph.add_edge(
177177
nodes_data[(i + 1) * d],
@@ -218,7 +218,7 @@ where
218218
}
219219
}
220220
}
221-
} else if i % 2 == 1 {
221+
} else {
222222
for (j, syndrome) in syndrome_chunk.iter().enumerate() {
223223
if j != 0 {
224224
graph.add_edge(

rustworkx-core/src/generators/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#[inline]
1414
pub fn get_num_nodes<T>(num_nodes: &Option<usize>, weights: &Option<Vec<T>>) -> usize {
15-
if weights.is_some() {
16-
weights.as_ref().unwrap().len()
15+
if let Some(v) = weights {
16+
v.len()
1717
} else {
1818
num_nodes.unwrap()
1919
}

rustworkx-core/src/graph_ext/contraction.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,7 @@ where
556556
.flat_map(|i| graph.edges_directed(*i, Direction::Incoming))
557557
.filter_map(|edge| {
558558
let pred = edge.source();
559-
if !nodes.contains(&pred) {
560-
Some((pred, edge.weight().clone()))
561-
} else {
562-
None
563-
}
559+
(!nodes.contains(&pred)).then_some((pred, edge.weight().clone()))
564560
})
565561
.collect();
566562

@@ -579,11 +575,7 @@ where
579575
.flat_map(|&i| graph.edges_directed(i, Direction::Outgoing))
580576
.filter_map(|edge| {
581577
let succ = edge.target();
582-
if !nodes.contains(&succ) {
583-
Some((succ, edge.weight().clone()))
584-
} else {
585-
None
586-
}
578+
(!nodes.contains(&succ)).then_some((succ, edge.weight().clone()))
587579
})
588580
.collect();
589581

rustworkx-core/src/min_scored.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct MinScored<K, T>(pub K, pub T);
3535
impl<K: PartialOrd, T> PartialEq for MinScored<K, T> {
3636
#[inline]
3737
fn eq(&self, other: &MinScored<K, T>) -> bool {
38-
self.cmp(other) == Ordering::Equal
38+
self.cmp(other).is_eq()
3939
}
4040
}
4141

rustworkx-core/src/planar/lr_planar.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ where
5555
.edges(a)
5656
.filter_map(|edge| {
5757
let e = (edge.source(), edge.target());
58-
if filter(&e) {
59-
Some(e)
60-
} else {
61-
None
62-
}
58+
filter(&e).then_some(e)
6359
})
6460
.collect::<Vec<_>>();
6561
edges.sort_by_key(compare);
@@ -335,7 +331,7 @@ where
335331
}
336332
}
337333
}
338-
_ => {}
334+
_ => (),
339335
}
340336
}
341337

0 commit comments

Comments
 (0)