diff --git a/Project.toml b/Project.toml index 8a0ae2a..e533e14 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SparseArraysBase" uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208" authors = ["ITensor developers and contributors"] -version = "0.3.1" +version = "0.3.2" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/src/wrappers.jl b/src/wrappers.jl index bf4fc78..f35b1a1 100644 --- a/src/wrappers.jl +++ b/src/wrappers.jl @@ -106,6 +106,10 @@ function storedparentvalues(a::SubArray) 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) diff --git a/test/test_basics.jl b/test/test_basics.jl index d968acd..d434504 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -11,7 +11,7 @@ using SparseArraysBase: storedlength, storedpairs, storedvalues -using Test: @test, @testset +using Test: @test, @test_throws, @testset elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) arrayts = (Array, JLArray) @@ -43,6 +43,15 @@ arrayts = (Array, JLArray) @test iszero(getunstoredindex(a, I)) end + n = 2 + a = @view dev(randn(elt, n, n))[1:2, 1] + @test storedlength(a) == length(a) + for indexstyle in (IndexLinear(), IndexCartesian()) + for I in eachindex(indexstyle, a) + @test isstored(a, I) + end + end + a = dev(randn(elt, n, n)) for I in ((1, 2), (CartesianIndex(1, 2),)) b = copy(a) diff --git a/test/test_sparsearraydok.jl b/test/test_sparsearraydok.jl index f32cadd..8409958 100644 --- a/test/test_sparsearraydok.jl +++ b/test/test_sparsearraydok.jl @@ -50,6 +50,33 @@ arrayts = (Array,) @test b == [0 24; 0 0] @test storedlength(b) == 1 + # isstored + a = SparseArrayDOK{elt}(undef, 4, 4) + a[2, 3] = 23 + for I in CartesianIndices(a) + if I == CartesianIndex(2, 3) + @test isstored(a, I) + @test isstored(a, Tuple(I)...) + else + @test !isstored(a, I) + @test !isstored(a, Tuple(I)...) + end + end + + # isstored SubArray + a′ = SparseArrayDOK{elt}(undef, 4, 4) + a′[2, 3] = 23 + a = @view a′[2:3, 2:3] + for I in CartesianIndices(a) + if I == CartesianIndex(1, 2) + @test isstored(a, I) + @test isstored(a, Tuple(I)...) + else + @test !isstored(a, I) + @test !isstored(a, Tuple(I)...) + end + end + a = SparseArrayDOK{elt}(undef, 3, 3, 3) a[1, 2, 3] = 123 b = permutedims(a, (2, 3, 1))