Skip to content

Improve isstored logic #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2025
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SparseArraysBase"
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.4.1"
version = "0.5.0"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
Dictionaries = "0.4.4"
Documenter = "1.8.1"
Literate = "2.20.1"
SparseArraysBase = "0.4.0"
SparseArraysBase = "0.5.0"
2 changes: 1 addition & 1 deletion examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Dictionaries = "0.4.4"
SparseArraysBase = "0.4.0"
SparseArraysBase = "0.5.0"
Test = "<0.0.1, 1"
14 changes: 6 additions & 8 deletions src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,22 @@ end

# Minimal interface for `SparseArrayInterface`.
# Fallbacks for dense/non-sparse arrays.
@interface ::AbstractArrayInterface function isstored(
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
function isstored(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
@_propagate_inbounds_meta
@boundscheck checkbounds(a, I...)
return true
end
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int)
function isstored(a::AbstractArray, I::Int)
@_propagate_inbounds_meta
return @interface interface isstored(a, Tuple(CartesianIndices(a)[I])...)
return isstored(a, Tuple(CartesianIndices(a)[I])...)
end
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int...)
function isstored(a::AbstractArray, I::Int...)
@_propagate_inbounds_meta
@boundscheck checkbounds(a, I...)
I′ = ntuple(i -> I[i], ndims(a))
return @inbounds @interface interface isstored(a, I′...)
return isstored(a, I′...)
end

@interface ::AbstractArrayInterface eachstoredindex(a::AbstractArray) = eachindex(a)
@interface ::AbstractArrayInterface getstoredindex(a::AbstractArray, I::Int...) =
getindex(a, I...)
Expand Down Expand Up @@ -111,7 +110,6 @@ end
SparseArraysBase.eachstoredindex(::T...)
SparseArraysBase.getstoredindex(::T, ::Int...)
SparseArraysBase.getunstoredindex(::T, ::Int...)
SparseArraysBase.isstored(::T, ::Int...)
SparseArraysBase.setstoredindex!(::T, ::Any, ::Int...)
SparseArraysBase.setunstoredindex!(::T, ::Any, ::Int...)
SparseArraysBase.storedlength(::T)
Expand Down
2 changes: 1 addition & 1 deletion src/sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ storage(a::SparseArrayDOK) = a.storage
Base.size(a::SparseArrayDOK) = a.size

storedvalues(a::SparseArrayDOK) = values(storage(a))
function isstored(a::SparseArrayDOK, I::Int...)
function isstored(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
return @interface interface(a) isstored(a, I...)
end
function eachstoredindex(a::SparseArrayDOK)
Expand Down
32 changes: 23 additions & 9 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@
return StoredValues(parent(a), collect(eachstoredparentindex(a)))
end

@interface ::AbstractArrayInterface function isstored(a::SubArray, I::Int...)
return isstored(parent(a), index_to_parentindex(a, I...)...)
end

using LinearAlgebra: Transpose
function parentindex_to_index(a::Transpose, I::CartesianIndex{2})
return cartesianindex_reverse(I)
Expand All @@ -124,13 +120,30 @@
return transpose(value)
end

function isstored_wrapped(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
return isstored(parent(a), index_to_parentindex(a, I...)...)
end

function isstored(a::Adjoint, I::Vararg{Int,2})
return isstored_wrapped(a, I...)
end
function isstored(a::PermutedDimsArray{<:Any,N}, I::Vararg{Int,N}) where {N}
return isstored_wrapped(a, I...)
end
function isstored(a::ReshapedArray{<:Any,N}, I::Vararg{Int,N}) where {N}
return isstored_wrapped(a, I...)

Check warning on line 134 in src/wrappers.jl

View check run for this annotation

Codecov / codecov/patch

src/wrappers.jl#L133-L134

Added lines #L133 - L134 were not covered by tests
end
function isstored(a::SubArray{<:Any,N}, I::Vararg{Int,N}) where {N}
return isstored_wrapped(a, I...)
end
function isstored(a::Transpose, I::Vararg{Int,2})
return isstored_wrapped(a, I...)

Check warning on line 140 in src/wrappers.jl

View check run for this annotation

Codecov / codecov/patch

src/wrappers.jl#L139-L140

Added lines #L139 - L140 were not covered by tests
end

# TODO: Turn these into `AbstractWrappedSparseArrayInterface` functions?
for type in (:Adjoint, :PermutedDimsArray, :ReshapedArray, :SubArray, :Transpose)
@eval begin
@interface ::AbstractSparseArrayInterface storedvalues(a::$type) = storedparentvalues(a)
@interface ::AbstractSparseArrayInterface function isstored(a::$type, I::Int...)
return isstored(parent(a), index_to_parentindex(a, I...)...)
end
@interface ::AbstractSparseArrayInterface function eachstoredindex(a::$type)
# TODO: Make lazy with `Iterators.map`.
return map(collect(eachstoredparentindex(a))) do I
Expand Down Expand Up @@ -180,8 +193,9 @@
@interface ::AbstractArrayInterface eachstoredindex(D::Diagonal) =
_diagind(D, IndexCartesian())

@interface ::AbstractArrayInterface isstored(D::Diagonal, i::Int, j::Int) =
i == j && Base.checkbounds(Bool, D, i, j)
function isstored(D::Diagonal, i::Int, j::Int)
return i == j && checkbounds(Bool, D, i, j)
end
@interface ::AbstractArrayInterface function getstoredindex(D::Diagonal, i::Int, j::Int)
return D.diag[i]
end
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ JLArrays = "0.2.0"
LinearAlgebra = "<0.0.1, 1"
Random = "<0.0.1, 1"
SafeTestsets = "0.1.0"
SparseArraysBase = "0.4.0"
SparseArraysBase = "0.5.0"
StableRNGs = "1.0.2"
Suppressor = "0.2.8"
Test = "<0.0.1, 1"
Loading