Skip to content

Commit ed7a42e

Browse files
authored
style: include if_not_else (#918)
1 parent 7d7d716 commit ed7a42e

File tree

9 files changed

+121
-124
lines changed

9 files changed

+121
-124
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ doc_markdown = { level = "allow", priority = 1 }
3434
explicit_deref_methods = { level = "allow", priority = 1 }
3535
explicit_iter_loop = { level = "allow", priority = 1 }
3636
float_cmp = { level = "allow", priority = 1 }
37-
if_not_else = { level = "allow", priority = 1 }
3837
implicit_clone = { level = "allow", priority = 1 }
3938
implicit_hasher = { level = "allow", priority = 1 }
4039
items_after_statements = { level = "allow", priority = 1 }

src/data_structures/b_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ impl BTreeProps {
6868
}
6969
None => Vec::with_capacity(self.max_keys),
7070
};
71-
let right_children = if !child.is_leaf() {
72-
Some(child.children.split_off(self.mid_key_index + 1))
73-
} else {
71+
let right_children = if child.is_leaf() {
7472
None
73+
} else {
74+
Some(child.children.split_off(self.mid_key_index + 1))
7575
};
7676
let new_child_node: Node<T> = Node::new(self.degree, Some(right_keys), right_children);
7777

src/data_structures/rb_tree.rs

Lines changed: 97 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ impl<K: Ord, V> RBTree<K, V> {
7676
}
7777
}
7878
node = Box::into_raw(Box::new(RBNode::new(key, value)));
79-
if !parent.is_null() {
80-
if (*node).key < (*parent).key {
81-
(*parent).left = node;
82-
} else {
83-
(*parent).right = node;
84-
}
85-
} else {
79+
if parent.is_null() {
8680
self.root = node;
81+
} else if (*node).key < (*parent).key {
82+
(*parent).left = node;
83+
} else {
84+
(*parent).right = node;
8785
}
8886
(*node).parent = parent;
8987
insert_fixup(self, node);
@@ -258,17 +256,17 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut node: *mut RBNode
258256

259257
gparent = (*parent).parent;
260258
tmp = (*gparent).right;
261-
if parent != tmp {
262-
/* parent = (*gparent).left */
259+
if parent == tmp {
260+
/* parent = (*gparent).right */
261+
tmp = (*gparent).left;
263262
if !tmp.is_null() && matches!((*tmp).color, Color::Red) {
264263
/*
265264
* Case 1 - color flips and recurse at g
266-
*
267-
* G g
268-
* / \ / \
269-
* p u --> P U
270-
* / /
271-
* n n
265+
* G g
266+
* / \ / \
267+
* u p --> U P
268+
* \ \
269+
* n n
272270
*/
273271

274272
(*parent).color = Color::Black;
@@ -278,46 +276,45 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut node: *mut RBNode
278276
parent = (*node).parent;
279277
continue;
280278
}
281-
tmp = (*parent).right;
279+
tmp = (*parent).left;
282280
if node == tmp {
283-
/* node = (*parent).right */
284281
/*
285-
* Case 2 - left rotate at p (then Case 3)
282+
* Case 2 - right rotate at p (then Case 3)
286283
*
287-
* G G
288-
* / \ / \
289-
* p U --> n U
290-
* \ /
291-
* n p
284+
* G G
285+
* / \ / \
286+
* U p --> U n
287+
* / \
288+
* n p
292289
*/
293290

294-
left_rotate(tree, parent);
291+
right_rotate(tree, parent);
295292
parent = node;
296293
}
297294
/*
298-
* Case 3 - right rotate at g
295+
* Case 3 - left rotate at g
299296
*
300-
* G P
301-
* / \ / \
302-
* p U --> n g
303-
* / \
304-
* n U
297+
* G P
298+
* / \ / \
299+
* U p --> g n
300+
* \ /
301+
* n U
305302
*/
306303

307304
(*parent).color = Color::Black;
308305
(*gparent).color = Color::Red;
309-
right_rotate(tree, gparent);
306+
left_rotate(tree, gparent);
310307
} else {
311-
/* parent = (*gparent).right */
312-
tmp = (*gparent).left;
308+
/* parent = (*gparent).left */
313309
if !tmp.is_null() && matches!((*tmp).color, Color::Red) {
314310
/*
315311
* Case 1 - color flips and recurse at g
316-
* G g
317-
* / \ / \
318-
* u p --> U P
319-
* \ \
320-
* n n
312+
*
313+
* G g
314+
* / \ / \
315+
* p u --> P U
316+
* / /
317+
* n n
321318
*/
322319

323320
(*parent).color = Color::Black;
@@ -327,34 +324,35 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut node: *mut RBNode
327324
parent = (*node).parent;
328325
continue;
329326
}
330-
tmp = (*parent).left;
327+
tmp = (*parent).right;
331328
if node == tmp {
329+
/* node = (*parent).right */
332330
/*
333-
* Case 2 - right rotate at p (then Case 3)
331+
* Case 2 - left rotate at p (then Case 3)
334332
*
335-
* G G
336-
* / \ / \
337-
* U p --> U n
338-
* / \
339-
* n p
333+
* G G
334+
* / \ / \
335+
* p U --> n U
336+
* \ /
337+
* n p
340338
*/
341339

342-
right_rotate(tree, parent);
340+
left_rotate(tree, parent);
343341
parent = node;
344342
}
345343
/*
346-
* Case 3 - left rotate at g
344+
* Case 3 - right rotate at g
347345
*
348-
* G P
349-
* / \ / \
350-
* U p --> g n
351-
* \ /
352-
* n U
346+
* G P
347+
* / \ / \
348+
* p U --> n g
349+
* / \
350+
* n U
353351
*/
354352

355353
(*parent).color = Color::Black;
356354
(*gparent).color = Color::Red;
357-
left_rotate(tree, gparent);
355+
right_rotate(tree, gparent);
358356
}
359357
break;
360358
}
@@ -377,7 +375,52 @@ unsafe fn delete_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut parent: *mut RBNo
377375
* black node count that is 1 lower than other leaf paths.
378376
*/
379377
sibling = (*parent).right;
380-
if node != sibling {
378+
if node == sibling {
379+
/* node = (*parent).right */
380+
sibling = (*parent).left;
381+
if matches!((*sibling).color, Color::Red) {
382+
/*
383+
* Case 1 - right rotate at parent
384+
*/
385+
386+
right_rotate(tree, parent);
387+
(*parent).color = Color::Red;
388+
(*sibling).color = Color::Black;
389+
sibling = (*parent).right;
390+
}
391+
sl = (*sibling).left;
392+
sr = (*sibling).right;
393+
394+
if !sr.is_null() && matches!((*sr).color, Color::Red) {
395+
/*
396+
* Case 2 - left rotate at sibling and then right rotate at parent
397+
*/
398+
399+
(*sr).color = (*parent).color;
400+
(*parent).color = Color::Black;
401+
left_rotate(tree, sibling);
402+
right_rotate(tree, parent);
403+
} else if !sl.is_null() && matches!((*sl).color, Color::Red) {
404+
/*
405+
* Case 3 - right rotate at parent
406+
*/
407+
408+
(*sl).color = (*parent).color;
409+
right_rotate(tree, parent);
410+
} else {
411+
/*
412+
* Case 4 - color flip
413+
*/
414+
415+
(*sibling).color = Color::Red;
416+
if matches!((*parent).color, Color::Black) {
417+
node = parent;
418+
parent = (*node).parent;
419+
continue;
420+
}
421+
(*parent).color = Color::Black;
422+
}
423+
} else {
381424
/* node = (*parent).left */
382425
if matches!((*sibling).color, Color::Red) {
383426
/*
@@ -442,51 +485,6 @@ unsafe fn delete_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, mut parent: *mut RBNo
442485
* Sl Sr Sl Sr
443486
*/
444487

445-
(*sibling).color = Color::Red;
446-
if matches!((*parent).color, Color::Black) {
447-
node = parent;
448-
parent = (*node).parent;
449-
continue;
450-
}
451-
(*parent).color = Color::Black;
452-
}
453-
} else {
454-
/* node = (*parent).right */
455-
sibling = (*parent).left;
456-
if matches!((*sibling).color, Color::Red) {
457-
/*
458-
* Case 1 - right rotate at parent
459-
*/
460-
461-
right_rotate(tree, parent);
462-
(*parent).color = Color::Red;
463-
(*sibling).color = Color::Black;
464-
sibling = (*parent).right;
465-
}
466-
sl = (*sibling).left;
467-
sr = (*sibling).right;
468-
469-
if !sr.is_null() && matches!((*sr).color, Color::Red) {
470-
/*
471-
* Case 2 - left rotate at sibling and then right rotate at parent
472-
*/
473-
474-
(*sr).color = (*parent).color;
475-
(*parent).color = Color::Black;
476-
left_rotate(tree, sibling);
477-
right_rotate(tree, parent);
478-
} else if !sl.is_null() && matches!((*sl).color, Color::Red) {
479-
/*
480-
* Case 3 - right rotate at parent
481-
*/
482-
483-
(*sl).color = (*parent).color;
484-
right_rotate(tree, parent);
485-
} else {
486-
/*
487-
* Case 4 - color flip
488-
*/
489-
490488
(*sibling).color = Color::Red;
491489
if matches!((*parent).color, Color::Black) {
492490
node = parent;

src/graph/lee_breadth_first_search.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ pub fn lee(matrix: Vec<Vec<i32>>, source: (usize, usize), destination: (usize, u
4747
}
4848
}
4949

50-
if min_dist != isize::MAX {
51-
min_dist
52-
} else {
50+
if min_dist == isize::MAX {
5351
-1
52+
} else {
53+
min_dist
5454
}
5555
}
5656

src/math/elliptic_curve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ impl<F: Field, const A: i64, const B: i64> Add for EllipticCurve<F, A, B> {
182182
// mirrored
183183
Self::infinity()
184184
} else {
185-
let slope = if self.x != p.x {
186-
(self.y - p.y) / (self.x - p.x)
187-
} else {
185+
let slope = if self.x == p.x {
188186
((self.x * self.x).integer_mul(3) + F::from_integer(A)) / self.y.integer_mul(2)
187+
} else {
188+
(self.y - p.y) / (self.x - p.x)
189189
};
190190
let x = slope * slope - self.x - p.x;
191191
let y = -self.y + slope * (self.x - x);

src/math/modular_exponential.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ pub fn gcd_extended(a: i64, m: i64) -> (i64, i64, i64) {
3737
/// Panics if the inverse does not exist (i.e., `b` and `m` are not coprime).
3838
pub fn mod_inverse(b: i64, m: i64) -> i64 {
3939
let (gcd, x, _) = gcd_extended(b, m);
40-
if gcd != 1 {
41-
panic!("Inverse does not exist");
42-
} else {
40+
if gcd == 1 {
4341
// Ensure the modular inverse is positive
4442
(x % m + m) % m
43+
} else {
44+
panic!("Inverse does not exist");
4545
}
4646
}
4747

src/math/trig_functions.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ pub fn tan<T: Into<f64> + Copy>(x: T, tol: f64) -> f64 {
103103
let cos_val = cosine(x, tol);
104104

105105
/* Cover special cases for division */
106-
if cos_val != 0f64 {
106+
if cos_val == 0f64 {
107+
f64::NAN
108+
} else {
107109
let sin_val = sine(x, tol);
108110
sin_val / cos_val
109-
} else {
110-
f64::NAN
111111
}
112112
}
113113

@@ -116,11 +116,11 @@ pub fn cotan<T: Into<f64> + Copy>(x: T, tol: f64) -> f64 {
116116
let sin_val = sine(x, tol);
117117

118118
/* Cover special cases for division */
119-
if sin_val != 0f64 {
119+
if sin_val == 0f64 {
120+
f64::NAN
121+
} else {
120122
let cos_val = cosine(x, tol);
121123
cos_val / sin_val
122-
} else {
123-
f64::NAN
124124
}
125125
}
126126

src/sorting/heap_sort.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ fn build_heap<T: Ord>(arr: &mut [T], is_max_heap: bool) {
3030
/// * `i` - The index to start fixing the heap violation.
3131
/// * `is_max_heap` - A boolean indicating whether to maintain a max heap or a min heap.
3232
fn heapify<T: Ord>(arr: &mut [T], i: usize, is_max_heap: bool) {
33-
let comparator: fn(&T, &T) -> Ordering = if !is_max_heap {
34-
|a, b| b.cmp(a)
35-
} else {
33+
let comparator: fn(&T, &T) -> Ordering = if is_max_heap {
3634
|a, b| a.cmp(b)
35+
} else {
36+
|a, b| b.cmp(a)
3737
};
3838

3939
let mut idx = i;

src/string/z_algorithm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ fn match_with_z_array<T: Eq>(
103103
}
104104
}
105105

106-
if !only_full_matches {
107-
z_array
108-
} else {
106+
if only_full_matches {
109107
find_full_matches(&z_array, pattern_size)
108+
} else {
109+
z_array
110110
}
111111
}
112112

0 commit comments

Comments
 (0)