Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions src/data_structures/b_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = Node::new(self.degree, Some(right_keys), right_children);

Expand Down
196 changes: 97 additions & 99 deletions src/data_structures/rb_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ impl<K: Ord, V> RBTree<K, V> {
}
}
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);
Expand Down Expand Up @@ -258,17 +256,17 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, 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;
Expand All @@ -278,46 +276,45 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, 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;
Expand All @@ -327,34 +324,35 @@ unsafe fn insert_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, 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;
}
Expand All @@ -377,7 +375,52 @@ unsafe fn delete_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, 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) {
/*
Expand Down Expand Up @@ -442,51 +485,6 @@ unsafe fn delete_fixup<K: Ord, V>(tree: &mut RBTree<K, V>, 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;
Expand Down
6 changes: 3 additions & 3 deletions src/graph/lee_breadth_first_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ pub fn lee(matrix: Vec<Vec<i32>>, source: (usize, usize), destination: (usize, u
}
}

if min_dist != isize::MAX {
min_dist
} else {
if min_dist == isize::MAX {
-1
} else {
min_dist
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/math/elliptic_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl<F: Field, const A: i64, const B: i64> Add for EllipticCurve<F, A, B> {
// 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);
Expand Down
6 changes: 3 additions & 3 deletions src/math/modular_exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/math/trig_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ pub fn tan<T: Into<f64> + 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
}
}

Expand All @@ -116,11 +116,11 @@ pub fn cotan<T: Into<f64> + 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
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sorting/heap_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ fn build_heap<T: Ord>(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<T: Ord>(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;
Expand Down
6 changes: 3 additions & 3 deletions src/string/z_algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ fn match_with_z_array<T: Eq>(
}
}

if !only_full_matches {
z_array
} else {
if only_full_matches {
find_full_matches(&z_array, pattern_size)
} else {
z_array
}
}

Expand Down