Skip to content

Commit 4e94eea

Browse files
authored
Define isstored for SubArray (#44)
1 parent d741b7f commit 4e94eea

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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.3.1"
4+
version = "0.3.2"
55

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

src/wrappers.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ 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+
109113
using LinearAlgebra: Transpose
110114
function parentindex_to_index(a::Transpose, I::CartesianIndex{2})
111115
return cartesianindex_reverse(I)

test/test_basics.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using SparseArraysBase:
1111
storedlength,
1212
storedpairs,
1313
storedvalues
14-
using Test: @test, @testset
14+
using Test: @test, @test_throws, @testset
1515

1616
elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
1717
arrayts = (Array, JLArray)
@@ -43,6 +43,15 @@ arrayts = (Array, JLArray)
4343
@test iszero(getunstoredindex(a, I))
4444
end
4545

46+
n = 2
47+
a = @view dev(randn(elt, n, n))[1:2, 1]
48+
@test storedlength(a) == length(a)
49+
for indexstyle in (IndexLinear(), IndexCartesian())
50+
for I in eachindex(indexstyle, a)
51+
@test isstored(a, I)
52+
end
53+
end
54+
4655
a = dev(randn(elt, n, n))
4756
for I in ((1, 2), (CartesianIndex(1, 2),))
4857
b = copy(a)

test/test_sparsearraydok.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ arrayts = (Array,)
5050
@test b == [0 24; 0 0]
5151
@test storedlength(b) == 1
5252

53+
# isstored
54+
a = SparseArrayDOK{elt}(undef, 4, 4)
55+
a[2, 3] = 23
56+
for I in CartesianIndices(a)
57+
if I == CartesianIndex(2, 3)
58+
@test isstored(a, I)
59+
@test isstored(a, Tuple(I)...)
60+
else
61+
@test !isstored(a, I)
62+
@test !isstored(a, Tuple(I)...)
63+
end
64+
end
65+
66+
# isstored SubArray
67+
a′ = SparseArrayDOK{elt}(undef, 4, 4)
68+
a′[2, 3] = 23
69+
a = @view a′[2:3, 2:3]
70+
for I in CartesianIndices(a)
71+
if I == CartesianIndex(1, 2)
72+
@test isstored(a, I)
73+
@test isstored(a, Tuple(I)...)
74+
else
75+
@test !isstored(a, I)
76+
@test !isstored(a, Tuple(I)...)
77+
end
78+
end
79+
5380
a = SparseArrayDOK{elt}(undef, 3, 3, 3)
5481
a[1, 2, 3] = 123
5582
b = permutedims(a, (2, 3, 1))

0 commit comments

Comments
 (0)