Skip to content

Commit 38cc4f6

Browse files
committed
Make partialsort!() and partialsortperm!() return a view rather than a copy
Returning a copy (partially) defeats the purpose of these functions, which is to avoid allocations.
1 parent 55347cd commit 38cc4f6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

base/deprecated.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,10 +1633,10 @@ function SymTridiagonal(dv::AbstractVector{T}, ev::AbstractVector{S}) where {T,S
16331633
end
16341634

16351635
# issue #22791
1636-
@deprecate_binding select partialsort
1637-
@deprecate_binding select! partialsort!
1638-
@deprecate_binding selectperm partialsortperm
1639-
@deprecate_binding selectperm! partialsortperm!
1636+
@deprecate select partialsort
1637+
@deprecate select! partialsort!
1638+
@deprecate selectperm partialsortperm
1639+
@deprecate selectperm! partialsortperm!
16401640

16411641
# END 0.7 deprecations
16421642

base/sort.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ issorted(itr;
8585
function partialsort!(v::AbstractVector, k::Union{Int,OrdinalRange}, o::Ordering)
8686
inds = indices(v, 1)
8787
sort!(v, first(inds), last(inds), PartialQuickSort(k), o)
88-
v[k]
88+
89+
if k isa Integer
90+
return v[k]
91+
else
92+
return view(v, k)
93+
end
8994
end
9095

9196
"""
@@ -704,7 +709,12 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
704709

705710
# do partial quicksort
706711
sort!(ix, PartialQuickSort(k), Perm(ord(lt, by, rev, order), v))
707-
return ix[k]
712+
713+
if k isa Integer
714+
return ix[k]
715+
else
716+
return view(ix, k)
717+
end
708718
end
709719

710720
## sortperm: the permutation to sort an array ##

0 commit comments

Comments
 (0)