Skip to content

Commit

Permalink
Fix rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Sep 1, 2024
1 parent 200c0ca commit c9d262e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- name: format
rustc --version
- name: check code formatting
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
9 changes: 5 additions & 4 deletions rust/misc/binary_search.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// invariant: f[lo] == false, f[hi] == true
pub fn binary_search_first_true<F>(f: F, from_inclusive: i32, to_inclusive: i32) -> i32
where
F: Fn(i32) -> bool,
{
pub fn binary_search_first_true<F: Fn(i32) -> 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 {
Expand Down
14 changes: 7 additions & 7 deletions rust/structures/mergeable_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<V: PartialOrd> Heap<V> {
}))
}

fn merge(a: Option<Box<Heap<V>>>, b: Option<Box<Heap<V>>>) -> Option<Box<Heap<V>>> {
pub fn merge(a: Option<Box<Heap<V>>>, b: Option<Box<Heap<V>>>) -> Option<Box<Heap<V>>> {
if a.is_none() {
return b;
}
Expand All @@ -34,13 +34,13 @@ impl<V: PartialOrd> Heap<V> {
Some(ha)
}

fn remove_min(heap: Option<Box<Heap<V>>>) -> (Option<Box<Heap<V>>>, V) {
let h = heap.unwrap();
(Self::merge(h.left, h.right), h.value)
pub fn insert(heap: Option<Box<Heap<V>>>, value: V) -> Option<Box<Heap<V>>> {
Self::merge(heap, Heap::new(value))
}

fn add(heap: Option<Box<Heap<V>>>, value: V) -> Option<Box<Heap<V>>> {
Self::merge(heap, Heap::new(value))
pub fn remove_min(heap: Option<Box<Heap<V>>>) -> (Option<Box<Heap<V>>>, V) {
let h = heap.unwrap();
(Self::merge(h.left, h.right), h.value)
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ mod tests {
}

fn sort_with_heap(seq: &[u32]) -> Vec<u32> {
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);
Expand Down
1 change: 1 addition & 0 deletions rust/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod disjoint_sets;
pub mod fenwick_tree;
pub mod mergeable_heap;
pub mod persistent_tree;
pub mod treap;

0 comments on commit c9d262e

Please sign in to comment.