Skip to content

Commit

Permalink
Optimize and improve dispatch to sort* (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored Dec 4, 2022
1 parent 996902a commit 57cbb74
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Common definitions

import Base: sort, findall, copy!
import Base: sort!, findall, copy!
import LinearAlgebra: promote_to_array_type, promote_to_arrays_
using LinearAlgebra: _SpecialArrays, _DenseConcatGroup

Expand Down Expand Up @@ -2123,15 +2123,15 @@ function _densifystarttolastnz!(x::SparseVector)
end

#sorting
function sort(x::AbstractCompressedVector{Tv,Ti}; kws...) where {Tv,Ti}
allvals = push!(copy(nonzeros(x)),zero(Tv))
sinds = sortperm(allvals;kws...)
n,k = length(x),length(allvals)
z = findfirst(isequal(k),sinds)::Int
newnzind = Vector{Ti}(1:k-1)
newnzind[z:end] .+= n-k+1
newnzvals = allvals[deleteat!(sinds[1:k],z)]
typeof(x)(n,newnzind,newnzvals)
function sort!(x::AbstractCompressedVector; kws...)
nz = nonzeros(x)
sort!(nz; kws...)
i = searchsortedfirst(nz, zero(eltype(x)); kws...)
I = nonzeroinds(x)
Base.require_one_based_indexing(x, nz, I)
I[1:i-1] .= 1:i-1
I[i:end] .= i+length(x)-length(nz):length(x)
x
end

function fkeep!(f, x::AbstractCompressedVector{Tv}) where Tv
Expand Down

0 comments on commit 57cbb74

Please sign in to comment.