-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsparsearray.jl
More file actions
213 lines (196 loc) · 6.69 KB
/
Copy pathsparsearray.jl
File metadata and controls
213 lines (196 loc) · 6.69 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# simple wrapper to give indices a custom wrapping behaviour
struct SparseArray{T, N} <: AbstractArray{T, N}
data::Dict{CartesianIndex{N}, T}
dims::NTuple{N, Int64}
function SparseArray{T, N}(::UndefInitializer, dims::Dims{N}) where {T, N}
return new{T, N}(Dict{CartesianIndex{N}, T}(), dims)
end
function SparseArray(a::SparseArray{T, N}) where {T, N}
return new{T, N}(copy(a.data), a.dims)
end
function SparseArray{T, N}(
a::Dict{CartesianIndex{N}, T}, dims::NTuple{N, Int64}
) where {T, N}
return new{T, N}(a, dims)
end
end
function SparseArray{T}(::UndefInitializer, dims::Dims{N}) where {T, N}
return SparseArray{T, N}(undef, dims)
end
SparseArray{T}(::UndefInitializer, dims...) where {T} = SparseArray{T}(undef, dims)
function SparseArray{T}(a::UniformScaling, dims::Dims{2}) where {T}
A = SparseArray{T}(undef, dims)
for i in 1:min(dims...)
A[i, i] = a[1, 1]
end
return A
end
SparseArray(a::UniformScaling, dims::Dims{2}) = SparseArray{eltype(a)}(a, dims)
SparseArray(a::UniformScaling, d1::Int, d2::Int) = SparseArray{eltype(a)}(a, (d1, d2))
nonzero_pairs(a::SparseArray) = pairs(a.data)
nonzero_keys(a::SparseArray) = keys(a.data)
nonzero_values(a::SparseArray) = values(a.data)
nonzero_length(a::SparseArray) = length(a.data)
_zero!(x::SparseArray) = (empty!(x.data); return x)
_sizehint!(x::SparseArray, n) = sizehint!(x.data, n)
# elementary getindex and setindex!
@inline function Base.getindex(a::SparseArray{T, N}, I::CartesianIndex{N}) where {T, N}
@boundscheck checkbounds(a, I)
return get(a.data, I, zero(T))
end
Base.@propagate_inbounds function Base.getindex(
a::SparseArray{T, N}, I::Vararg{Int, N}
) where {T, N}
return getindex(a, CartesianIndex(I))
end
@inline function Base.setindex!(a::SparseArray{T, N}, v, I::CartesianIndex{N}) where {T, N}
@boundscheck checkbounds(a, I)
if !iszero(v)
a.data[I] = v
else
delete!(a.data, I) # does not do anything if there was no key corresponding to I
end
return v
end
Base.@propagate_inbounds function Base.setindex!(
a::SparseArray{T, N},
v, I::Vararg{Int, N}
) where {T, N}
return setindex!(a, v, CartesianIndex(I))
end
@inline function increaseindex!(a::SparseArray{T, N}, v, I::CartesianIndex{N}) where {T, N}
@boundscheck checkbounds(a, I)
iszero(v) && return
h = a.data
index = Base.ht_keyindex2!(h, I)
@inbounds begin
if index > 0
currentv = h.vals[index]
newv = currentv + convert(T, v)
if iszero(newv)
Base._delete!(h, index)
else
h.age += 1
h.keys[index] = I
h.vals[index] = newv
end
else
newv = convert(T, v)
Base._setindex!(h, newv, I, -index)
end
end
return newv
end
# following code is used to index with ranges etc
_newindex(i::Int, range::Int) = i == range ? () : nothing
function _newindex(i::Int, range::AbstractVector{Int})
k = findfirst(==(i), range)
return k === nothing ? nothing : (k,)
end
_newindices(I::Tuple{}, indices::Tuple{}) = ()
function _newindices(I::Tuple, indices::Tuple)
i = _newindex(I[1], indices[1])
Itail = _newindices(Base.tail(I), Base.tail(indices))
(i === nothing || Itail === nothing) && return nothing
return (i..., Itail...)
end
_findfirstvalue(v, r) = findfirst(==(v), r)
# slicing should produce SparseArray
function Base._unsafe_getindex(
::IndexCartesian, a::SparseArray{T, N}, I::Vararg{Union{Int, AbstractVector{Int}}, N}
) where {T, N}
@boundscheck checkbounds(a, I...)
indices = Base.to_indices(a, I)
b = SparseArray{T}(undef, length.(Base.index_shape(indices...)))
for (k, v) in a.data
newI = _newindices(k.I, indices)
if newI !== nothing
b[newI...] = v
end
end
return b
end
Base.Array(a::SparseArray{T, N}) where {T, N} = Array{T, N}(a)
function Base.Array{T, N}(a::SparseArray) where {T, N}
d = fill(zero(T), size(a))
for (I, v) in a.data
d[I] = v
end
return d
end
SparseArray(a::AbstractArray{T, N}) where {T, N} = SparseArray{T, N}(a)
SparseArray{T}(a::AbstractArray{<:Any, N}) where {T, N} = SparseArray{T, N}(a)
function SparseArray{T, N}(a::AbstractArray{<:Any, N}) where {T, N}
d = SparseArray{T, N}(undef, size(a))
for I in CartesianIndices(a)
iszero(a[I]) && continue
d[I] = a[I]
end
return d
end
Base.convert(::Type{S}, a::S) where {S <: SparseArray} = a
Base.convert(S::Type{<:SparseArray}, a::AbstractArray) = S(a)
function SparseArray(A::Adjoint{T, <:SparseArray{T, 2}}) where {T}
B = SparseArray{T}(undef, size(A))
for (I, v) in parent(A).data
B[I[2], I[1]] = conj(v)
end
return B
end
function SparseArray(A::Transpose{T, <:SparseArray{T, 2}}) where {T}
B = SparseArray{T}(undef, size(A))
for (I, v) in parent(A).data
B[I[2], I[1]] = v
end
return B
end
Base.copy(a::SparseArray) = SparseArray(a)
Base.size(a::SparseArray) = a.dims
function Base.copy!(dst::SparseArray, src::SparseArray)
axes(dst) == axes(src) ||
throw(ArgumentError("arrays must have the same axes for copy! (consider using `copyto!`)"))
_zero!(dst)
for (I, v) in nonzero_pairs(src)
dst[I] = v
end
return dst
end
function Base.similar(::SparseArray, ::Type{S}, dims::Dims{N}) where {S, N}
return SparseArray{S}(undef, dims)
end
# show and friends
function Base.show(io::IO, ::MIME"text/plain", x::SparseArray)
xnnz = nonzero_length(x)
print(
io, join(size(x), "×"), " ", typeof(x), " with ", xnnz, " stored ",
xnnz == 1 ? "entry" : "entries"
)
return if xnnz != 0
println(io, ":")
show(IOContext(io, :typeinfo => eltype(x)), x)
end
end
Base.show(io::IO, x::SparseArray) = show(convert(IOContext, io), x)
function Base.show(io::IOContext, x::SparseArray)
nzind = nonzero_keys(x)
if isempty(nzind)
return show(io, MIME("text/plain"), x)
end
limit = get(io, :limit, false)::Bool
half_screen_rows = limit ? div(displaysize(io)[1] - 8, 2) : typemax(Int)
pads = map(1:ndims(x)) do i
return ndigits(maximum(getindex.(nzind, i)))
end
if !haskey(io, :compact)
io = IOContext(io, :compact => true)
end
for (k, (ind, val)) in enumerate(nonzero_pairs(x))
if k < half_screen_rows || k > length(nzind) - half_screen_rows
print(io, " ", '[', join(lpad.(Tuple(ind), pads), ","), "] = ", val)
k != length(nzind) && println(io)
elseif k == half_screen_rows
println(io, " ", join(" " .^ pads, " "), " \u22ee")
end
end
return
end