-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a81ed27
commit 92865c7
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Rust | ||
|
||
# on: [push, pull_request] | ||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
check: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Format | ||
run: cargo fmt --check | ||
- name: Clippy | ||
run: cargo clippy -- -D warnings -W clippy::pedantic | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Build | ||
run: cargo build --verbose | ||
- name: Run tests | ||
run: cargo test --verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
#![allow(dead_code, clippy::module_name_repetitions)] // yes, I want to name my structs with the same name as the file | ||
mod node; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
let result = 2 + 2; | ||
assert_eq!(result, 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
pub enum Parent { | ||
Node(usize), // parent node in the tree | ||
Path(usize), // path to the root in the forest | ||
Root, // root of the tree | ||
} | ||
|
||
pub struct Path { | ||
pub max_weight_idx: usize, | ||
pub max_weight: f64, | ||
} | ||
|
||
impl Path { | ||
pub fn new(max_weight_idx: usize, max_weight: f64) -> Self { | ||
Path { | ||
max_weight_idx, | ||
max_weight, | ||
} | ||
} | ||
|
||
pub fn update(&mut self, idx: usize, weight: f64) { | ||
if weight > self.max_weight { | ||
self.max_weight = weight; | ||
self.max_weight_idx = idx; | ||
} | ||
} | ||
} | ||
|
||
pub struct Node { | ||
pub left: Option<usize>, | ||
pub right: Option<usize>, | ||
pub parent: Parent, | ||
pub flipped: bool, | ||
// path aggregates | ||
pub weight: f64, | ||
pub path: Path, | ||
} | ||
|
||
impl Node { | ||
pub fn new(idx: usize, weight: f64) -> Self { | ||
Node { | ||
left: None, | ||
right: None, | ||
parent: Parent::Root, | ||
flipped: false, | ||
weight: 0.0, | ||
path: Path::new(idx, weight), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
let result = 2 + 2; | ||
assert_eq!(result, 4); | ||
} | ||
} |