Skip to content

Commit

Permalink
Fix bug where non-order keyword arguments are passed from sort! to …
Browse files Browse the repository at this point in the history
…`searchsortedfirst` (#335)
  • Loading branch information
LilithHafner authored and dkarrasch committed Mar 6, 2023
1 parent fcb1719 commit 7df072a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2122,16 +2122,19 @@ function _densifystarttolastnz!(x::SparseVector)
x
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)
#sorting TODO: integrate with `Base.Sort.IEEEFloatOptimization`'s partitioning by zero
searchsortedfirst_discard_keywords(v::AbstractVector, x; lt=isless, by=identity,
rev::Union{Bool,Nothing}=nothing, order::Base.Order.Ordering=Forward, kws...) =
searchsortedfirst(v,x,Base.Order.ord(lt,by,rev,order))
function sort!(x::AbstractCompressedVector; kws...)
nz = nonzeros(x)
sort!(nz; kws...)
i = searchsortedfirst_discard_keywords(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
6 changes: 6 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ g12063() = f12063(0, 0, 0, 0, 0, 0, 0.0, spzeros(0,0), Int[])
@test String(take!(io)) == "transpose(sparse([1, 2, 1, 2], [1, 1, 2, 2], [1, 3, 2, 4], 2, 2))"
end

@testset "Issue #334" begin
x = sprand(10, .3);
@test issorted(sort!(x; alg=Base.DEFAULT_STABLE));
@test_throws MethodError sort!(x; banana=:blue); # From discussion at #335
end

end # SparseTestsBase

end # module

0 comments on commit 7df072a

Please sign in to comment.