-
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
68c2534
commit 1ba85ab
Showing
4 changed files
with
71 additions
and
14 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,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() | ||
} |
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,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; |
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,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.) | ||
} | ||
} |
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,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, | ||
} | ||
} | ||
} |