From 4fa7ea57a2843b3f73cc6da4bfbe2f41f2730418 Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Thu, 7 Jul 2022 14:09:40 +0200 Subject: [PATCH] Let `length(::SparseVector{Tv,Ti})` return `Ti` --- src/sparsevector.jl | 1 + test/sparsevector.jl | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/sparsevector.jl b/src/sparsevector.jl index bda8ac4e..f8eed52e 100644 --- a/src/sparsevector.jl +++ b/src/sparsevector.jl @@ -53,6 +53,7 @@ const AdjOrTransSparseVectorUnion{Tv,Ti} = LinearAlgebra.AdjOrTrans{Tv, <:Sparse ### Basic properties +length(x::SparseVector) = getfield(x, :n) size(x::SparseVector) = (getfield(x, :n),) count(f, x::SparseVector) = count(f, nonzeros(x)) + f(zero(eltype(x)))*(length(x) - nnz(x)) diff --git a/test/sparsevector.jl b/test/sparsevector.jl index ced997c4..ff8135c1 100644 --- a/test/sparsevector.jl +++ b/test/sparsevector.jl @@ -33,9 +33,12 @@ 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]) + y = SparseVector(8, Int128[4], [5]) @test y isa SparseVector{Int,Int128} - @test @inferred size(y) == (@inferred(length(y)),) + @test @inferred size(y) == (@inferred(length(y))::Int128,) + y = SparseVector(8, Int8[4], [5.0]) + @test y isa SparseVector{Float64,Int8} + @test @inferred size(y) == (@inferred(length(y))::Int8,) end @testset "isstored" begin