Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ julia> findnz(A)
"""
function findnz end

"""
iternz(A::AbstractSparseArray)

Equivalent to `zip(findnz(A)...)` but does not allocated
```
"""
function iternz end

widelength(x::AbstractSparseArray) = prod(Int64.(size(x)))


Expand Down
31 changes: 31 additions & 0 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,37 @@
return SparseMatrixCSC(m, n, colptr, I, V)
end


abstract type SparseIndexIterate end
@inline getcolptr(x::SparseIndexIterate) = getcolptr(x.m)
@inline getrowval(x::SparseIndexIterate) = getrowval(x.m)
@inline getnzval(x::SparseIndexIterate) = getnzval(x.m)
@inline nonzeroinds(x::SparseIndexIterate) = nonzeroinds(x.m)
@inline nonzeros(x::SparseIndexIterate) = nonzeros(x.m)
@inline nnz(x::SparseIndexIterate) = nnz(x.m)

Base.length(x::SparseIndexIterate) = nnz(x.m)
Base.size(x::SparseIndexIterate) = size(x.m)

Check warning on line 1785 in src/sparsematrix.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsematrix.jl#L1785

Added line #L1785 was not covered by tests
Base.size(x::SparseIndexIterate, i) = size(x.m)[i]
struct IterateNZCSC{T<: AbstractSparseMatrixCSC} <: SparseIndexIterate
m::T
end

Base.eltype(::IterateNZCSC{T}) where {Ti, Tv, T <: AbstractSparseMatrixCSC{Tv, Ti}} = Tuple{Ti, Ti, Tv}
Base.iterate(x::IterateNZCSC, state=(1, 0)) = @inbounds let (j, ind) = state
ind += 1
while (j < size(x, 2)) && (ind > getcolptr(x)[j + 1] - 1)
j += 1
end
(j > size(x, 2) || ind > getcolptr(x)[end] - 1) && return nothing

(getrowval(x)[ind], j, getnzval(x)[ind]), (j, ind)
end

iternz(S::AbstractSparseMatrixCSC) = IterateNZCSC(S)



"""
sprand([rng],[type],m,[n],p::AbstractFloat,[rfn])

Expand Down
20 changes: 20 additions & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,26 @@ function _sparse_findprevnz(v::SparseVector, i::Integer)
end
end



struct IterateSparseVec{T<: AbstractSparseVector} <: SparseIndexIterate
m::T
end

Base.eltype(::IterateSparseVec{T}) where {Ti, Tv, T <: AbstractSparseVector{Tv, Ti}} = Tuple{Ti, Tv}

Base.iterate(x::IterateSparseVec, state=0) = @inbounds begin
state += 1
if state > nnz(x)
nothing
else
(nonzeroinds(x)[state], nonzeros(x)[state]), state
end
end

iternz(S::AbstractSparseVector) = IterateSparseVec(S)


### Generic functions operating on AbstractSparseVector

### getindex
Expand Down
6 changes: 6 additions & 0 deletions test/sparsematrix_constructors_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1578,4 +1578,10 @@ end
@test_throws ArgumentError SparseArrays.expandptr([2; 3])
end

@testset "iteratenz" begin
for i in 1:20
A = sprandn(100, 100, 1 / i)
@test collect(SparseArrays.iternz(A)) == collect(zip(findnz(A)...))
end
end
end
7 changes: 7 additions & 0 deletions test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ end
@test findnz(@view Xc[:,2]) == ([2], [1.25])
end
end

@testset "iteratenz (vector)" begin
for i in 1:10
A = sprandn(100, i / 100)
@test collect(SparseArrays.iternz(A)) == collect(zip(findnz(A)...))
end
end
### Array manipulation

@testset "copy[!]" begin
Expand Down