Skip to content

Commit

Permalink
Let length(::SparseVector{Tv,Ti}) return Ti (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch authored Jul 9, 2022
1 parent e081db6 commit c5ddb5b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
7 changes: 5 additions & 2 deletions test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c5ddb5b

Please sign in to comment.