Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where non-order keyword arguments are passed from sort! to searchsortedfirst #335

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ function Vector(x::AbstractSparseVector{Tv}) where Tv
return r
end
Array(x::AbstractSparseVector) = Vector(x)

function Base.collect(x::Union{AbstractSparseVector,AbstractSparseMatrix})
if Base.has_offset_axes(x)
return Base._collect_indices(axes(x), x)
Expand Down Expand Up @@ -2138,11 +2138,14 @@ function _densifystarttolastnz!(x::SparseVector)
x
end

#sorting
#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...)
Comment on lines +2142 to 2145
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not apply the same trick to sort! directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want sort!(sprand(10, .1), banana=:blue) to continue to error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Judging from the code, however, I don't quite see where that call would throw.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should throw at sort!(nz; kws...). I'll also add a test.

nz = nonzeros(x)
sort!(nz; kws...)
i = searchsortedfirst(nz, zero(eltype(x)); 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
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));
LilithHafner marked this conversation as resolved.
Show resolved Hide resolved
@test_throws MethodError sort!(x; banana=:blue); # From discussion at #335
end

end # SparseTestsBase

end # module