Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ytskuh opened this issue Feb 21, 2023 · 1 comment

Comments

@ytskuh
Copy link

ytskuh commented Feb 21, 2023

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.

@tarriel
Copy link

tarriel commented Jun 19, 2023

Not sure if this is still an issue for you, I think your problem is here

while let Some(p_index) = points.iter().position(|x| x == point) {

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 {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants