Skip to content

Commit

Permalink
Format rust code (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 authored Aug 25, 2024
1 parent e63889f commit 80e09a9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ jobs:
- name: install rustc
run: |
sudo apt install rustc
- name: format
run: |
cd rust
cargo fmt -- --check
- name: compile
run: |
cd rust
Expand Down
2 changes: 1 addition & 1 deletion rust/combinatorics/permutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub fn next_permutation(p: &mut [usize]) -> Option<()> {

#[cfg(test)]
mod tests {
use rstest::rstest;
use crate::next_permutation;
use rstest::rstest;

#[rstest]
#[case(vec![0], vec![0], None)]
Expand Down
4 changes: 3 additions & 1 deletion rust/graphs/shortestpaths/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub fn dijkstra_heap(graph: &[Vec<(usize, i32)>], s: usize) -> (Vec<i32>, Vec<us
prio[s] = 0;
q.push((0, s));
while let Some((d, u)) = q.pop() {
if -d != prio[u] { continue; }
if -d != prio[u] {
continue;
}
for (v, len) in &graph[u] {
let nprio = prio[u] + len;
if prio[*v] > nprio {
Expand Down
7 changes: 2 additions & 5 deletions rust/misc/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ where

#[cfg(test)]
mod tests {
use rstest::rstest;
use crate::binary_search_first_true;
use rstest::rstest;

#[rstest]
#[case(100, 0)]
#[case(100, 1)]
#[case(100, 50)]
#[case(100, 100)]
fn basic_test(
#[case] n: i32,
#[case] pos: i32,
) {
fn basic_test(#[case] n: i32, #[case] pos: i32) {
assert_eq!(binary_search_first_true(|i| { i >= pos }, 0, n), pos);
}
}
6 changes: 4 additions & 2 deletions rust/structures/disjoint_sets.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
struct DisjointSets {
p: Vec<usize>
p: Vec<usize>,
}

impl DisjointSets {
pub fn new(n: usize) -> Self {
DisjointSets { p: (0..n).collect() }
DisjointSets {
p: (0..n).collect(),
}
}

pub fn root(&mut self, x: usize) -> usize {
Expand Down
6 changes: 4 additions & 2 deletions rust/structures/fenwick_tree.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::ops::AddAssign;

struct Fenwick<T> {
t: Vec<T>
t: Vec<T>,
}

impl<T: AddAssign + Copy + From<i32>> Fenwick<T> {
fn new(n: usize) -> Self {
Fenwick { t: vec![0.into(); n] }
Fenwick {
t: vec![0.into(); n],
}
}

pub fn add(&mut self, mut i: usize, value: T) {
Expand Down
10 changes: 8 additions & 2 deletions rust/structures/persistent_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ impl PersistentTree {
} else {
let mid = (left + right) >> 1;
if pos <= mid {
Node::new_parent(&Self::set(pos, value, root.left.as_ref().unwrap(), left, mid), root.right.as_ref().unwrap())
Node::new_parent(
&Self::set(pos, value, root.left.as_ref().unwrap(), left, mid),
root.right.as_ref().unwrap(),
)
} else {
Node::new_parent(root.left.as_ref().unwrap(), &Self::set(pos, value, root.right.as_ref().unwrap(), mid + 1, right))
Node::new_parent(
root.left.as_ref().unwrap(),
&Self::set(pos, value, root.right.as_ref().unwrap(), mid + 1, right),
)
}
};
Rc::new(node)
Expand Down

0 comments on commit 80e09a9

Please sign in to comment.