Skip to content

Commit 5a9c162

Browse files
authored
Better printing to show unstored values (#13)
1 parent b4f5a37 commit 5a9c162

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
name = "SparseArraysBase"
22
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.3"
4+
version = "0.2.4"
55

66
[deps]
7+
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
78
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
89
DerivableInterfaces = "6c5e35bf-e59e-4898-b73c-732dcc4ba65f"
910
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
1011
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1112
MapBroadcast = "ebd9b9da-f48d-417c-9660-449667d60261"
1213

1314
[compat]
15+
Accessors = "0.1.41"
1416
Aqua = "0.8.9"
1517
ArrayLayouts = "1.11.0"
1618
DerivableInterfaces = "0.3.7"

src/abstractsparsearray.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,28 @@ using LinearAlgebra: LinearAlgebra
2424
# which is where matrix multiplication and factorizations
2525
# should go.
2626
@derive AnyAbstractSparseArray AbstractArrayOps
27+
const MIMEtextplain = MIME"text/plain"
28+
@derive (T=AnyAbstractSparseArray,) begin
29+
Base.show(::IO, ::MIMEtextplain, ::T)
30+
end
31+
32+
# Wraps a sparse array but replaces the unstored values.
33+
# This is used in printing in order to customize printing
34+
# of zero/unstored values.
35+
struct ReplacedUnstoredSparseArray{T,N,F,Parent<:AbstractArray{T,N}} <:
36+
AbstractSparseArray{T,N}
37+
parent::Parent
38+
getunstoredindex::F
39+
end
40+
Base.parent(a::ReplacedUnstoredSparseArray) = a.parent
41+
Base.size(a::ReplacedUnstoredSparseArray) = size(parent(a))
42+
function isstored(a::ReplacedUnstoredSparseArray, I::Int...)
43+
return isstored(parent(a), I...)
44+
end
45+
function getstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
46+
return getstoredindex(parent(a), I...)
47+
end
48+
function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
49+
return a.getunstoredindex(a, I...)
50+
end
51+
@derive ReplacedUnstoredSparseArray AbstractArrayOps

src/abstractsparsearrayinterface.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function storedlength end
1414
function storedpairs end
1515
function storedvalues end
1616

17+
# Replace the function for accessing
18+
# unstored values.
19+
function set_getunstoredindex end
20+
1721
# Generic functionality for converting to a
1822
# dense array, trying to preserve information
1923
# about the array (such as which device it is on).
@@ -376,3 +380,26 @@ struct SparseLayout <: AbstractSparseLayout end
376380
@interface ::AbstractSparseArrayInterface function ArrayLayouts.MemoryLayout(type::Type)
377381
return SparseLayout()
378382
end
383+
384+
# Like `Char` but prints without quotes.
385+
struct UnquotedChar <: AbstractChar
386+
char::Char
387+
end
388+
Base.show(io::IO, c::UnquotedChar) = print(io, c.char)
389+
Base.show(io::IO, ::MIME"text/plain", c::UnquotedChar) = show(io, c)
390+
391+
function show_getunstoredindex(a::AbstractArray, I::Int...)
392+
return UnquotedChar('')
393+
end
394+
395+
@interface ::AbstractSparseArrayInterface function Base.show(
396+
io::IO, mime::MIME"text/plain", a::AbstractArray
397+
)
398+
summary(io, a)
399+
isempty(a) && return nothing
400+
print(io, ":")
401+
println(io)
402+
a′ = ReplacedUnstoredSparseArray(a, show_getunstoredindex)
403+
Base.print_array(io, a′)
404+
return nothing
405+
end

src/sparsearraydok.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Accessors: @set
12
using Dictionaries: Dictionary, IndexError, set!
23

34
function default_getunstoredindex(a::AbstractArray, I::Int...)
@@ -10,6 +11,11 @@ struct SparseArrayDOK{T,N,F} <: AbstractSparseArray{T,N}
1011
getunstoredindex::F
1112
end
1213

14+
function set_getunstoredindex(a::SparseArrayDOK, f)
15+
@set a.getunstoredindex = f
16+
return a
17+
end
18+
1319
using DerivableInterfaces: DerivableInterfaces
1420
# This defines the destination type of various operations in DerivableInterfaces.jl.
1521
DerivableInterfaces.arraytype(::AbstractSparseArrayInterface, T::Type) = SparseArrayDOK{T}

test/basics/test_sparsearraydok.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,11 @@ arrayts = (Array,)
168168
@test storedlength(b) == 2
169169
@test b[1, 2] == 12
170170
@test b[4, 3] == 21
171+
172+
# Printing
173+
# Not testing other element types since they change the
174+
# spacing so it isn't easy to make the test general.
175+
a = SparseArrayDOK{Float64}(2, 2)
176+
a[1, 2] = 12
177+
@test sprint(show, "text/plain", a) == "$(summary(a)):\n$(eltype(a)(12))\n ⋅ ⋅"
171178
end

0 commit comments

Comments
 (0)