Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Rust CI

on:
push:
branches:
- '**'
pull_request:

jobs:
build-test-lint:
runs-on: ubuntu-latest

steps:
- name: Checkout do código
uses: actions/checkout@v4

- name: Instalar Rust
uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt

- name: Cache de dependências
uses: Swatinem/rust-cache@v2

- name: Check de formatação (rustfmt)
working-directory: competitive_programming
run: cargo fmt --all -- --check

- name: Lint (clippy)
working-directory: competitive_programming
run: cargo clippy --all-targets --all-features -- -D warnings

- name: Build
working-directory: competitive_programming
run: cargo build --all-targets

- name: Rodar testes
working-directory: competitive_programming
run: cargo test --all
80 changes: 38 additions & 42 deletions competitive_programming/src/data_structures/fenwick_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,59 @@
*/

pub trait Constants {

fn initial() -> Self; // the initial constant
fn initial() -> Self; // the initial constant
}

#[derive(Clone)]
pub struct FenwickTree<T, OP> {
data: Vec<T>, // the fenwick tree data
length: usize, // the number of elements of the fenwick tree
op: OP // the binary operator to apply an operation in the fenwick tree
data: Vec<T>, // the fenwick tree data
length: usize, // the number of elements of the fenwick tree
op: OP, // the binary operator to apply an operation in the fenwick tree
}

impl<T: Constants + Copy, OP: Fn(T, T) -> T> FenwickTree<T, OP> {

/**
* create a new instance of FenwickTree
* @param length the number of elements of the Fenwick Tree
* @param op the binary function that handles with operations
* @return the new instance of FenwickTree
*/
pub fn new(length: usize, op: OP) -> Self {

Self {
data: vec![T::initial(); length + 1],
length,
op
}
}

/**
* find the result of an operation of first k elements
* @param k the number of the first elements for which we want to find the result
*/
pub fn query(&self, mut k: i32) -> T {
let mut result = T::initial();

assert!(k <= self.length as i32);
pub fn new(length: usize, op: OP) -> Self {
Self {
data: vec![T::initial(); length + 1],
length,
op,
}
}

while k > 0 {
result = (self.op)(result, self.data[k as usize]);
k -= k & -k;
}
/**
* find the result of an operation of first k elements
* @param k the number of the first elements for which we want to find the result
*/
pub fn query(&self, mut k: i32) -> T {
let mut result = T::initial();

result
}
assert!(k <= self.length as i32);

/**
* update a value to the element at position k
* @param k the position for which we want to modify
* @param value the value for which we want to apply
*/
pub fn update(&mut self, mut k: i32, value: T) {
while k > 0 {
result = (self.op)(result, self.data[k as usize]);
k -= k & -k;
}

assert!(k > 0);
result
}

while k <= self.length as i32 {
self.data[k as usize] = (self.op)(self.data[k as usize], value);
k += k & -k;
}
}
}
/**
* update a value to the element at position k
* @param k the position for which we want to modify
* @param value the value for which we want to apply
*/
pub fn update(&mut self, mut k: i32, value: T) {
assert!(k > 0);

while k <= self.length as i32 {
self.data[k as usize] = (self.op)(self.data[k as usize], value);
k += k & -k;
}
}
}
8 changes: 5 additions & 3 deletions competitive_programming/src/data_structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod segment_tree;
pub mod fenwick_tree;
pub mod union_find;
pub mod recursive_segment_tree;
pub mod recursive_segment_tree;
pub mod segment_tree;
mod union_find;

pub use union_find::DisjointSet;
Loading
Loading