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 38cc4f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 4 additions & 4 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1633,10 +1633,10 @@ function SymTridiagonal(dv::AbstractVector{T}, ev::AbstractVector{S}) where {T,S
end

# issue #22791
@deprecate_binding select partialsort
@deprecate_binding select! partialsort!
@deprecate_binding selectperm partialsortperm
@deprecate_binding selectperm! partialsortperm!
@deprecate select partialsort
@deprecate select! partialsort!
@deprecate selectperm partialsortperm
@deprecate selectperm! partialsortperm!

# END 0.7 deprecations

Expand Down
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 38cc4f6

Please sign in to comment.