From f0600cc074eca0d5ae51aec5c8360402a9012743 Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Wed, 1 Oct 2025 00:34:16 +0200 Subject: [PATCH] style: include `if_not_else` --- Cargo.toml | 1 - src/data_structures/b_tree.rs | 6 +- src/data_structures/rb_tree.rs | 196 +++++++++++++------------- src/graph/lee_breadth_first_search.rs | 6 +- src/math/elliptic_curve.rs | 6 +- src/math/modular_exponential.rs | 6 +- src/math/trig_functions.rs | 12 +- src/sorting/heap_sort.rs | 6 +- src/string/z_algorithm.rs | 6 +- 9 files changed, 121 insertions(+), 124 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 88b3ff0577a..2ca4eda0566 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,6 @@ doc_markdown = { level = "allow", priority = 1 } explicit_deref_methods = { level = "allow", priority = 1 } explicit_iter_loop = { level = "allow", priority = 1 } float_cmp = { level = "allow", priority = 1 } -if_not_else = { level = "allow", priority = 1 } implicit_clone = { level = "allow", priority = 1 } implicit_hasher = { level = "allow", priority = 1 } items_after_statements = { level = "allow", priority = 1 } diff --git a/src/data_structures/b_tree.rs b/src/data_structures/b_tree.rs index bf7266932ae..74316a3dc31 100644 --- a/src/data_structures/b_tree.rs +++ b/src/data_structures/b_tree.rs @@ -68,10 +68,10 @@ impl BTreeProps { } None => Vec::with_capacity(self.max_keys), }; - let right_children = if !child.is_leaf() { - Some(child.children.split_off(self.mid_key_index + 1)) - } else { + let right_children = if child.is_leaf() { None + } else { + Some(child.children.split_off(self.mid_key_index + 1)) }; let new_child_node: Node = Node::new(self.degree, Some(right_keys), right_children); diff --git a/src/data_structures/rb_tree.rs b/src/data_structures/rb_tree.rs index 3465ad5d4d3..df7d09cb006 100644 --- a/src/data_structures/rb_tree.rs +++ b/src/data_structures/rb_tree.rs @@ -76,14 +76,12 @@ impl RBTree { } } node = Box::into_raw(Box::new(RBNode::new(key, value))); - if !parent.is_null() { - if (*node).key < (*parent).key { - (*parent).left = node; - } else { - (*parent).right = node; - } - } else { + if parent.is_null() { self.root = node; + } else if (*node).key < (*parent).key { + (*parent).left = node; + } else { + (*parent).right = node; } (*node).parent = parent; insert_fixup(self, node); @@ -258,17 +256,17 @@ unsafe fn insert_fixup(tree: &mut RBTree, mut node: *mut RBNode gparent = (*parent).parent; tmp = (*gparent).right; - if parent != tmp { - /* parent = (*gparent).left */ + if parent == tmp { + /* parent = (*gparent).right */ + tmp = (*gparent).left; if !tmp.is_null() && matches!((*tmp).color, Color::Red) { /* * Case 1 - color flips and recurse at g - * - * G g - * / \ / \ - * p u --> P U - * / / - * n n + * G g + * / \ / \ + * u p --> U P + * \ \ + * n n */ (*parent).color = Color::Black; @@ -278,46 +276,45 @@ unsafe fn insert_fixup(tree: &mut RBTree, mut node: *mut RBNode parent = (*node).parent; continue; } - tmp = (*parent).right; + tmp = (*parent).left; if node == tmp { - /* node = (*parent).right */ /* - * Case 2 - left rotate at p (then Case 3) + * Case 2 - right rotate at p (then Case 3) * - * G G - * / \ / \ - * p U --> n U - * \ / - * n p + * G G + * / \ / \ + * U p --> U n + * / \ + * n p */ - left_rotate(tree, parent); + right_rotate(tree, parent); parent = node; } /* - * Case 3 - right rotate at g + * Case 3 - left rotate at g * - * G P - * / \ / \ - * p U --> n g - * / \ - * n U + * G P + * / \ / \ + * U p --> g n + * \ / + * n U */ (*parent).color = Color::Black; (*gparent).color = Color::Red; - right_rotate(tree, gparent); + left_rotate(tree, gparent); } else { - /* parent = (*gparent).right */ - tmp = (*gparent).left; + /* parent = (*gparent).left */ if !tmp.is_null() && matches!((*tmp).color, Color::Red) { /* * Case 1 - color flips and recurse at g - * G g - * / \ / \ - * u p --> U P - * \ \ - * n n + * + * G g + * / \ / \ + * p u --> P U + * / / + * n n */ (*parent).color = Color::Black; @@ -327,34 +324,35 @@ unsafe fn insert_fixup(tree: &mut RBTree, mut node: *mut RBNode parent = (*node).parent; continue; } - tmp = (*parent).left; + tmp = (*parent).right; if node == tmp { + /* node = (*parent).right */ /* - * Case 2 - right rotate at p (then Case 3) + * Case 2 - left rotate at p (then Case 3) * - * G G - * / \ / \ - * U p --> U n - * / \ - * n p + * G G + * / \ / \ + * p U --> n U + * \ / + * n p */ - right_rotate(tree, parent); + left_rotate(tree, parent); parent = node; } /* - * Case 3 - left rotate at g + * Case 3 - right rotate at g * - * G P - * / \ / \ - * U p --> g n - * \ / - * n U + * G P + * / \ / \ + * p U --> n g + * / \ + * n U */ (*parent).color = Color::Black; (*gparent).color = Color::Red; - left_rotate(tree, gparent); + right_rotate(tree, gparent); } break; } @@ -377,7 +375,52 @@ unsafe fn delete_fixup(tree: &mut RBTree, mut parent: *mut RBNo * black node count that is 1 lower than other leaf paths. */ sibling = (*parent).right; - if node != sibling { + if node == sibling { + /* node = (*parent).right */ + sibling = (*parent).left; + if matches!((*sibling).color, Color::Red) { + /* + * Case 1 - right rotate at parent + */ + + right_rotate(tree, parent); + (*parent).color = Color::Red; + (*sibling).color = Color::Black; + sibling = (*parent).right; + } + sl = (*sibling).left; + sr = (*sibling).right; + + if !sr.is_null() && matches!((*sr).color, Color::Red) { + /* + * Case 2 - left rotate at sibling and then right rotate at parent + */ + + (*sr).color = (*parent).color; + (*parent).color = Color::Black; + left_rotate(tree, sibling); + right_rotate(tree, parent); + } else if !sl.is_null() && matches!((*sl).color, Color::Red) { + /* + * Case 3 - right rotate at parent + */ + + (*sl).color = (*parent).color; + right_rotate(tree, parent); + } else { + /* + * Case 4 - color flip + */ + + (*sibling).color = Color::Red; + if matches!((*parent).color, Color::Black) { + node = parent; + parent = (*node).parent; + continue; + } + (*parent).color = Color::Black; + } + } else { /* node = (*parent).left */ if matches!((*sibling).color, Color::Red) { /* @@ -442,51 +485,6 @@ unsafe fn delete_fixup(tree: &mut RBTree, mut parent: *mut RBNo * Sl Sr Sl Sr */ - (*sibling).color = Color::Red; - if matches!((*parent).color, Color::Black) { - node = parent; - parent = (*node).parent; - continue; - } - (*parent).color = Color::Black; - } - } else { - /* node = (*parent).right */ - sibling = (*parent).left; - if matches!((*sibling).color, Color::Red) { - /* - * Case 1 - right rotate at parent - */ - - right_rotate(tree, parent); - (*parent).color = Color::Red; - (*sibling).color = Color::Black; - sibling = (*parent).right; - } - sl = (*sibling).left; - sr = (*sibling).right; - - if !sr.is_null() && matches!((*sr).color, Color::Red) { - /* - * Case 2 - left rotate at sibling and then right rotate at parent - */ - - (*sr).color = (*parent).color; - (*parent).color = Color::Black; - left_rotate(tree, sibling); - right_rotate(tree, parent); - } else if !sl.is_null() && matches!((*sl).color, Color::Red) { - /* - * Case 3 - right rotate at parent - */ - - (*sl).color = (*parent).color; - right_rotate(tree, parent); - } else { - /* - * Case 4 - color flip - */ - (*sibling).color = Color::Red; if matches!((*parent).color, Color::Black) { node = parent; diff --git a/src/graph/lee_breadth_first_search.rs b/src/graph/lee_breadth_first_search.rs index e0c25fd7906..0889221cd81 100644 --- a/src/graph/lee_breadth_first_search.rs +++ b/src/graph/lee_breadth_first_search.rs @@ -47,10 +47,10 @@ pub fn lee(matrix: Vec>, source: (usize, usize), destination: (usize, u } } - if min_dist != isize::MAX { - min_dist - } else { + if min_dist == isize::MAX { -1 + } else { + min_dist } } diff --git a/src/math/elliptic_curve.rs b/src/math/elliptic_curve.rs index 641b759dc8b..9409f01cfa7 100644 --- a/src/math/elliptic_curve.rs +++ b/src/math/elliptic_curve.rs @@ -182,10 +182,10 @@ impl Add for EllipticCurve { // mirrored Self::infinity() } else { - let slope = if self.x != p.x { - (self.y - p.y) / (self.x - p.x) - } else { + let slope = if self.x == p.x { ((self.x * self.x).integer_mul(3) + F::from_integer(A)) / self.y.integer_mul(2) + } else { + (self.y - p.y) / (self.x - p.x) }; let x = slope * slope - self.x - p.x; let y = -self.y + slope * (self.x - x); diff --git a/src/math/modular_exponential.rs b/src/math/modular_exponential.rs index 1e9a9f41cac..9d0029970b2 100644 --- a/src/math/modular_exponential.rs +++ b/src/math/modular_exponential.rs @@ -37,11 +37,11 @@ pub fn gcd_extended(a: i64, m: i64) -> (i64, i64, i64) { /// Panics if the inverse does not exist (i.e., `b` and `m` are not coprime). pub fn mod_inverse(b: i64, m: i64) -> i64 { let (gcd, x, _) = gcd_extended(b, m); - if gcd != 1 { - panic!("Inverse does not exist"); - } else { + if gcd == 1 { // Ensure the modular inverse is positive (x % m + m) % m + } else { + panic!("Inverse does not exist"); } } diff --git a/src/math/trig_functions.rs b/src/math/trig_functions.rs index e18b5154029..68cb8a95b2d 100644 --- a/src/math/trig_functions.rs +++ b/src/math/trig_functions.rs @@ -103,11 +103,11 @@ pub fn tan + Copy>(x: T, tol: f64) -> f64 { let cos_val = cosine(x, tol); /* Cover special cases for division */ - if cos_val != 0f64 { + if cos_val == 0f64 { + f64::NAN + } else { let sin_val = sine(x, tol); sin_val / cos_val - } else { - f64::NAN } } @@ -116,11 +116,11 @@ pub fn cotan + Copy>(x: T, tol: f64) -> f64 { let sin_val = sine(x, tol); /* Cover special cases for division */ - if sin_val != 0f64 { + if sin_val == 0f64 { + f64::NAN + } else { let cos_val = cosine(x, tol); cos_val / sin_val - } else { - f64::NAN } } diff --git a/src/sorting/heap_sort.rs b/src/sorting/heap_sort.rs index 7b37a7c5149..8369d805da9 100644 --- a/src/sorting/heap_sort.rs +++ b/src/sorting/heap_sort.rs @@ -30,10 +30,10 @@ fn build_heap(arr: &mut [T], is_max_heap: bool) { /// * `i` - The index to start fixing the heap violation. /// * `is_max_heap` - A boolean indicating whether to maintain a max heap or a min heap. fn heapify(arr: &mut [T], i: usize, is_max_heap: bool) { - let comparator: fn(&T, &T) -> Ordering = if !is_max_heap { - |a, b| b.cmp(a) - } else { + let comparator: fn(&T, &T) -> Ordering = if is_max_heap { |a, b| a.cmp(b) + } else { + |a, b| b.cmp(a) }; let mut idx = i; diff --git a/src/string/z_algorithm.rs b/src/string/z_algorithm.rs index a2825e02ddc..df28cfa210d 100644 --- a/src/string/z_algorithm.rs +++ b/src/string/z_algorithm.rs @@ -103,10 +103,10 @@ fn match_with_z_array( } } - if !only_full_matches { - z_array - } else { + if only_full_matches { find_full_matches(&z_array, pattern_size) + } else { + z_array } }