From dfb37e4a02ec2bb6f5cce8a1687e5619ee56c5f5 Mon Sep 17 00:00:00 2001 From: Andrei Navumenka Date: Sun, 1 Sep 2024 17:55:22 -0400 Subject: [PATCH] Fix rust code --- .github/workflows/rust.yml | 6 +++--- rust/misc/binary_search.rs | 9 +++++---- rust/structures/mergeable_heap.rs | 14 +++++++------- rust/structures/mod.rs | 1 + 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index aeb07ef3..b8e3a0d0 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -7,16 +7,16 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: install rustc + - name: print rustc version run: | - sudo apt install rustc + rustc --version - name: format run: | cd rust cargo fmt -- --check RUSTFLAGS="-A unused" cargo clippy cargo clippy --tests - - name: compile + - name: compile and test run: | cd rust cargo test diff --git a/rust/misc/binary_search.rs b/rust/misc/binary_search.rs index 6b26c2d3..a673f32d 100644 --- a/rust/misc/binary_search.rs +++ b/rust/misc/binary_search.rs @@ -1,8 +1,9 @@ // invariant: f[lo] == false, f[hi] == true -pub fn binary_search_first_true(f: F, from_inclusive: i32, to_inclusive: i32) -> i32 -where - F: Fn(i32) -> bool, -{ +pub fn binary_search_first_true bool>( + f: F, + from_inclusive: i32, + to_inclusive: i32, +) -> i32 { let mut lo = from_inclusive - 1; let mut hi = to_inclusive + 1; while hi - lo > 1 { diff --git a/rust/structures/mergeable_heap.rs b/rust/structures/mergeable_heap.rs index bbea4aa6..576a1d81 100644 --- a/rust/structures/mergeable_heap.rs +++ b/rust/structures/mergeable_heap.rs @@ -15,7 +15,7 @@ impl Heap { })) } - fn merge(a: Option>>, b: Option>>) -> Option>> { + pub fn merge(a: Option>>, b: Option>>) -> Option>> { if a.is_none() { return b; } @@ -34,13 +34,13 @@ impl Heap { Some(ha) } - fn remove_min(heap: Option>>) -> (Option>>, V) { - let h = heap.unwrap(); - (Self::merge(h.left, h.right), h.value) + pub fn insert(heap: Option>>, value: V) -> Option>> { + Self::merge(heap, Heap::new(value)) } - fn add(heap: Option>>, value: V) -> Option>> { - Self::merge(heap, Heap::new(value)) + pub fn remove_min(heap: Option>>) -> (Option>>, V) { + let h = heap.unwrap(); + (Self::merge(h.left, h.right), h.value) } } @@ -92,7 +92,7 @@ mod tests { } fn sort_with_heap(seq: &[u32]) -> Vec { - let mut heap = seq.iter().fold(None, |h, v| Heap::add(h, v)); + let mut heap = seq.iter().fold(None, |h, v| Heap::insert(h, v)); let mut heap_sorted_values = Vec::new(); while heap.is_some() { let (updated_heap, min_value) = Heap::remove_min(heap); diff --git a/rust/structures/mod.rs b/rust/structures/mod.rs index bdb69099..44045524 100644 --- a/rust/structures/mod.rs +++ b/rust/structures/mod.rs @@ -2,3 +2,4 @@ pub mod disjoint_sets; pub mod fenwick_tree; pub mod mergeable_heap; pub mod persistent_tree; +pub mod treap;