-
-
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
Added sortperm! #8792
Added sortperm! #8792
Conversation
* Allows preallocation (and therefore, reuse) of an index array * Useful for #8316
This makes me very happy. Thanks, Kevin! |
This is so awesome! I think |
Very nice. |
@@ -341,6 +342,14 @@ sortperm(v::AbstractVector; alg::Algorithm=DEFAULT_UNSTABLE, | |||
lt::Function=isless, by::Function=identity, rev::Bool=false, order::Ordering=Forward) = | |||
sort!([1:length(v)], alg, Perm(ord(lt,by,rev,order),v)) | |||
|
|||
function sortperm!{I<:Integer}(x::Vector{I}, v::AbstractVector; alg::Algorithm=DEFAULT_UNSTABLE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess x can be an AbstractVector
. Any reason it should not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that should be fine. I actually started with that, and then changed it for some reason that I forget.
What about workspace allocation in the various sort algorithms? Presumably MergeSort has a workspace. Can we make that reusable by passing a work array too? |
One change which I'm unsure of here is loosening the types allowed for
lt(p::Perm, a, b)
fromInt
toInteger
. I did it with the idea that, if a user is providing a preallocated array, she could choose to use a different integer type (e.g., a largeInt32
array might have better cache performance thanInt64
).That said, restricting the
sortperm
array type toVector{Int}
would be fine as well. Thoughts?Cc: @ViralBShah, @StefanKarpinski