NearestNeighbors.jl icon indicating copy to clipboard operation
NearestNeighbors.jl copied to clipboard

Retrieve up to `k` random indices `inrange`

Open juliohm opened this issue 5 months ago • 1 comments

The inrange function is super useful, yet some applications only need a subset of at most k indices inside the range. This is particularly useful in hot loops because then it is possible to pre-allocate a buffer of length k once, and populate this buffer with a set of random indices inside the range.

How hard would it be to add a new knninrange function that achieves this goal? From what I understand, the following core function is responsible for adding indices to the final result of inrange:

https://github.com/KristofferC/NearestNeighbors.jl/blob/8867613ce97d6cf16850c54dbcdc2c999cb2a69a/src/tree_ops.jl#L117-L128

Could we adjust the code to consider custom criteria besides check_in_range ̇?

juliohm avatar Jul 03 '25 11:07 juliohm

It shouldn't be hard, you would need to have the full count available in add_points_inrange! so that it stops when enough points have been added and then "bubble up" all the way from that.

KristofferC avatar Jul 03 '25 12:07 KristofferC