diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 27f5eaa6..aeb07ef3 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -14,6 +14,8 @@ jobs: run: | cd rust cargo fmt -- --check + RUSTFLAGS="-A unused" cargo clippy + cargo clippy --tests - name: compile run: | cd rust diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 27937258..13353021 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -5,40 +5,7 @@ version = "0.1.0" edition = "2021" [dev-dependencies] -rstest="0.21.0" +rstest="0.22.0" -[[bin]] -name = "permutations" -path = "combinatorics/permutations.rs" - -[[bin]] -name = "topological_sort" -path = "graphs/dfs/topological_sort.rs" - -[[bin]] -name = "dijkstra" -path = "graphs/shortestpaths/dijkstra.rs" - -[[bin]] -name = "max_bipartite_matching_EV" -path = "graphs/matchings/max_bipartite_matching_EV.rs" - -[[bin]] -name = "euclid" -path = "numbertheory/euclid.rs" - -[[bin]] -name = "disjoint_sets" -path = "structures/disjoint_sets.rs" - -[[bin]] -name = "fenwick_tree" -path = "structures/fenwick_tree.rs" - -[[bin]] -name = "persistent_tree" -path = "structures/persistent_tree.rs" - -[[bin]] -name = "binary_search" -path = "misc/binary_search.rs" +[lib] +path = "lib.rs" diff --git a/rust/combinatorics/mod.rs b/rust/combinatorics/mod.rs new file mode 100644 index 00000000..5806ec2c --- /dev/null +++ b/rust/combinatorics/mod.rs @@ -0,0 +1 @@ +pub mod permutations; diff --git a/rust/combinatorics/permutations.rs b/rust/combinatorics/permutations.rs index 79018b67..523ed8b4 100644 --- a/rust/combinatorics/permutations.rs +++ b/rust/combinatorics/permutations.rs @@ -8,7 +8,7 @@ pub fn next_permutation(p: &mut [usize]) -> Option<()> { #[cfg(test)] mod tests { - use crate::next_permutation; + use crate::combinatorics::permutations::next_permutation; use rstest::rstest; #[rstest] diff --git a/rust/graphs/dfs/mod.rs b/rust/graphs/dfs/mod.rs new file mode 100644 index 00000000..c6115a9b --- /dev/null +++ b/rust/graphs/dfs/mod.rs @@ -0,0 +1 @@ +pub mod topological_sort; diff --git a/rust/graphs/dfs/topological_sort.rs b/rust/graphs/dfs/topological_sort.rs index 3ba603d0..e92b0d88 100644 --- a/rust/graphs/dfs/topological_sort.rs +++ b/rust/graphs/dfs/topological_sort.rs @@ -18,12 +18,12 @@ pub fn topological_sort(graph: &[Vec]) -> Vec { } } order.reverse(); - return order; + order } #[cfg(test)] mod tests { - use crate::topological_sort; + use crate::graphs::dfs::topological_sort::topological_sort; #[test] fn basic_test() { diff --git a/rust/graphs/matchings/max_bipartite_matching_EV.rs b/rust/graphs/matchings/max_bipartite_matching_ev.rs similarity index 90% rename from rust/graphs/matchings/max_bipartite_matching_EV.rs rename to rust/graphs/matchings/max_bipartite_matching_ev.rs index c6fa6852..377616d8 100644 --- a/rust/graphs/matchings/max_bipartite_matching_EV.rs +++ b/rust/graphs/matchings/max_bipartite_matching_ev.rs @@ -1,6 +1,3 @@ -#![feature(test)] -extern crate test; - fn find_path(graph: &[Vec], u1: usize, matching: &mut [usize], vis: &mut [bool]) -> bool { vis[u1] = true; for v in &graph[u1] { @@ -10,7 +7,7 @@ fn find_path(graph: &[Vec], u1: usize, matching: &mut [usize], vis: &mut return true; } } - return false; + false } pub fn max_matching(graph: &[Vec], n2: usize) -> (usize, Vec) { @@ -23,12 +20,12 @@ pub fn max_matching(graph: &[Vec], n2: usize) -> (usize, Vec) { matches += 1; } } - return (matches, matching); + (matches, matching) } #[cfg(test)] mod tests { - use crate::max_matching; + use crate::graphs::matchings::max_bipartite_matching_ev::max_matching; use test::Bencher; #[test] diff --git a/rust/graphs/matchings/mod.rs b/rust/graphs/matchings/mod.rs new file mode 100644 index 00000000..290d4a73 --- /dev/null +++ b/rust/graphs/matchings/mod.rs @@ -0,0 +1 @@ +pub mod max_bipartite_matching_ev; diff --git a/rust/graphs/mod.rs b/rust/graphs/mod.rs new file mode 100644 index 00000000..a3f5e383 --- /dev/null +++ b/rust/graphs/mod.rs @@ -0,0 +1,3 @@ +pub mod dfs; +pub mod matchings; +pub mod shortestpaths; diff --git a/rust/graphs/shortestpaths/dijkstra.rs b/rust/graphs/shortestpaths/dijkstra.rs index 5bcc4b35..fa00f99f 100644 --- a/rust/graphs/shortestpaths/dijkstra.rs +++ b/rust/graphs/shortestpaths/dijkstra.rs @@ -22,12 +22,12 @@ pub fn dijkstra_heap(graph: &[Vec<(usize, i32)>], s: usize) -> (Vec, Vec i32 { - return if b == 0 { a.abs() } else { gcd(b, a % b) }; + if b == 0 { + a.abs() + } else { + gcd(b, a % b) + } } // returns { gcd(a,b), x, y } such that gcd(a,b) = a*x + b*y @@ -21,12 +25,16 @@ fn euclid(mut a: i64, mut b: i64) -> [i64; 3] { y = _y1; a = _b; } - return if a > 0 { [a, x, y] } else { [-a, -x, -y] }; + if a > 0 { + [a, x, y] + } else { + [-a, -x, -y] + } } #[cfg(test)] mod tests { - use crate::{euclid, gcd}; + use crate::numbertheory::euclid::{euclid, gcd}; #[test] fn basic_test() { diff --git a/rust/numbertheory/mod.rs b/rust/numbertheory/mod.rs new file mode 100644 index 00000000..2544748d --- /dev/null +++ b/rust/numbertheory/mod.rs @@ -0,0 +1 @@ +pub mod euclid; diff --git a/rust/structures/disjoint_sets.rs b/rust/structures/disjoint_sets.rs index 0bdcfa03..b40ded9d 100644 --- a/rust/structures/disjoint_sets.rs +++ b/rust/structures/disjoint_sets.rs @@ -25,7 +25,7 @@ impl DisjointSets { #[cfg(test)] mod tests { - use crate::DisjointSets; + use crate::structures::disjoint_sets::DisjointSets; #[test] fn basic_test() { diff --git a/rust/structures/fenwick_tree.rs b/rust/structures/fenwick_tree.rs index 883e8b04..f0971801 100644 --- a/rust/structures/fenwick_tree.rs +++ b/rust/structures/fenwick_tree.rs @@ -25,13 +25,13 @@ impl> Fenwick { res += self.t[i as usize]; i = (i & (i + 1)) - 1; } - return res; + res } } #[cfg(test)] mod tests { - use crate::Fenwick; + use crate::structures::fenwick_tree::Fenwick; #[test] fn basic_test() { diff --git a/rust/structures/mod.rs b/rust/structures/mod.rs new file mode 100644 index 00000000..da7b429a --- /dev/null +++ b/rust/structures/mod.rs @@ -0,0 +1,3 @@ +pub mod disjoint_sets; +pub mod fenwick_tree; +pub mod persistent_tree; diff --git a/rust/structures/persistent_tree.rs b/rust/structures/persistent_tree.rs index 5a550e7c..d70818fb 100644 --- a/rust/structures/persistent_tree.rs +++ b/rust/structures/persistent_tree.rs @@ -71,7 +71,7 @@ impl PersistentTree { #[cfg(test)] mod tests { - use crate::PersistentTree; + use crate::structures::persistent_tree::PersistentTree; #[test] fn basic_test() {