Skip to content

Commit cf85344

Browse files
committed
Make length type match index type in sparse vectors
1 parent 6468dcb commit cf85344

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

stdlib/SparseArrays/src/sparsevector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import LinearAlgebra: promote_to_array_type, promote_to_arrays_
1515
Vector type for storing sparse vectors.
1616
"""
1717
struct SparseVector{Tv,Ti<:Integer} <: AbstractSparseVector{Tv,Ti}
18-
n::Int # Length of the sparse vector
18+
n::Ti # Length of the sparse vector
1919
nzind::Vector{Ti} # Indices of stored values
2020
nzval::Vector{Tv} # Stored values, typically nonzeros
2121

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

stdlib/SparseArrays/test/sparsevector.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ x1_full[SparseArrays.nonzeroinds(spv_x1)] = nonzeros(spv_x1)
3333
@test SparseArrays.nonzeroinds(x) == [2, 5, 6]
3434
@test nonzeros(x) == [1.25, -0.75, 3.5]
3535
@test count(SparseVector(8, [2, 5, 6], [true,false,true])) == 2
36+
y = SparseVector(typemax(Int128), Int128[4], [5])
37+
@test y isa SparseVector{Int,Int128}
38+
@test @inferred size(y) == (@inferred(length(y)),)
3639
end
3740

3841
@testset "isstored" begin

0 commit comments

Comments
 (0)