Skip to content

Commit

Permalink
added SSTree
Browse files Browse the repository at this point in the history
  • Loading branch information
azizkayumov committed Dec 25, 2023
1 parent 5f38c00 commit bb7f3c4
Show file tree
Hide file tree
Showing 11 changed files with 1,297 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
conv = "0.3.3"
ordered-float = "4.1.1"

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# sstree
# rindex
Rindex: reverse nearest neighbor search index for high-dimensional clustered datasets.
1 change: 1 addition & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub trait Index<const D: usize> {

fn core_distance_of(&self, point_index: usize) -> f64;
fn neighbors_of(&self, point_index: usize) -> Vec<usize>;
fn num_points(&self) -> usize;
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod distance;
mod index;
#[allow(clippy::module_name_repetitions)]
mod linear;
mod tree;

pub use index::Index;
pub use linear::LinearIndex;
pub use tree::sstree::SSTree;
9 changes: 4 additions & 5 deletions src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ impl<const D: usize> LinearIndex<D> {
false
}
}

#[must_use]
pub fn num_points(&self) -> usize {
self.data.len()
}
}

impl<const D: usize> Index<D> for LinearIndex<D> {
Expand Down Expand Up @@ -86,6 +81,10 @@ impl<const D: usize> Index<D> for LinearIndex<D> {
.map(|(_, neighbor_index)| *neighbor_index)
.collect()
}

fn num_points(&self) -> usize {
self.data.len()
}
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod node;
mod sphere;
pub mod sstree;
35 changes: 35 additions & 0 deletions src/tree/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use super::sphere::Sphere;

pub struct InsertionEntry<const D: usize> {
pub idx: usize,
pub sphere: Sphere<D>,
pub parent_height: usize,
}

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
}
}
17 changes: 17 additions & 0 deletions src/tree/sphere.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::distance::euclidean;

#[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.)
}
}
Loading

0 comments on commit bb7f3c4

Please sign in to comment.