Skip to content

Commit

Permalink
geometry objects, traits
Browse files Browse the repository at this point in the history
  • Loading branch information
coado committed Jun 30, 2024
1 parent 60ccc1c commit c33dbd8
Show file tree
Hide file tree
Showing 15 changed files with 500 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/data_structures/fenwick_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use num::{Bounded, Zero};
use std::cmp::min;
use std::ops::{AddAssign, Sub};

#[derive(Debug, Clone, Default)]
pub struct FenwickTree<T>
where
T: Copy + AddAssign + Sub<Output = T> + Zero,
Expand Down Expand Up @@ -103,6 +104,7 @@ impl From<Vec<i8>> for FenwickTree<i8> {
}
}

#[derive(Debug, Clone, Default)]
pub struct MinFenwickTree<T>
where
T: Copy + AddAssign + Sub<Output = T> + Zero + Bounded + Ord,
Expand Down Expand Up @@ -144,6 +146,7 @@ where
}
}

#[derive(Debug, Clone, Default)]
pub struct FenwickTree2D<T>
where
T: Copy + AddAssign + Sub<Output = T> + Zero,
Expand Down
1 change: 0 additions & 1 deletion src/data_structures/min_queue.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/data_structures/min_stack.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Debug, Clone)]
pub struct Stack<T> {
pub stack: Vec<T>,
min_stack: Vec<T>,
Expand Down
2 changes: 1 addition & 1 deletion src/data_structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod fenwick_tree;
pub mod min_queue;
pub mod min_stack;
pub mod segment_tree;
pub mod sparse_table;
pub mod union_find;
1 change: 1 addition & 0 deletions src/data_structures/segment_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::cmp::{max, min};
/// O(log n) time complexity for range queries and updates.
/// The segment tree is a binary tree that stores the range of values in an array.
/// The root of the tree stores the range of values from 0 to n-1
pub struct SegmentTree<T: PartialEq + Copy> {
n: usize,
nums: Vec<T>,
Expand Down
2 changes: 2 additions & 0 deletions src/data_structures/sparse_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use num::Zero;
use std::f64;
use std::ops::AddAssign;

#[derive(Debug, Clone)]
pub enum SparseTableType {
MIN,
MAX,
SUM,
}

#[derive(Debug, Clone)]
pub struct SparseTable<T>
where
T: Ord + Zero + Copy + AddAssign,
Expand Down
124 changes: 124 additions & 0 deletions src/data_structures/union_find.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#[derive(Debug, Clone)]
pub struct UnionFind {
parent: Vec<usize>,
rank: Vec<usize>,
set_size: Vec<usize>,
num_sets: usize,
}

impl UnionFind {
pub fn new() -> Self {

Check failure on line 10 in src/data_structures/union_find.rs

View workflow job for this annotation

GitHub Actions / Clippy

you should consider adding a `Default` implementation for `UnionFind`
UnionFind {
parent: Vec::new(),
rank: Vec::new(),
set_size: Vec::new(),
num_sets: 0,
}
}

pub fn with_capacity(n: usize) -> Self {
UnionFind {
parent: vec![0; n].iter().enumerate().map(|(i, _)| i).collect(),
rank: vec![0; n],
set_size: vec![1; n],
num_sets: n,
}
}

pub fn find_set(&mut self, i: usize) -> usize {
if self.parent[i] == i {
return i;
}

self.parent[i] = self.find_set(self.parent[i]);
self.parent[i]
}

pub fn is_same_set(&mut self, i: usize, j: usize) -> bool {
self.find_set(i) == self.find_set(j)
}

pub fn union_set(&mut self, i: usize, j: usize) -> bool {
if self.is_same_set(i, j) {
return false;
}

let mut x = self.find_set(i);
let mut y = self.find_set(j);

if self.rank[x] > self.rank[y] {
std::mem::swap(&mut x, &mut y);
}
self.parent[x] = y;
self.set_size[y] += self.set_size[x];

if self.rank[x] == self.rank[y] {
self.rank[y] += 1;
}

self.num_sets -= 1;
true
}

pub fn size_of_set(&mut self, i: usize) -> usize {
let x = self.find_set(i);
self.set_size[x]
}

pub fn num_sets(&self) -> usize {
self.num_sets
}

pub fn set_size(&self) -> usize {
self.set_size.len()
}
}

#[cfg(test)]
mod tests {
use super::UnionFind;

#[test]
fn test_union_find() {
let mut uf = UnionFind::with_capacity(5);

assert_eq!(uf.num_sets(), 5);
assert_eq!(uf.size_of_set(0), 1);
assert_eq!(uf.size_of_set(1), 1);
assert_eq!(uf.size_of_set(2), 1);
assert_eq!(uf.size_of_set(3), 1);
assert_eq!(uf.size_of_set(4), 1);

assert_eq!(uf.union_set(0, 1), true);
assert_eq!(uf.num_sets(), 4);
assert_eq!(uf.size_of_set(0), 2);
assert_eq!(uf.size_of_set(1), 2);
assert_eq!(uf.size_of_set(2), 1);
assert_eq!(uf.size_of_set(3), 1);
assert_eq!(uf.size_of_set(4), 1);

assert_eq!(uf.union_set(2, 3), true);
assert_eq!(uf.num_sets(), 3);
assert_eq!(uf.size_of_set(0), 2);
assert_eq!(uf.size_of_set(1), 2);
assert_eq!(uf.size_of_set(2), 2);
assert_eq!(uf.size_of_set(3), 2);
assert_eq!(uf.size_of_set(4), 1);

assert_eq!(uf.union_set(0, 2), true);
assert_eq!(uf.num_sets(), 2);
assert_eq!(uf.size_of_set(0), 4);
assert_eq!(uf.size_of_set(1), 4);
assert_eq!(uf.size_of_set(2), 4);
assert_eq!(uf.size_of_set(3), 4);
assert_eq!(uf.size_of_set(4), 1);

assert_eq!(uf.union_set(0, 4), true);
assert_eq!(uf.num_sets(), 1);
assert_eq!(uf.size_of_set(0), 5);
assert_eq!(uf.size_of_set(1), 5);
assert_eq!(uf.size_of_set(2), 5);
assert_eq!(uf.size_of_set(3), 5);
assert_eq!(uf.size_of_set(4), 5);
}
}
1 change: 1 addition & 0 deletions src/geometry/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod objects;
Loading

0 comments on commit c33dbd8

Please sign in to comment.