Skip to content

Commit deb81ae

Browse files
authored
Improve isstored logic (#46)
1 parent 1236822 commit deb81ae

File tree

7 files changed

+34
-22
lines changed

7 files changed

+34
-22
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseArraysBase"
22
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.4.1"
4+
version = "0.5.0"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

docs/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
88
Dictionaries = "0.4.4"
99
Documenter = "1.8.1"
1010
Literate = "2.20.1"
11-
SparseArraysBase = "0.4.0"
11+
SparseArraysBase = "0.5.0"

examples/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
55

66
[compat]
77
Dictionaries = "0.4.4"
8-
SparseArraysBase = "0.4.0"
8+
SparseArraysBase = "0.5.0"
99
Test = "<0.0.1, 1"

src/abstractsparsearrayinterface.jl

+6-8
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,22 @@ end
3535

3636
# Minimal interface for `SparseArrayInterface`.
3737
# Fallbacks for dense/non-sparse arrays.
38-
@interface ::AbstractArrayInterface function isstored(
39-
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
40-
) where {N}
38+
function isstored(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
4139
@_propagate_inbounds_meta
4240
@boundscheck checkbounds(a, I...)
4341
return true
4442
end
45-
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int)
43+
function isstored(a::AbstractArray, I::Int)
4644
@_propagate_inbounds_meta
47-
return @interface interface isstored(a, Tuple(CartesianIndices(a)[I])...)
45+
return isstored(a, Tuple(CartesianIndices(a)[I])...)
4846
end
49-
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int...)
47+
function isstored(a::AbstractArray, I::Int...)
5048
@_propagate_inbounds_meta
5149
@boundscheck checkbounds(a, I...)
5250
I′ = ntuple(i -> I[i], ndims(a))
53-
return @inbounds @interface interface isstored(a, I′...)
51+
return isstored(a, I′...)
5452
end
53+
5554
@interface ::AbstractArrayInterface eachstoredindex(a::AbstractArray) = eachindex(a)
5655
@interface ::AbstractArrayInterface getstoredindex(a::AbstractArray, I::Int...) =
5756
getindex(a, I...)
@@ -111,7 +110,6 @@ end
111110
SparseArraysBase.eachstoredindex(::T...)
112111
SparseArraysBase.getstoredindex(::T, ::Int...)
113112
SparseArraysBase.getunstoredindex(::T, ::Int...)
114-
SparseArraysBase.isstored(::T, ::Int...)
115113
SparseArraysBase.setstoredindex!(::T, ::Any, ::Int...)
116114
SparseArraysBase.setunstoredindex!(::T, ::Any, ::Int...)
117115
SparseArraysBase.storedlength(::T)

src/sparsearraydok.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ storage(a::SparseArrayDOK) = a.storage
7474
Base.size(a::SparseArrayDOK) = a.size
7575

7676
storedvalues(a::SparseArrayDOK) = values(storage(a))
77-
function isstored(a::SparseArrayDOK, I::Int...)
77+
function isstored(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
7878
return @interface interface(a) isstored(a, I...)
7979
end
8080
function eachstoredindex(a::SparseArrayDOK)

src/wrappers.jl

+23-9
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ function storedparentvalues(a::SubArray)
106106
return StoredValues(parent(a), collect(eachstoredparentindex(a)))
107107
end
108108

109-
@interface ::AbstractArrayInterface function isstored(a::SubArray, I::Int...)
110-
return isstored(parent(a), index_to_parentindex(a, I...)...)
111-
end
112-
113109
using LinearAlgebra: Transpose
114110
function parentindex_to_index(a::Transpose, I::CartesianIndex{2})
115111
return cartesianindex_reverse(I)
@@ -124,13 +120,30 @@ function value_to_parentvalue(a::Transpose, value)
124120
return transpose(value)
125121
end
126122

123+
function isstored_wrapped(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
124+
return isstored(parent(a), index_to_parentindex(a, I...)...)
125+
end
126+
127+
function isstored(a::Adjoint, I::Vararg{Int,2})
128+
return isstored_wrapped(a, I...)
129+
end
130+
function isstored(a::PermutedDimsArray{<:Any,N}, I::Vararg{Int,N}) where {N}
131+
return isstored_wrapped(a, I...)
132+
end
133+
function isstored(a::ReshapedArray{<:Any,N}, I::Vararg{Int,N}) where {N}
134+
return isstored_wrapped(a, I...)
135+
end
136+
function isstored(a::SubArray{<:Any,N}, I::Vararg{Int,N}) where {N}
137+
return isstored_wrapped(a, I...)
138+
end
139+
function isstored(a::Transpose, I::Vararg{Int,2})
140+
return isstored_wrapped(a, I...)
141+
end
142+
127143
# TODO: Turn these into `AbstractWrappedSparseArrayInterface` functions?
128144
for type in (:Adjoint, :PermutedDimsArray, :ReshapedArray, :SubArray, :Transpose)
129145
@eval begin
130146
@interface ::AbstractSparseArrayInterface storedvalues(a::$type) = storedparentvalues(a)
131-
@interface ::AbstractSparseArrayInterface function isstored(a::$type, I::Int...)
132-
return isstored(parent(a), index_to_parentindex(a, I...)...)
133-
end
134147
@interface ::AbstractSparseArrayInterface function eachstoredindex(a::$type)
135148
# TODO: Make lazy with `Iterators.map`.
136149
return map(collect(eachstoredparentindex(a))) do I
@@ -180,8 +193,9 @@ end
180193
@interface ::AbstractArrayInterface eachstoredindex(D::Diagonal) =
181194
_diagind(D, IndexCartesian())
182195

183-
@interface ::AbstractArrayInterface isstored(D::Diagonal, i::Int, j::Int) =
184-
i == j && Base.checkbounds(Bool, D, i, j)
196+
function isstored(D::Diagonal, i::Int, j::Int)
197+
return i == j && checkbounds(Bool, D, i, j)
198+
end
185199
@interface ::AbstractArrayInterface function getstoredindex(D::Diagonal, i::Int, j::Int)
186200
return D.diag[i]
187201
end

test/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ JLArrays = "0.2.0"
2323
LinearAlgebra = "<0.0.1, 1"
2424
Random = "<0.0.1, 1"
2525
SafeTestsets = "0.1.0"
26-
SparseArraysBase = "0.4.0"
26+
SparseArraysBase = "0.5.0"
2727
StableRNGs = "1.0.2"
2828
Suppressor = "0.2.8"
2929
Test = "<0.0.1, 1"

0 commit comments

Comments
 (0)