From 38cc4f6e84b39711431a6c0e549b29185649371d Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Mon, 31 Jul 2017 16:13:07 +0200 Subject: [PATCH] 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. --- base/deprecated.jl | 8 ++++---- base/sort.jl | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index bfe9877fbbb18..747957e6f1beb 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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 diff --git a/base/sort.jl b/base/sort.jl index cf173218e14ab..ede1e1e032bce 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -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 """ @@ -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 ##