Skip to content

Commit

Permalink
Fix bug in dense2ind and add tests for copy (#37855)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt authored Oct 3, 2020
1 parent defde49 commit 1b3ddd4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 3 additions & 4 deletions stdlib/SparseArrays/src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,14 @@ sparse(a::AbstractVector) = sparsevec(a)

function _dense2indval!(nzind::Vector{Ti}, nzval::Vector{Tv}, s::AbstractArray{Tv}) where {Tv,Ti}
require_one_based_indexing(s)
cap = length(nzind);
cap = length(nzind)
@assert cap == length(nzval)
n = length(s)
c = 0
@inbounds for i = 1:n
v = s[i]
@inbounds for (i, v) in enumerate(s)
if !iszero(v)
if c >= cap
cap *= 2
cap = (cap == 0) ? 1 : 2*cap
resize!(nzind, cap)
resize!(nzval, cap)
end
Expand Down
5 changes: 5 additions & 0 deletions stdlib/SparseArrays/test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ end
copyto!(x2, x) # copyto!(SparseVector, AbstractVector)
@test Vector(x2) == collect(x)
end
let x = 1:9, x1 = spzeros(length(x)), x2 = spzeros(length(x)-1)
@test_throws ArgumentError copy!(x2, x)
copy!(x1, convert.(eltype(x1), collect(x))) # copy!(SparseVector, AbstractVector)
@test Vector(x1) == collect(x)
end
end
@testset "vec/reinterpret/float/complex" begin
a = SparseVector(8, [2, 5, 6], Int32[12, 35, 72])
Expand Down

0 comments on commit 1b3ddd4

Please sign in to comment.