Skip to content

Commit

Permalink
Cleaned up some control flow and optimized some parts. (#1241)
Browse files Browse the repository at this point in the history
* Tweaks to mostly control flow, as well as some grammar corrections.

* Formated parts

---------

Co-authored-by: Ivan Carvalho <[email protected]>
  • Loading branch information
Larmbs and IvanIsCoding authored Jul 4, 2024
1 parent b18e494 commit 553bff1
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 51 deletions.
22 changes: 11 additions & 11 deletions rustworkx-core/src/bipartite_coloring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,18 @@ fn rbmg_edge_color(g0: &RegularBipartiteMultiGraph) -> Vec<Matching> {
let mut g: RegularBipartiteMultiGraph = RegularBipartiteMultiGraph::clone(g0);
let mut coloring: Vec<Matching> = 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<Matching> = None;

Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/src/connectivity/biconnected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ where
}
}
}
_ => {}
_ => (),
});

if let Some(x) = components {
Expand Down
24 changes: 13 additions & 11 deletions rustworkx-core/src/dag_algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,17 @@ where
// Remove null edges from color_fn
let colors = colors.into_iter().flatten().collect::<Vec<usize>>();

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] {
block_list[c0_block_id].push(node);
} 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);
Expand All @@ -619,7 +620,7 @@ where
let mut new_block: Vec<G::NodeId> =
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]);

Expand All @@ -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();
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions rustworkx-core/src/generators/barbell_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions rustworkx-core/src/generators/heavy_hex_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions rustworkx-core/src/generators/heavy_square_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions rustworkx-core/src/generators/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#[inline]
pub fn get_num_nodes<T>(num_nodes: &Option<usize>, weights: &Option<Vec<T>>) -> usize {
if weights.is_some() {
weights.as_ref().unwrap().len()
if let Some(v) = weights {
v.len()
} else {
num_nodes.unwrap()
}
Expand Down
12 changes: 2 additions & 10 deletions rustworkx-core/src/graph_ext/contraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/src/min_scored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct MinScored<K, T>(pub K, pub T);
impl<K: PartialOrd, T> PartialEq for MinScored<K, T> {
#[inline]
fn eq(&self, other: &MinScored<K, T>) -> bool {
self.cmp(other) == Ordering::Equal
self.cmp(other).is_eq()
}
}

Expand Down
8 changes: 2 additions & 6 deletions rustworkx-core/src/planar/lr_planar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
edges.sort_by_key(compare);
Expand Down Expand Up @@ -335,7 +331,7 @@ where
}
}
}
_ => {}
_ => (),
}
}

Expand Down
1 change: 1 addition & 0 deletions rustworkx-core/src/steiner_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ where
})
.collect());
}

let mut edge_weights: Vec<Option<f64>> = vec![None; graph.edge_bound()];
for edge in graph.edge_references() {
let index = EdgeIndexable::to_index(&graph, edge.id());
Expand Down
2 changes: 1 addition & 1 deletion rustworkx-core/src/token_swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 553bff1

Please sign in to comment.