code refactoring with std::cmp::Ordering
diff --git a/src/algo/dominators.rs b/src/algo/dominators.rs
index c1f778e..4d39f2e 100644
--- a/src/algo/dominators.rs
+++ b/src/algo/dominators.rs
@@ -12,6 +12,7 @@
//! strictly dominates **B** and there does not exist any node **C** where **A**
//! dominates **C** and **C** dominates **B**.
+use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
@@ -189,14 +190,13 @@
}
fn intersect(dominators: &[usize], mut finger1: usize, mut finger2: usize) -> usize {
- while finger1 != finger2 {
- if finger1 < finger2 {
- finger1 = dominators[finger1];
- } else if finger2 < finger1 {
- finger2 = dominators[finger2];
+ loop {
+ match finger1.cmp(&finger2) {
+ Ordering::Less => finger1 = dominators[finger1],
+ Ordering::Greater => finger2 = dominators[finger2],
+ Ordering::Equal => return finger1,
}
}
- finger1
}
fn predecessor_sets_to_idx_vecs<N>(post_order: &[N],
diff --git a/src/graph_impl/stable_graph/mod.rs b/src/graph_impl/stable_graph/mod.rs
index 3a0f90f..a0bae5b 100644
--- a/src/graph_impl/stable_graph/mod.rs
+++ b/src/graph_impl/stable_graph/mod.rs
@@ -290,13 +290,7 @@
/// edges, including *n* calls to `.remove_edge()` where *n* is the number
/// of edges with an endpoint in `a`.
pub fn remove_node(&mut self, a: NodeIndex<Ix>) -> Option<N> {
- let node_weight = match self.g.nodes.get_mut(a.index()) {
- None => return None,
- Some(n) => n.weight.take(),
- };
- if let None = node_weight {
- return None;
- }
+ let node_weight = self.g.nodes.get_mut(a.index())?.weight.take()?;
for d in &DIRECTIONS {
let k = d.index();
@@ -319,7 +313,7 @@
self.free_node = a;
self.node_count -= 1;
- node_weight
+ Some(node_weight)
}
pub fn contains_node(&self, a: NodeIndex<Ix>) -> bool {
diff --git a/src/graphmap.rs b/src/graphmap.rs
index 3ec4076..2183235 100644
--- a/src/graphmap.rs
+++ b/src/graphmap.rs
@@ -135,10 +135,10 @@
/// Use their natural order to map the node pair (a, b) to a canonical edge id.
#[inline]
fn edge_key(a: N, b: N) -> (N, N) {
- if Ty::is_directed() {
+ if Ty::is_directed() || a <= b {
(a, b)
} else {
- if a <= b { (a, b) } else { (b, a) }
+ (b, a)
}
}
diff --git a/src/unionfind.rs b/src/unionfind.rs
index d53c113..e64d8b6 100644
--- a/src/unionfind.rs
+++ b/src/unionfind.rs
@@ -1,5 +1,6 @@
//! `UnionFind<K>` is a disjoint-set data structure.
+use std::cmp::Ordering;
use super::graph::IndexType;
/// `UnionFind<K>` is a disjoint-set data structure. It tracks set membership of *n* elements
@@ -101,25 +102,25 @@
/// Returns `true` if the given elements belong to the same set, and returns
/// `false` otherwise.
pub fn equiv(&self, x: K, y: K) -> bool {
- self.find(x) == self.find(y)
+ self.find(x) == self.find(y)
}
/// Unify the two sets containing `x` and `y`.
///
/// Return `false` if the sets were already the same, `true` if they were unified.
- ///
+ ///
/// **Panics** if `x` or `y` is out of bounds.
pub fn union(&mut self, x: K, y: K) -> bool
{
if x == y {
- return false
+ return false;
}
let xrep = self.find_mut(x);
let yrep = self.find_mut(y);
if xrep == yrep {
- return false
+ return false;
}
let xrepu = xrep.index();
@@ -127,16 +128,15 @@
let xrank = self.rank[xrepu];
let yrank = self.rank[yrepu];
- // The rank corresponds roughly to the depth of the treeset, so put the
+ // The rank corresponds roughly to the depth of the treeset, so put the
// smaller set below the larger
- if xrank < yrank {
- self.parent[xrepu] = yrep;
- } else if xrank > yrank {
- self.parent[yrepu] = xrep;
- } else {
- // put y below x when equal.
- self.parent[yrepu] = xrep;
- self.rank[xrepu] += 1;
+ match xrank.cmp(&yrank) {
+ Ordering::Less => self.parent[xrepu] = yrep,
+ Ordering::Greater => self.parent[yrepu] = xrep,
+ Ordering::Equal => {
+ self.parent[yrepu] = xrep;
+ self.rank[xrepu] += 1;
+ }
}
true
}