-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSparseRowView.jl
49 lines (38 loc) · 1.42 KB
/
SparseRowView.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
export SparseRowView, vals, cols
using SparseArrays
import SparseArrays: nnz
export nnz
"""
SparseRowView(vals::AbstractArray{Data}, cols::AbstractArray{IndexType}, count::Integer=length(vals), start::Integer=1, stride::Integer=1)
Creates a view of a sparse row
"""
struct SparseRowView{Data, IndexType <: Integer}
vals::AbstractArray{Data}
cols::AbstractArray{IndexType}
function SparseRowView(vals::AbstractArray{Data}, cols::AbstractArray{IndexType}
) where {Data, IndexType}
if length(vals) != length(cols)
throw(InvalidArgumentError("length(vals) = $(length(vals)) "
* "!= length(cols) = $(length(cols))"))
end
new{Data, IndexType}(vals, cols)
end
end
function SparseRowView(vals::AbstractArray{Data}, cols::AbstractArray{IndexType},
count::Integer, start::Integer=1, stride::Integer=1) where {Data, IndexType}
SparseRowView(view(vals, range(start, step=stride, length=count)),
view(cols, range(start, step=stride, length=count)))
end
function nnz(row::SparseRowView{Data, IndexType}) where{Data, IndexType}
IndexType(length(row.vals))
end
"""
vals(::SparseRowView{Data, IndexType})::AbstractArray{Data, 1}
Gets the values of the row
"""
vals(row::SparseRowView) = row.vals
"""
cols(::SparseRowView{Data, IndexType})::AbstractArray{IndexType, 1}
Gets the column indices of the row
"""
cols(row::SparseRowView) = row.cols