mirror of
https://github.com/andreytkachenko/kdtree-rust.git
synced 2024-11-22 01:16:25 +04:00
Removed duplication, insert_node now checks for duplicates
This commit is contained in:
parent
885ed1d785
commit
a6c19441b8
47
src/bench.rs
47
src/bench.rs
@ -5,48 +5,7 @@ extern crate rand;
|
||||
use bencher::Bencher;
|
||||
use rand::Rng;
|
||||
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point2WithId {
|
||||
dims: [f64; 2],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point2WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64) -> Point2WithId {
|
||||
Point2WithId {
|
||||
dims: [x, y],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl kdtree::kdtree::KdtreePointTrait for Point2WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point3WithId {
|
||||
dims: [f64; 3],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point3WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
|
||||
Point3WithId {
|
||||
dims: [x, y, z],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl kdtree::kdtree::KdtreePointTrait for Point3WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
use kdtree::kdtree::test_common::*;
|
||||
|
||||
fn gen_random() -> f64 {
|
||||
rand::thread_rng().gen_range(0., 10000.)
|
||||
@ -90,15 +49,13 @@ fn bench_creating_1000_000_node_tree(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_adding_to_1000_tree(b: &mut Bencher) {
|
||||
fn bench_adding_same_node_to_1000_tree(b: &mut Bencher) {
|
||||
let len = 1000usize;
|
||||
let mut points = generate_points(len);
|
||||
let mut tree = kdtree::kdtree::Kdtree::new(&mut points);
|
||||
|
||||
let point = Point3WithId::new(-1 as i32, gen_random(),gen_random(),gen_random());
|
||||
println!("before ..");
|
||||
b.iter(|| {
|
||||
println!("in lam ..");
|
||||
tree.insert_node(point);
|
||||
});
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ impl Bounds {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ::kdtree::test_common::tests_utils::*;
|
||||
use ::kdtree::test_common::*;
|
||||
|
||||
#[test]
|
||||
fn bounds_test() {
|
||||
|
@ -1,6 +1,4 @@
|
||||
#[cfg(test)]
|
||||
pub mod test_common;
|
||||
|
||||
pub mod distance;
|
||||
|
||||
mod partition;
|
||||
@ -184,7 +182,7 @@ impl<T: KdtreePointTrait> KdtreeNode<T> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ::kdtree::test_common::tests_utils::Point2WithId;
|
||||
use ::kdtree::test_common::Point2WithId;
|
||||
|
||||
use super::*;
|
||||
|
||||
@ -278,6 +276,19 @@ mod tests {
|
||||
assert_eq!(tree.nodes[0].right_node.is_some(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_add_filters_duplicates() {
|
||||
let mut vec = vec![Point2WithId::new(0,0.,0.)];
|
||||
|
||||
let mut tree = Kdtree::new(&mut vec);
|
||||
|
||||
let node = Point2WithId::new(0,1.,0.);
|
||||
tree.insert_node(node);
|
||||
tree.insert_node(node);
|
||||
|
||||
assert_eq!(tree.nodes.len(), 2);
|
||||
}
|
||||
|
||||
fn qc_value_vec_to_2d_points_vec(xs: &Vec<f64>) -> Vec<Point2WithId> {
|
||||
let mut vec: Vec<Point2WithId> = vec![];
|
||||
for i in 0..xs.len() {
|
||||
|
@ -129,7 +129,7 @@ fn partition_kdtree<T: KdtreePointTrait>(vec: &mut [T], index_of_splitting_point
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ::kdtree::*;
|
||||
use ::kdtree::test_common::tests_utils::*;
|
||||
use ::kdtree::test_common::*;
|
||||
|
||||
use ::rand::distributions::{IndependentSample, Range};
|
||||
use ::rand::*;
|
||||
|
@ -1,61 +1,64 @@
|
||||
#[cfg(test)]
|
||||
pub mod tests_utils {
|
||||
use super::super::*;
|
||||
use super::KdtreePointTrait;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point3WithId {
|
||||
dims: [f64; 3],
|
||||
pub id: i32,
|
||||
}
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point3WithId {
|
||||
dims: [f64; 3],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point3WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
|
||||
Point3WithId {
|
||||
dims: [x, y, z],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point2WithId {
|
||||
dims: [f64; 2],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point2WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64) -> Point2WithId {
|
||||
Point2WithId {
|
||||
dims: [x, y],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KdtreePointTrait for Point2WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point1WithId {
|
||||
dims: [f64; 1],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point1WithId {
|
||||
pub fn new(id: i32, x: f64) -> Point1WithId {
|
||||
Point1WithId {
|
||||
dims: [x],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KdtreePointTrait for Point1WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
impl Point3WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
|
||||
Point3WithId {
|
||||
dims: [x, y, z],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KdtreePointTrait for Point3WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point2WithId {
|
||||
dims: [f64; 2],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point2WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64) -> Point2WithId {
|
||||
Point2WithId {
|
||||
dims: [x, y],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KdtreePointTrait for Point2WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point1WithId {
|
||||
dims: [f64; 1],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point1WithId {
|
||||
pub fn new(id: i32, x: f64) -> Point1WithId {
|
||||
Point1WithId {
|
||||
dims: [x],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KdtreePointTrait for Point1WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
@ -3,30 +3,8 @@ extern crate rand;
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
use kdtree::kdtree::*;
|
||||
use kdtree::kdtree::test_common::tests_utils::*;
|
||||
|
||||
//these could be taken from test_common, but I dont fully understand the module thingy yet.
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub struct Point3WithId {
|
||||
dims: [f64; 3],
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
impl Point3WithId {
|
||||
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
|
||||
Point3WithId {
|
||||
dims: [x, y, z],
|
||||
id: id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KdtreePointTrait for Point3WithId {
|
||||
fn dims(&self) -> &[f64] {
|
||||
return &self.dims;
|
||||
}
|
||||
}
|
||||
use kdtree::kdtree::test_common::*;
|
||||
use kdtree::kdtree::KdtreePointTrait;
|
||||
|
||||
fn gen_random() -> f64 {
|
||||
rand::thread_rng().gen_range(0., 10000.)
|
||||
@ -36,9 +14,15 @@ fn gen_random_usize( max_value : usize) -> usize {
|
||||
rand::thread_rng().gen_range(0usize, max_value)
|
||||
}
|
||||
|
||||
fn test() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn find_nn_with_linear_search<'a>(points : &'a Vec<Point3WithId>, find_for : Point3WithId) -> &Point3WithId {
|
||||
let distance_fun = kdtree::kdtree::distance::squared_euclidean;
|
||||
|
||||
|
||||
let mut best_found_distance = distance_fun(find_for.dims(), points[0].dims());
|
||||
let mut closed_found_point = &points[0];
|
||||
|
||||
@ -64,10 +48,12 @@ fn generate_points(point_count : usize) -> Vec<Point3WithId> {
|
||||
points
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_against_1000_random_points() {
|
||||
let point_count = 1000usize;
|
||||
let points = generate_points(point_count);
|
||||
kdtree::kdtree::test_common::Point1WithId::new(0,0.);
|
||||
|
||||
let tree = kdtree::kdtree::Kdtree::new(&mut points.clone());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user