kdtree-rs icon indicating copy to clipboard operation
kdtree-rs copied to clipboard

Program stucks when removing non-existing points or points at the same place

Open ytskuh opened this issue 2 years ago • 1 comments

Kind of this

let a = ([0f64, 0f64], 0);
let b = ([1f64, 0f64], 1);
let mut kdtree = KdTree::new(2);

kdtree.add(a.0, a.1).unwrap();
kdtree.add(b.0, b.1).unwrap();

kdtree.remove(&[0f64, 0f64], &1).unwrap();

or

let a = ([0f64, 0f64], 0);
let b = ([0f64, 0f64], 1);
let mut kdtree = KdTree::new(2);

kdtree.add(a.0, a.1).unwrap();
kdtree.add(b.0, b.1).unwrap();

kdtree.remove(&[0f64, 0f64], &1).unwrap();

will cause the program to stuck. There may be some issue with the implementation of the remove fn.

ytskuh avatar Feb 21 '23 11:02 ytskuh

Not sure if this is still an issue for you, I think your problem is here https://github.com/mrhooray/kdtree-rs/blob/7f03078e20788ec27126dc71e3ba81fb8b8f1c58/src/kdtree.rs#L263 basically if it is a duplicate point on the tree, and the one it finds is not the one you want to delete, it will loop forever not sure if it is the best solution, but I zipped the two iterators together and it works good enough for me.

        if let (Some(mut points), Some(mut bucket)) = (self.points.take(), self.bucket.take()) {
            while let Some(p_index) = points.iter().zip(bucket.iter()).position(|(p, d)| p == point && d == data) {
                    points.remove(p_index);
                    bucket.remove(p_index);
                    removed += 1;
                    self.size -= 1;
            }
            self.points = Some(points);
            self.bucket = Some(bucket);
        } else {

tarriel avatar Jun 19 '23 09:06 tarriel