Skip to content

Commit 1236822

Browse files
authored
Fix isstored with trailing singletons, vector show (#45)
1 parent 248dd87 commit 1236822

6 files changed

+36
-5
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.0"
4+
version = "0.4.1"
55

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

src/abstractsparsearray.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ using LinearAlgebra: LinearAlgebra
2828
@derive AnyAbstractSparseArray AbstractArrayOps
2929

3030
function Base.replace_in_print_matrix(
31-
A::AnyAbstractSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString
31+
a::AnyAbstractSparseVecOrMat, i::Integer, j::Integer, s::AbstractString
3232
)
33-
return isstored(A, CartesianIndex(i, j)) ? s : Base.replace_with_centered_mark(s)
33+
return isstored(a, i, j) ? s : Base.replace_with_centered_mark(s)
3434
end
3535

3636
# Special-purpose constructors

src/abstractsparsearrayinterface.jl

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Base: @_propagate_inbounds_meta
12
using DerivableInterfaces: DerivableInterfaces, @derive, @interface, AbstractArrayInterface
23

34
# This is to bring `ArrayLayouts.zero!` into the namespace
@@ -34,7 +35,23 @@ end
3435

3536
# Minimal interface for `SparseArrayInterface`.
3637
# Fallbacks for dense/non-sparse arrays.
37-
@interface ::AbstractArrayInterface isstored(a::AbstractArray, I::Int...) = true
38+
@interface ::AbstractArrayInterface function isstored(
39+
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
40+
) where {N}
41+
@_propagate_inbounds_meta
42+
@boundscheck checkbounds(a, I...)
43+
return true
44+
end
45+
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int)
46+
@_propagate_inbounds_meta
47+
return @interface interface isstored(a, Tuple(CartesianIndices(a)[I])...)
48+
end
49+
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int...)
50+
@_propagate_inbounds_meta
51+
@boundscheck checkbounds(a, I...)
52+
I′ = ntuple(i -> I[i], ndims(a))
53+
return @inbounds @interface interface isstored(a, I′...)
54+
end
3855
@interface ::AbstractArrayInterface eachstoredindex(a::AbstractArray) = eachindex(a)
3956
@interface ::AbstractArrayInterface getstoredindex(a::AbstractArray, I::Int...) =
4057
getindex(a, I...)
@@ -153,6 +170,12 @@ function Base.setindex!(a::StoredValues, value, I::Int)
153170
return setstoredindex!(a.array, value, a.storedindices[I])
154171
end
155172

173+
@interface ::AbstractSparseArrayInterface function isstored(
174+
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
175+
) where {N}
176+
return CartesianIndex(I) in eachstoredindex(a)
177+
end
178+
156179
@interface ::AbstractSparseArrayInterface storedvalues(a::AbstractArray) = StoredValues(a)
157180

158181
@interface ::AbstractSparseArrayInterface function eachstoredindex(

src/sparsearraydok.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Accessors: @set
2+
using DerivableInterfaces: @interface, interface
23
using Dictionaries: Dictionary, IndexError, set!
34

45
function getzero(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
@@ -74,7 +75,7 @@ Base.size(a::SparseArrayDOK) = a.size
7475

7576
storedvalues(a::SparseArrayDOK) = values(storage(a))
7677
function isstored(a::SparseArrayDOK, I::Int...)
77-
return CartesianIndex(I) in keys(storage(a))
78+
return @interface interface(a) isstored(a, I...)
7879
end
7980
function eachstoredindex(a::SparseArrayDOK)
8081
return keys(storage(a))

test/test_basics.jl

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ arrayts = (Array, JLArray)
2626
for indexstyle in (IndexLinear(), IndexCartesian())
2727
for I in eachindex(indexstyle, a)
2828
@test isstored(a, I)
29+
if indexstyle == IndexCartesian()
30+
@test isstored(a, Tuple(I)..., 1)
31+
end
2932
end
3033
end
3134
@test eachstoredindex(a) == eachindex(a)

test/test_sparsearraydok.jl

+4
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ arrayts = (Array,)
210210
a[1, 2] = 12
211211
@test sprint(show, "text/plain", a) ==
212212
"$(summary(a)):\n$(eltype(a)(12))\n ⋅ ⋅ "
213+
214+
a = SparseArrayDOK{elt}(undef, 2)
215+
a[1] = 1
216+
@test sprint(show, "text/plain", a) == "$(summary(a)):\n $(eltype(a)(1))\n"
213217
end
214218

215219
# Regression test for:

0 commit comments

Comments
 (0)