Skip to content

Commit

Permalink
Make partialsort!() and partialsortperm!() return a view rather than …
Browse files Browse the repository at this point in the history
…a copy

Returning a copy (partially) defeats the purpose of these functions, which is
to avoid allocations.
  • Loading branch information
nalimilan committed Aug 7, 2017
1 parent 55347cd commit a0ac7d1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ issorted(itr;
function partialsort!(v::AbstractVector, k::Union{Int,OrdinalRange}, o::Ordering)
inds = indices(v, 1)
sort!(v, first(inds), last(inds), PartialQuickSort(k), o)
v[k]

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

"""
Expand Down Expand Up @@ -704,7 +709,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 a0ac7d1

Please sign in to comment.