From 44cf4c5bd1f6be6ddd60babb7e29af9cb159c813 Mon Sep 17 00:00:00 2001 From: Dawid Date: Sat, 18 May 2024 12:13:40 +0200 Subject: [PATCH] gh workflows --- .github/workflows/actions.yml | 64 +++++++++++++++++++++++++++++ src/data_structures/fenwick_tree.rs | 5 +-- src/data_structures/min_queue.rs | 2 +- src/data_structures/min_stack.rs | 9 ++++ src/data_structures/sparse_table.rs | 4 +- src/math/binary_exponentiation.rs | 5 +-- src/utils.rs | 10 ++--- 7 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/actions.yml diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml new file mode 100644 index 0000000..2875c7b --- /dev/null +++ b/.github/workflows/actions.yml @@ -0,0 +1,64 @@ +on: [push, pull_request] + +name: Continuous integration + +jobs: + check: + name: Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - uses: actions-rs/cargo@v1 + with: + command: check + + test: + name: Test Suite + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - uses: actions-rs/cargo@v1 + with: + command: test + + fmt: + name: Rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - run: rustup component add rustfmt + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + + clippy: + name: Clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - run: rustup component add clippy + - uses: actions-rs/cargo@v1 + with: + command: clippy + args: -- -D warnings diff --git a/src/data_structures/fenwick_tree.rs b/src/data_structures/fenwick_tree.rs index 03c110e..5935867 100644 --- a/src/data_structures/fenwick_tree.rs +++ b/src/data_structures/fenwick_tree.rs @@ -43,8 +43,8 @@ impl From> for FenwickTree { fn from(nums: Vec) -> Self { let mut fenwick_tree = FenwickTree::new(nums.len()); - for i in 0..nums.len() { - fenwick_tree.tree[i] += nums[i]; + for (i, val) in nums.iter().enumerate() { + fenwick_tree.tree[i] += val; let r = i | (i + 1); fenwick_tree.tree[r] += fenwick_tree.tree[i]; } @@ -143,7 +143,6 @@ mod test { use std::cmp::min; use super::{FenwickTree, FenwickTree2D, MinFenwickTree}; - use crate::utils::print_2d_vector; use rand::prelude::*; #[test] diff --git a/src/data_structures/min_queue.rs b/src/data_structures/min_queue.rs index 5061c59..d754b25 100644 --- a/src/data_structures/min_queue.rs +++ b/src/data_structures/min_queue.rs @@ -1 +1 @@ -use std::collections::VecDeque; +// use std::collections::VecDeque; diff --git a/src/data_structures/min_stack.rs b/src/data_structures/min_stack.rs index 0cc5554..12244aa 100644 --- a/src/data_structures/min_stack.rs +++ b/src/data_structures/min_stack.rs @@ -35,6 +35,15 @@ where } } +impl Default for Stack +where + T: Ord + Copy, +{ + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod test { use super::Stack; diff --git a/src/data_structures/sparse_table.rs b/src/data_structures/sparse_table.rs index ba43f52..3163dc4 100644 --- a/src/data_structures/sparse_table.rs +++ b/src/data_structures/sparse_table.rs @@ -18,9 +18,7 @@ impl SparseTable { let k = f64::log2(nums.len() as f64) as usize; let mut st = vec![vec![0; nums.len()]; k + 1]; - for i in 0..nums.len() { - st[0][i] = nums[i]; - } + st[0][..nums.len()].copy_from_slice(&nums[..]); for i in 1..=k { let mut j = 0; diff --git a/src/math/binary_exponentiation.rs b/src/math/binary_exponentiation.rs index e6c1d7e..9904c44 100644 --- a/src/math/binary_exponentiation.rs +++ b/src/math/binary_exponentiation.rs @@ -1,4 +1,3 @@ - pub fn bipow_rec(a: i64, b: i64) -> i64 { if b == 0 { return 1; @@ -6,9 +5,9 @@ pub fn bipow_rec(a: i64, b: i64) -> i64 { let res = bipow_rec(a, b / 2); if b % 2 == 1 { - return res * res * 2; + res * res * a } else { - return res * res; + res * res } } diff --git a/src/utils.rs b/src/utils.rs index 6b496e8..d77a3ab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,16 +1,16 @@ use std::fmt::Display; pub fn print_2d_vector(vector: &Vec>) { - for i in 0..vector.len() { - for j in 0..vector[i].len() { - print!("{}, ", vector[i][j]); + for row in vector { + for val in row { + print!("{}, ", val); } println!(); } } pub fn print_vector(vector: &Vec) { - for i in 0..vector.len() { - print!("{}, ", vector[i]); + for val in vector { + print!("{}, ", val); } }