Skip to content

Commit

Permalink
Make partialsortperm!() return a view rather than a copy
Browse files Browse the repository at this point in the history
Returning a copy (partially) defeats the purpose of the function, which is
to avoid allocations.
  • Loading branch information
nalimilan committed Jul 31, 2017
1 parent 0ac1efe commit 7f4852d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ those indices is returned.
Note that this is equivalent to, but more efficient than, calling `sortperm(...)[k]`.
"""
partialsortperm(v::AbstractVector, k::Union{Integer,OrdinalRange}; kwargs...) =
partialsortperm!(similar(Vector{eltype(k)}, indices(v,1)), v, k; kwargs..., initialized=false)
copy(partialsortperm!(similar(Vector{eltype(k)}, indices(v,1)), v, k; kwargs..., initialized=false))

"""
partialsortperm!(ix, v, k, [alg=<algorithm>,] [by=<transform>,] [lt=<comparison>,] [rev=false,] [initialized=false])
Expand All @@ -705,7 +705,12 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,

# do partial quicksort
sort!(ix, PartialQuickSort(k), Perm(ord(lt, by, rev, order), v))
return ix[k]

if k isa Integer
return ix[k]
else
return view(ix, k)
end
end

## sortperm: the permutation to sort an array ##
Expand Down

0 comments on commit 7f4852d

Please sign in to comment.