From 553bff1823a30293c82fb811f4457b094700a728 Mon Sep 17 00:00:00 2001 From: Liam Israel <138258207+Larmbs@users.noreply.github.com> Date: Wed, 3 Jul 2024 23:18:17 -0400 Subject: [PATCH] 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 <8753214+IvanIsCoding@users.noreply.github.com> --- rustworkx-core/src/bipartite_coloring.rs | 22 ++++++++--------- .../src/connectivity/biconnected.rs | 2 +- rustworkx-core/src/dag_algo.rs | 24 ++++++++++--------- .../src/generators/barbell_graph.rs | 7 +++--- .../src/generators/heavy_hex_graph.rs | 4 ++-- .../src/generators/heavy_square_graph.rs | 4 ++-- rustworkx-core/src/generators/utils.rs | 4 ++-- rustworkx-core/src/graph_ext/contraction.rs | 12 ++-------- rustworkx-core/src/min_scored.rs | 2 +- rustworkx-core/src/planar/lr_planar.rs | 8 ++----- rustworkx-core/src/steiner_tree.rs | 1 + rustworkx-core/src/token_swapper.rs | 2 +- 12 files changed, 41 insertions(+), 51 deletions(-) diff --git a/rustworkx-core/src/bipartite_coloring.rs b/rustworkx-core/src/bipartite_coloring.rs index c56a97ff8..a4bdbecf4 100644 --- a/rustworkx-core/src/bipartite_coloring.rs +++ b/rustworkx-core/src/bipartite_coloring.rs @@ -340,18 +340,18 @@ fn rbmg_edge_color(g0: &RegularBipartiteMultiGraph) -> Vec { let mut g: RegularBipartiteMultiGraph = RegularBipartiteMultiGraph::clone(g0); let mut coloring: Vec = Vec::with_capacity(g.degree); - if g.degree == 0 { - return coloring; - } - - if g.degree == 1 { - let mut matching: Matching = Vec::with_capacity(g.l_nodes.len()); - for edge in g.graph.edge_references() { - matching.push((edge.source(), edge.target())); + match g.degree { + 0 => return coloring, + 1 => { + let mut matching: Matching = Vec::with_capacity(g.l_nodes.len()); + for edge in g.graph.edge_references() { + matching.push((edge.source(), edge.target())); + } + coloring.push(matching); + return coloring; } - coloring.push(matching); - return coloring; - } + _ => (), + }; let mut odd_degree_matching: Option = None; diff --git a/rustworkx-core/src/connectivity/biconnected.rs b/rustworkx-core/src/connectivity/biconnected.rs index 5dfb6382c..b0a1b0f6a 100644 --- a/rustworkx-core/src/connectivity/biconnected.rs +++ b/rustworkx-core/src/connectivity/biconnected.rs @@ -140,7 +140,7 @@ where } } } - _ => {} + _ => (), }); if let Some(x) = components { diff --git a/rustworkx-core/src/dag_algo.rs b/rustworkx-core/src/dag_algo.rs index 9023e0387..97dff04a3 100644 --- a/rustworkx-core/src/dag_algo.rs +++ b/rustworkx-core/src/dag_algo.rs @@ -595,8 +595,8 @@ where // Remove null edges from color_fn let colors = colors.into_iter().flatten().collect::>(); - if colors.len() <= 2 && is_match { - if colors.len() == 1 { + match (colors.len(), is_match) { + (1, true) => { let c0 = colors[0]; ensure_vector_has_index!(pending_list, block_id, c0); if let Some(c0_block_id) = block_id[c0] { @@ -604,7 +604,8 @@ where } else { pending_list[c0].push(node); } - } else if colors.len() == 2 { + } + (2, true) => { let c0 = colors[0]; let c1 = colors[1]; ensure_vector_has_index!(pending_list, block_id, c0); @@ -619,7 +620,7 @@ where let mut new_block: Vec = Vec::with_capacity(pending_list[c0].len() + pending_list[c1].len() + 1); - // Clears pending lits and add to new block + // Clears pending list and add to new block new_block.append(&mut pending_list[c0]); new_block.append(&mut pending_list[c1]); @@ -631,14 +632,15 @@ where block_list.push(new_block); } } - } else { - for color in colors { - ensure_vector_has_index!(pending_list, block_id, color); - if let Some(color_block_id) = block_id[color] { - block_list[color_block_id].append(&mut pending_list[color]); + _ => { + for color in colors { + ensure_vector_has_index!(pending_list, block_id, color); + if let Some(color_block_id) = block_id[color] { + block_list[color_block_id].append(&mut pending_list[color]); + } + block_id[color] = None; + pending_list[color].clear(); } - block_id[color] = None; - pending_list[color].clear(); } } } diff --git a/rustworkx-core/src/generators/barbell_graph.rs b/rustworkx-core/src/generators/barbell_graph.rs index 74d6f56f1..ec4fd3b1f 100644 --- a/rustworkx-core/src/generators/barbell_graph.rs +++ b/rustworkx-core/src/generators/barbell_graph.rs @@ -151,10 +151,9 @@ where default_edge_weight(), ); for (node_a, node_b) in pairwise(path_nodes) { - match node_a { - Some(node_a) => graph.add_edge(node_a, node_b, default_edge_weight()), - None => continue, - }; + if let Some(node_a) = node_a { + graph.add_edge(node_a, node_b, default_edge_weight()); + } } } diff --git a/rustworkx-core/src/generators/heavy_hex_graph.rs b/rustworkx-core/src/generators/heavy_hex_graph.rs index abe3a3203..5080f76d5 100644 --- a/rustworkx-core/src/generators/heavy_hex_graph.rs +++ b/rustworkx-core/src/generators/heavy_hex_graph.rs @@ -170,7 +170,7 @@ where default_edge_weight(), ); } - } else if i % 2 == 1 { + } else { graph.add_edge( nodes_data[i * d + (d - 1)], syndrome_chunk[syndrome_chunk.len() - 1], @@ -225,7 +225,7 @@ where } } } - } else if i % 2 == 1 { + } else { for (j, syndrome) in syndrome_chunk.iter().enumerate() { if j != syndrome_chunk.len() - 1 { graph.add_edge( diff --git a/rustworkx-core/src/generators/heavy_square_graph.rs b/rustworkx-core/src/generators/heavy_square_graph.rs index 52777f8ea..dcf10f566 100644 --- a/rustworkx-core/src/generators/heavy_square_graph.rs +++ b/rustworkx-core/src/generators/heavy_square_graph.rs @@ -171,7 +171,7 @@ where default_edge_weight(), ); } - } else if i % 2 == 1 { + } else { graph.add_edge(nodes_data[i * d], syndrome_chunk[0], default_edge_weight()); graph.add_edge( nodes_data[(i + 1) * d], @@ -218,7 +218,7 @@ where } } } - } else if i % 2 == 1 { + } else { for (j, syndrome) in syndrome_chunk.iter().enumerate() { if j != 0 { graph.add_edge( diff --git a/rustworkx-core/src/generators/utils.rs b/rustworkx-core/src/generators/utils.rs index b63ab7043..c8c31702d 100644 --- a/rustworkx-core/src/generators/utils.rs +++ b/rustworkx-core/src/generators/utils.rs @@ -12,8 +12,8 @@ #[inline] pub fn get_num_nodes(num_nodes: &Option, weights: &Option>) -> usize { - if weights.is_some() { - weights.as_ref().unwrap().len() + if let Some(v) = weights { + v.len() } else { num_nodes.unwrap() } diff --git a/rustworkx-core/src/graph_ext/contraction.rs b/rustworkx-core/src/graph_ext/contraction.rs index bebb5b0bc..6c1b26afb 100644 --- a/rustworkx-core/src/graph_ext/contraction.rs +++ b/rustworkx-core/src/graph_ext/contraction.rs @@ -556,11 +556,7 @@ where .flat_map(|i| graph.edges_directed(*i, Direction::Incoming)) .filter_map(|edge| { let pred = edge.source(); - if !nodes.contains(&pred) { - Some((pred, edge.weight().clone())) - } else { - None - } + (!nodes.contains(&pred)).then_some((pred, edge.weight().clone())) }) .collect(); @@ -579,11 +575,7 @@ where .flat_map(|&i| graph.edges_directed(i, Direction::Outgoing)) .filter_map(|edge| { let succ = edge.target(); - if !nodes.contains(&succ) { - Some((succ, edge.weight().clone())) - } else { - None - } + (!nodes.contains(&succ)).then_some((succ, edge.weight().clone())) }) .collect(); diff --git a/rustworkx-core/src/min_scored.rs b/rustworkx-core/src/min_scored.rs index 1712cb36a..0ae9bab65 100644 --- a/rustworkx-core/src/min_scored.rs +++ b/rustworkx-core/src/min_scored.rs @@ -35,7 +35,7 @@ pub struct MinScored(pub K, pub T); impl PartialEq for MinScored { #[inline] fn eq(&self, other: &MinScored) -> bool { - self.cmp(other) == Ordering::Equal + self.cmp(other).is_eq() } } diff --git a/rustworkx-core/src/planar/lr_planar.rs b/rustworkx-core/src/planar/lr_planar.rs index 6e11724a0..bfe77fe5c 100644 --- a/rustworkx-core/src/planar/lr_planar.rs +++ b/rustworkx-core/src/planar/lr_planar.rs @@ -55,11 +55,7 @@ where .edges(a) .filter_map(|edge| { let e = (edge.source(), edge.target()); - if filter(&e) { - Some(e) - } else { - None - } + filter(&e).then_some(e) }) .collect::>(); edges.sort_by_key(compare); @@ -335,7 +331,7 @@ where } } } - _ => {} + _ => (), } } diff --git a/rustworkx-core/src/steiner_tree.rs b/rustworkx-core/src/steiner_tree.rs index ea39afea1..c93bd1a78 100644 --- a/rustworkx-core/src/steiner_tree.rs +++ b/rustworkx-core/src/steiner_tree.rs @@ -61,6 +61,7 @@ where }) .collect()); } + let mut edge_weights: Vec> = vec![None; graph.edge_bound()]; for edge in graph.edge_references() { let index = EdgeIndexable::to_index(&graph, edge.id()); diff --git a/rustworkx-core/src/token_swapper.rs b/rustworkx-core/src/token_swapper.rs index bb3bb4d16..a982d35e7 100644 --- a/rustworkx-core/src/token_swapper.rs +++ b/rustworkx-core/src/token_swapper.rs @@ -196,7 +196,7 @@ where // Adds an edge to digraph if distance from the token to a neighbor is // less than distance from token to node. sub_digraph is same except // for self-edges. - if !(tokens.contains_key(&node)) { + if !tokens.contains_key(&node) { return Ok(()); } if tokens[&node] == node {