Skip to content

Commit

Permalink
develop (#19)
Browse files Browse the repository at this point in the history
* closes #17

* closes #18

* fix default params
  • Loading branch information
azizkayumov committed Sep 9, 2024
1 parent 6199824 commit 9d97c80
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bench/benches/uniform/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn benchmark(criterion: &mut Criterion) {

pub fn build_rindex() -> (Rindex<D>, Vec<(usize, [f64; D])>) {
let mut rng = StdRng::seed_from_u64(0);
let mut index = Rindex::new(10, K).expect("Failed to create Rindex");
let mut index = Rindex::new(K);
let mut points = Vec::new();
for _ in 0..NUM_OPERATIONS {
let should_delete = rng.gen_bool(DELETION_PROB);
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
}

// Configure the tree
let mut tree = Rindex::new(5, 5).expect("Invalid fanout");
let mut tree = Rindex::new(5);
let mut point_ids = Vec::new();
let deletion_probability = 0.25; // 25%

Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "rindex"
version = "0.2.1"
edition = "2021"
rust-version = "1.63"
description = "Rindex: reverse nearest neighbor search index for high-dimensional clustered datasets."
description = "Rindex: dynamic spatial index for efficiently maintaining *k* nearest neighbors graph of multi-dimensional clustered datasets."
readme = "README.md"
documentation = "https://docs.rs/rindex"
homepage = "https://github.com/azizkayumov/rindex"
Expand Down
6 changes: 2 additions & 4 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ The following example shows how to maintain *k* nearest neighbors using Rindex:
use rindex::Rindex;

fn main() {
let fanout = 10;
let k = 3; // maintain 3 nearest neighbors for each point
let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
let mut rindex = Rindex::new(k);

// Insert some points
let a = rindex.insert([1.0, 1.0]);
Expand Down Expand Up @@ -62,9 +61,8 @@ The traditional query operations are supported in addition to the reverse neares
use rindex::Rindex;

fn main() {
let fanout = 10;
let k = 3;
let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
let mut rindex = Rindex::new(k);
let a = rindex.insert([1.0, 1.0]);
let b = rindex.insert([2.0, 2.0]);
let c = rindex.insert([3.0, 3.0]);
Expand Down
51 changes: 31 additions & 20 deletions lib/src/rindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use std::{collections::BinaryHeap, vec};
use crate::{distance::euclidean, index::Index, node::Node, sphere::Sphere};
use ordered_float::OrderedFloat;

/// Rindex: dynamic spatial index for efficiently maintaining *k* nearest neighbors graph of multi-dimensional clustered datasets.
///
/// # Examples
///
/// ```
/// use rindex::Rindex;
///
/// let fanout = 10;
/// let k = 3;
/// let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
/// let mut rindex = Rindex::new(k);
///
/// // Insert some points
/// let a = rindex.insert([1.0, 1.0]);
Expand Down Expand Up @@ -43,21 +44,21 @@ pub struct Rindex<const D: usize> {

impl<const D: usize> Default for Rindex<D> {
fn default() -> Self {
Rindex::new(10, 10).expect("Invalid fanout")
Rindex::new(10)
}
}

// Public methods
impl<const D: usize> Rindex<D> {
#[must_use]
pub fn new(fanout: usize, k: usize) -> Option<Self> {
if fanout < 4 || k < 1 {
pub fn new_with_params(max_fanout: usize, k: usize) -> Option<Self> {
if max_fanout < 4 || k < 1 {
return None;
}
Some(Rindex {
min_fanout: fanout / 2,
max_fanout: fanout,
reinsert_fanout: fanout / 3,
min_fanout: max_fanout / 2,
max_fanout,
reinsert_fanout: max_fanout / 3,
reinsert_height: 1,
k,
root: usize::MAX,
Expand All @@ -66,6 +67,20 @@ impl<const D: usize> Rindex<D> {
})
}

#[must_use]
pub fn new(k: usize) -> Self {
Rindex {
min_fanout: 5,
max_fanout: 10,
reinsert_fanout: 3,
reinsert_height: 1,
k,
root: usize::MAX,
index: Index::new(),
num_points: 0,
}
}

/// # Examples
/// ```
/// use rindex::Rindex;
Expand Down Expand Up @@ -202,9 +217,8 @@ impl<const D: usize> Rindex<D> {
/// ```
/// use rindex::Rindex;
///
/// let fanout = 10;
/// let k = 3;
/// let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
/// let mut rindex = Rindex::new(k);
/// let a = rindex.insert([1.0, 1.0]);
/// let b = rindex.insert([2.0, 2.0]);
/// let c = rindex.insert([3.0, 3.0]);
Expand Down Expand Up @@ -242,9 +256,8 @@ impl<const D: usize> Rindex<D> {
/// ```
/// use rindex::Rindex;
///
/// let fanout = 10;
/// let k = 3;
/// let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
/// let mut rindex = Rindex::new(k);
///
/// // Insert some points
/// let a = rindex.insert([1.0, 1.0]);
Expand Down Expand Up @@ -278,9 +291,8 @@ impl<const D: usize> Rindex<D> {
/// ```
/// use rindex::Rindex;
///
/// let fanout = 10;
/// let k = 3;
/// let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
/// let mut rindex = Rindex::new(k);
///
/// // Insert some points
/// let a = rindex.insert([1.0, 1.0]);
Expand All @@ -306,7 +318,7 @@ impl<const D: usize> Rindex<D> {
///
/// let fanout = 4;
/// let k = 3;
/// let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
/// let mut rindex = Rindex::new_with_params(fanout, k).expect("Failed to create Rindex");
///
/// // Insert some points
/// let a = rindex.insert([1.0, 1.0]);
Expand Down Expand Up @@ -862,7 +874,7 @@ mod tests {
fn split() {
let fanout = 8;
let k = 10;
let mut rindex = Rindex::new(fanout, k).expect("Invalid fanout");
let mut rindex = Rindex::new_with_params(fanout, k).expect("Invalid fanout");

// Create 9 point nodes, as the fanout of 8 will trigger a split
let node_a = rindex.add_slot(Node::point([0.0, 0.0]));
Expand Down Expand Up @@ -910,7 +922,7 @@ mod tests {
fn update() {
let fanout = 8;
let k = 10;
let mut rindex = Rindex::new(fanout, k).expect("Invalid fanout");
let mut rindex = Rindex::new_with_params(fanout, k).expect("Invalid fanout");

// The tree should be empty
assert_eq!(rindex.height(), 0);
Expand Down Expand Up @@ -982,7 +994,7 @@ mod tests {
fn knn_distances() {
let fanout = 5;
let k = 5;
let mut rindex = Rindex::new(fanout, k).expect("Invalid fanout");
let mut rindex = Rindex::new_with_params(fanout, k).expect("Invalid fanout");

// Insert some points
let a = rindex.insert([0.0, 1.0]);
Expand Down Expand Up @@ -1058,9 +1070,8 @@ mod tests {

#[test]
fn reverse_query() {
let fanout = 5;
let k = 5;
let mut rindex = Rindex::new(fanout, k).expect("Invalid fanout");
let mut rindex = Rindex::new(k);

for i in 0..100 {
let _ = rindex.insert([i as f64, i as f64]);
Expand Down
3 changes: 1 addition & 2 deletions lib/tests/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use std::collections::{BinaryHeap, HashMap};
fn test_knn_graph() {
let num_ops = 1000;
let deletion_probability = 0.2;
let fanout = 10;
let k = 5;

// Configure the random number generator and the points
let mut rng = StdRng::seed_from_u64(0);
let mut points = Vec::new();

// Create the rindex
let mut rindex = Rindex::new(fanout, k).expect("Invalid fanout");
let mut rindex = Rindex::new(k);

// Create the brute force neighbors
let mut bruteforce = BruteForceNeighbors::new(k);
Expand Down
1 change: 1 addition & 0 deletions lib/tests/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rindex::Rindex;

#[test]
fn test_random() {
// Create a new Rindex
let mut rindex = Rindex::default();

// We will perform some random insertions and deletions
Expand Down
8 changes: 4 additions & 4 deletions lib/tests/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use rindex::Rindex;
#[allow(unused_variables)]
#[test]
fn test_main_usage() {
let fanout = 10;
let k = 3; // maintain 3 nearest neighbors for each point
let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
let mut rindex = Rindex::new(k);

// Insert some points
let a = rindex.insert([1.0, 1.0]);
Expand Down Expand Up @@ -38,9 +37,10 @@ fn test_update_operations() {
#[allow(unused_variables)]
#[test]
fn test_query_operations() {
let fanout = 10;
let k = 3;
let mut rindex = Rindex::new(fanout, k).expect("Failed to create Rindex");
let mut rindex = Rindex::new(k);

// Insert some points
let a = rindex.insert([1.0, 1.0]);
let b = rindex.insert([2.0, 2.0]);
let c = rindex.insert([3.0, 3.0]);
Expand Down

0 comments on commit 9d97c80

Please sign in to comment.