Skip to content

Commit

Permalink
basic structure
Browse files Browse the repository at this point in the history
  • Loading branch information
azizkayumov committed Dec 22, 2023
1 parent 68c2534 commit 1ba85ab
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/distance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub fn euclidean<const D: usize>(a: &[f64; D], b: &[f64; D]) -> f64 {
let mut sum = 0.0;
for (x, y) in a.iter().zip(b.iter()) {
sum += (x - y).powi(2);
}
sum.sqrt()
}
18 changes: 4 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

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

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
mod distance;
mod node;
mod sstree;
pub use sstree::SSTree;
45 changes: 45 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::distance::euclidean;

pub struct Node<const D: usize> {
pub idx: usize,
pub height: usize,
pub sphere: Sphere<D>,
pub parent: usize,
pub children: Vec<usize>,
pub variance: [f64; D],
pub bound: f64,
}

impl<const D: usize> Node<D> {
pub fn new(idx: usize, height: usize, sphere: Sphere<D>, parent: usize) -> Node<D> {
Node {
idx,
height,
sphere,
parent,
children: Vec::new(),
variance: [f64::INFINITY; D],
bound: f64::INFINITY,
}
}

pub fn is_leaf(&self) -> bool {
self.height == 0
}
}

#[derive(Clone, Copy)]
pub struct Sphere<const D: usize> {
pub center: [f64; D],
pub radius: f64,
}

impl<const D: usize> Sphere<D> {
pub fn new(center: [f64; D], radius: f64) -> Sphere<D> {
Sphere { center, radius }
}

pub fn min_distance(&self, other: &[f64; D]) -> f64 {
(euclidean(&self.center, other) - self.radius).max(0.)
}
}
15 changes: 15 additions & 0 deletions src/sstree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub struct SSTree<const D: usize> {
k: usize,
branching_factor: usize,
root: usize,
}

impl<const D: usize> SSTree<D> {
pub fn new(k: usize) -> Self {
SSTree {
k,
branching_factor: 2 * k + 1,
root: usize::MAX,
}
}
}

0 comments on commit 1ba85ab

Please sign in to comment.