Skip to content

Commit 38604c3

Browse files
authored
Fix bug where non-order keyword arguments are passed from sort! to searchsortedfirst (#335)
1 parent 0d2d30a commit 38604c3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/sparsevector.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ function Vector(x::AbstractSparseVector{Tv}) where Tv
10641064
return r
10651065
end
10661066
Array(x::AbstractSparseVector) = Vector(x)
1067-
1067+
10681068
function Base.collect(x::Union{AbstractSparseVector,AbstractSparseMatrix})
10691069
if Base.has_offset_axes(x)
10701070
return Base._collect_indices(axes(x), x)
@@ -2138,11 +2138,14 @@ function _densifystarttolastnz!(x::SparseVector)
21382138
x
21392139
end
21402140

2141-
#sorting
2141+
#sorting TODO: integrate with `Base.Sort.IEEEFloatOptimization`'s partitioning by zero
2142+
searchsortedfirst_discard_keywords(v::AbstractVector, x; lt=isless, by=identity,
2143+
rev::Union{Bool,Nothing}=nothing, order::Base.Order.Ordering=Forward, kws...) =
2144+
searchsortedfirst(v,x,Base.Order.ord(lt,by,rev,order))
21422145
function sort!(x::AbstractCompressedVector; kws...)
21432146
nz = nonzeros(x)
21442147
sort!(nz; kws...)
2145-
i = searchsortedfirst(nz, zero(eltype(x)); kws...)
2148+
i = searchsortedfirst_discard_keywords(nz, zero(eltype(x)); kws...)
21462149
I = nonzeroinds(x)
21472150
Base.require_one_based_indexing(x, nz, I)
21482151
I[1:i-1] .= 1:i-1

test/issues.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,12 @@ g12063() = f12063(0, 0, 0, 0, 0, 0, 0.0, spzeros(0,0), Int[])
783783
@test String(take!(io)) == "transpose(sparse([1, 2, 1, 2], [1, 1, 2, 2], [1, 3, 2, 4], 2, 2))"
784784
end
785785

786+
@testset "Issue #334" begin
787+
x = sprand(10, .3);
788+
@test issorted(sort!(x; alg=Base.DEFAULT_STABLE));
789+
@test_throws MethodError sort!(x; banana=:blue); # From discussion at #335
790+
end
791+
786792
end # SparseTestsBase
787793

788794
end # module

0 commit comments

Comments
 (0)