-
-
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
selectperm? #10767
Comments
Digging through sort.jl, I came up with this (note I prefix everything with absolute path because I wrote this function outside of Base): function selectperm(v::AbstractVector,
k::Union(Int,OrdinalRange);
lt::Function=isless,
by::Function=identity,
rev::Bool=false,
order::Base.Order.Ordering=Base.Order.Forward)
select!(collect(1:length(v)), k, Base.Order.Perm(Base.Order.ord(lt,by,rev,order),v))
end It seems to do the job for me. Is this the best way to get this functionality? If so, I'm happy to package this up inside sort.jl and make the interface more like the current |
That does the trick. It seems unfortunate to allocate |
It would be great if we could solve that problem. The application I'm currently using this for typically has |
I also needed this functionality in a hot loop, so I had to avoid allocation. I ended up implementing a partial quick sort algorithm and using that. |
I wonder if this would be worth including in the standard library or SortingAlgorithms? |
Since the standard library already has |
It does seem quite logical, I must say. |
I agree with @nalimilan here. This seems like it naturally belongs with the rest of its family. |
I'm ok with the API being allocation by default and with the option of pre-allocation when performance is critical. That marries default convenience with the option of being fast without too much hassle. |
I haven't tested yet, but I suppose that the code @carlobaldassi shared will be more performant than what I have here. Maybe we make |
+1 for having this in base. |
I wrote that code quite some time ago, and when I benchmarked it it was indeed the fastest way I could find to achieve the goal. I only had one use case though, and it was before the new GC. It would be good to benchmark it again. But at least I remember I thoroughly tested the PartialQuickSort implementation, so there should be no surprises on that side hopefully. |
I'll try to package this up into a PR in the next few days |
See #10800 |
@spencerlyon2 If you add |
Ahh, that's how it works. I added that line and expected it to close this issue immediately. I guess I should have waited for the PR to be accepted. |
No problem! This should be fine. |
I was hoping to find a routine in base that would give me the first
k
elements ofsortperm(v)
.This would be much like
select(v, 1:k)
, but instead of returning the actual elements, it would return the index where those elements can be found.Does such a function exist?
The text was updated successfully, but these errors were encountered: