Skip to content

Commit

Permalink
basic Node struct
Browse files Browse the repository at this point in the history
  • Loading branch information
azizkayumov committed Sep 25, 2023
1 parent a81ed27 commit 92865c7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
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
9 changes: 3 additions & 6 deletions src/lib.rs
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);
}
}
58 changes: 58 additions & 0 deletions src/node.rs
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);
}
}

0 comments on commit 92865c7

Please sign in to comment.