-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
searchsorted by applies by function to item being searched #9429
Comments
I think that makes sense. Care to try fixing this and making a pull request? No worries if not. |
I encountered the exact same problem in trying to find a column in a column-lex-sorted Matrix: A = abs(rand(Int,3,7)) % 10
B = sortcols(A)
x = B[:,5]
searchsortedfirst(collect(1:7), x, by = i->B[:,i], lt = lexless) This fails like the OP describes. The alternative is even more clumsy here as it would require the addition of an extra column containing the search target to I don't have experience with pull request but am willing to look into it. |
This came up at #19295 (comment). I guess #19295 covers this now? |
#19295 is now closed, but this issue still exists. Let me give another example where it is very annoying: struct Coord{Tv}
x::Tv
y::Tv
end
norm2(p::Coord) = p.x * p.x + p.y * p.y
function function_from_another_package()
ps = [Coord(randn(), randn()) for i = 1 : 10]
I = sortperm(ps, by = norm2)
return ps, I
end
function find_first_coord_outside_unit_circle()
ps, I = function_from_another_package()
# You can't pass the distance 1 as you'd expect
searchsortedfirst(I, 1.0, by = i -> norm2(ps[i])) # nope...
# You can't pass a coord on the unit circle either...
searchsortedfirst(I, Coord(1.0, 0.0), by = i -> norm2(ps[i])) # nope...
# The only solution seems to be the following -- but this
# requires me looking into the internals of searchsortedfirst
searchsortedfirst(I, 1.0, lt = (p, q) -> norm2(ps[p]) < q)
end |
I have a potential solution here: https://github.com/haampie/SortingSortingOut.jl#make-search-convenient----search-by-transformed-value-not-by-specific-vector-element |
Consider this:
The first search call fails and the second is not ideal because one has to use a dummy value (NaN here) to fill in the parts of the item being searched that are not part of the key. I think that in
searchsortedfirst(S, x, by=f)
it would make more sense to apply f() to the items of S but not to the item x. If the searchsorted functions worked like that, then the current behavior is easily obtained by usingsearchsortedfirst(S, f(x), by=f)
. Inversion of f() is not necessarily as easy.The text was updated successfully, but these errors were encountered: