From c425d35eaf96f4b8004f8cb80f6b4ba705798cd1 Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Sat, 13 Feb 2021 11:21:38 +0100 Subject: [PATCH] Make length type match index type in sparse vectors --- stdlib/SparseArrays/src/sparsevector.jl | 4 ++-- stdlib/SparseArrays/test/sparsevector.jl | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/SparseArrays/src/sparsevector.jl b/stdlib/SparseArrays/src/sparsevector.jl index f893fb04eadf3..eecda6d72add8 100644 --- a/stdlib/SparseArrays/src/sparsevector.jl +++ b/stdlib/SparseArrays/src/sparsevector.jl @@ -15,7 +15,7 @@ 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 @@ -23,7 +23,7 @@ struct SparseVector{Tv,Ti<:Integer} <: AbstractSparseVector{Tv,Ti} 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 diff --git a/stdlib/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index 03aed95a43a83..2dc9738111a87 100644 --- a/stdlib/SparseArrays/test/sparsevector.jl +++ b/stdlib/SparseArrays/test/sparsevector.jl @@ -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