Skip to content

Commit

Permalink
Make length type match index type in sparse vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch committed Feb 13, 2021
1 parent 6468dcb commit cf85344
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions stdlib/SparseArrays/src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import LinearAlgebra: promote_to_array_type, promote_to_arrays_
Vector type for storing sparse vectors.
"""
struct SparseVector{Tv,Ti<:Integer} <: AbstractSparseVector{Tv,Ti}
n::Int # Length of the sparse vector
n::Ti # Length of the sparse vector
nzind::Vector{Ti} # Indices of stored values
nzval::Vector{Tv} # Stored values, typically nonzeros

function SparseVector{Tv,Ti}(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) where {Tv,Ti<:Integer}
n >= 0 || throw(ArgumentError("The number of elements must be non-negative."))
length(nzind) == length(nzval) ||
throw(ArgumentError("index and value vectors must be the same length"))
new(convert(Int, n), nzind, nzval)
new(convert(Ti, n), nzind, nzval)
end
end

Expand Down
3 changes: 3 additions & 0 deletions stdlib/SparseArrays/test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ x1_full[SparseArrays.nonzeroinds(spv_x1)] = nonzeros(spv_x1)
@test SparseArrays.nonzeroinds(x) == [2, 5, 6]
@test nonzeros(x) == [1.25, -0.75, 3.5]
@test count(SparseVector(8, [2, 5, 6], [true,false,true])) == 2
y = SparseVector(typemax(Int128), Int128[4], [5])
@test y isa SparseVector{Int,Int128}
@test @inferred size(y) == (@inferred(length(y)),)
end

@testset "isstored" begin
Expand Down

0 comments on commit cf85344

Please sign in to comment.