Removed duplication, insert_node now checks for duplicates

This commit is contained in:
Olek 2016-12-30 19:14:34 +01:00
parent 885ed1d785
commit a6c19441b8
7 changed files with 79 additions and 122 deletions

View File

@ -5,48 +5,7 @@ extern crate rand;
use bencher::Bencher; use bencher::Bencher;
use rand::Rng; use rand::Rng;
use kdtree::kdtree::test_common::*;
#[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;
}
}
fn gen_random() -> f64 { fn gen_random() -> f64 {
rand::thread_rng().gen_range(0., 10000.) 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 len = 1000usize;
let mut points = generate_points(len); let mut points = generate_points(len);
let mut tree = kdtree::kdtree::Kdtree::new(&mut points); let mut tree = kdtree::kdtree::Kdtree::new(&mut points);
let point = Point3WithId::new(-1 as i32, gen_random(),gen_random(),gen_random()); let point = Point3WithId::new(-1 as i32, gen_random(),gen_random(),gen_random());
println!("before ..");
b.iter(|| { b.iter(|| {
println!("in lam ..");
tree.insert_node(point); tree.insert_node(point);
}); });
} }

View File

@ -90,7 +90,7 @@ impl Bounds {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use ::kdtree::test_common::tests_utils::*; use ::kdtree::test_common::*;
#[test] #[test]
fn bounds_test() { fn bounds_test() {

View File

@ -1,6 +1,4 @@
#[cfg(test)]
pub mod test_common; pub mod test_common;
pub mod distance; pub mod distance;
mod partition; mod partition;
@ -184,7 +182,7 @@ impl<T: KdtreePointTrait> KdtreeNode<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ::kdtree::test_common::tests_utils::Point2WithId; use ::kdtree::test_common::Point2WithId;
use super::*; use super::*;
@ -278,6 +276,19 @@ mod tests {
assert_eq!(tree.nodes[0].right_node.is_some(), true); 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> { fn qc_value_vec_to_2d_points_vec(xs: &Vec<f64>) -> Vec<Point2WithId> {
let mut vec: Vec<Point2WithId> = vec![]; let mut vec: Vec<Point2WithId> = vec![];
for i in 0..xs.len() { for i in 0..xs.len() {

View File

@ -129,7 +129,7 @@ fn partition_kdtree<T: KdtreePointTrait>(vec: &mut [T], index_of_splitting_point
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ::kdtree::*; use ::kdtree::*;
use ::kdtree::test_common::tests_utils::*; use ::kdtree::test_common::*;
use ::rand::distributions::{IndependentSample, Range}; use ::rand::distributions::{IndependentSample, Range};
use ::rand::*; use ::rand::*;

View File

@ -1,61 +1,64 @@
#[cfg(test)] use super::KdtreePointTrait;
pub mod tests_utils {
use super::super::*;
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub struct Point3WithId { pub struct Point3WithId {
dims: [f64; 3], dims: [f64; 3],
pub id: i32, pub id: i32,
} }
impl Point3WithId { impl Point3WithId {
pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId { pub fn new(id: i32, x: f64, y: f64, z: f64) -> Point3WithId {
Point3WithId { Point3WithId {
dims: [x, y, z], dims: [x, y, z],
id: id, 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 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;
}
}

View File

@ -3,30 +3,8 @@ extern crate rand;
use rand::Rng; use rand::Rng;
use kdtree::kdtree::*; use kdtree::kdtree::test_common::*;
use kdtree::kdtree::test_common::tests_utils::*; use kdtree::kdtree::KdtreePointTrait;
//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;
}
}
fn gen_random() -> f64 { fn gen_random() -> f64 {
rand::thread_rng().gen_range(0., 10000.) 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) 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 { fn find_nn_with_linear_search<'a>(points : &'a Vec<Point3WithId>, find_for : Point3WithId) -> &Point3WithId {
let distance_fun = kdtree::kdtree::distance::squared_euclidean; let distance_fun = kdtree::kdtree::distance::squared_euclidean;
let mut best_found_distance = distance_fun(find_for.dims(), points[0].dims()); let mut best_found_distance = distance_fun(find_for.dims(), points[0].dims());
let mut closed_found_point = &points[0]; let mut closed_found_point = &points[0];
@ -64,10 +48,12 @@ fn generate_points(point_count : usize) -> Vec<Point3WithId> {
points points
} }
#[test] #[test]
fn test_against_1000_random_points() { fn test_against_1000_random_points() {
let point_count = 1000usize; let point_count = 1000usize;
let points = generate_points(point_count); let points = generate_points(point_count);
kdtree::kdtree::test_common::Point1WithId::new(0,0.);
let tree = kdtree::kdtree::Kdtree::new(&mut points.clone()); let tree = kdtree::kdtree::Kdtree::new(&mut points.clone());