-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsparsearray.jl
More file actions
117 lines (98 loc) · 2.9 KB
/
sparsearray.jl
File metadata and controls
117 lines (98 loc) · 2.9 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
abstract type AbstractSparseArray{T,N} <: AbstractArray{T,N} end
function Base.getindex(
sa::AbstractSparseArray{T,N},
idx::NTuple{N,Any},
) where {T,N}
return get(_data(sa), idx, zero(T))
end
function Base.getindex(
sa::AbstractSparseArray{T,N},
idx::NamedTuple,
) where {T,N}
return select(sa, idx)
end
function Base.getindex(sa::AbstractSparseArray{T,N}, idx...) where {T,N}
length(idx) != N && throw(BoundsError(sa, idx))
return _getindex(sa, idx)
end
function Base.setindex!(
sa::AbstractSparseArray{T,N},
val,
idx::NTuple{N,Any},
) where {T,N}
return set!(_data(sa), idx, val)
end
function Base.setindex!(sa::AbstractSparseArray{T,N}, val, idx...) where {T,N}
length(idx) != N && throw(BoundsError(sa, idx))
return setindex!(sa, val, idx)
end
Base.keys(sa::AbstractSparseArray) = keys(_data(sa))
function Base.summary(io::IO, sa::AbstractSparseArray)
num_entries = length(sa)
return print(
io,
typeof(sa),
" with ",
num_entries,
isone(num_entries) ? " entry" : " entries",
)
end
Base.length(sa::AbstractSparseArray) = length(_data(sa))
Base.sum(sa::AbstractSparseArray) = sum(_data(sa))
function Base.show(io::IO, ::MIME"text/plain", sa::AbstractSparseArray)
summary(io, sa)
if !iszero(length(_data(sa)))
println(io, ":")
if length(_data(sa)) > 20
show(io, first(_data(sa), 10))
print(io, "\n ⋮\n")
show(io, last(_data(sa), 10))
else
show(io, sa)
end
end
end
Base.show(io::IO, sa::AbstractSparseArray) = show(io, _data(sa))
Base.eachindex(sa::AbstractSparseArray) = keys(_data(sa))
function Base.firstindex(sa::AbstractSparseArray, d)
return minimum(x -> x[d], _data(sa).indices)
end
function Base.lastindex(sa::AbstractSparseArray, d)
return maximum(x -> x[d], _data(sa).indices)
end
function select(
sa::AbstractSparseArray{T,N},
pattern::NTuple{N,Any},
) where {T,N}
return select(keys(sa), pattern)
end
function select(
sa::AbstractSparseArray{T,N},
pattern...;
cache = true,
) where {T,N}
length(pattern) != N && throw(BoundsError(sa, pattern))
return select_test(sa, pattern, cache)
end
"""
SparseArray{T,N, K <: NTuple{N,Any} }
Implementation of an AbstractSparseArray where data is stored
in a dictionary.
"""
struct SparseArray{T,N,K<:NTuple{N,Any}} <: AbstractSparseArray{T,N}
data::Dictionary{K,T}
end
function SparseArray(d::Dict{K,T}) where {T,N,K<:NTuple{N,Any}}
return SparseArray(Dictionary(d))
end
function SparseArray(d::Dict{S,T}) where {S,T}
dd = Dict((key,) => val for (key, val) in d)
return SparseArray(Dictionary(dd))
end
function SparseArray{T,N}() where {T,N}
return SparseArray(Dictionary{NTuple{N,Any},T}())
end
function SparseArray{T,N,K}() where {T,N,K<:NTuple{N,Any}}
return SparseArray(Dictionary{K,T}())
end
_data(sa::SparseArray) = sa.data