From a0ac7d1f39ff431bbaadf73d83379b004baa5695 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/sort.jl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/base/sort.jl b/base/sort.jl index cf173218e14abf..ede1e1e032bce1 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 ##