Skip to content

Commit ab10c08

Browse files
committed
simplify show implementation
1 parent 34551cf commit ab10c08

File tree

3 files changed

+6
-51
lines changed

3 files changed

+6
-51
lines changed

src/abstractsparsearray.jl

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,11 @@ using LinearAlgebra: LinearAlgebra
2525
# should go.
2626
@derive AnyAbstractSparseArray AbstractArrayOps
2727

28-
# This type alias is a temporary workaround since `@derive`
29-
# doesn't parse the `@MIME_str` macro properly at the moment.
30-
const MIMEtextplain = MIME"text/plain"
31-
@derive (T=AnyAbstractSparseArray,) begin
32-
Base.show(::IO, ::MIMEtextplain, ::T)
33-
end
34-
35-
# Wraps a sparse array but replaces the unstored values.
36-
# This is used in printing in order to customize printing
37-
# of zero/unstored values.
38-
struct ReplacedUnstoredSparseArray{T,N,F,Parent<:AbstractArray{T,N}} <:
39-
AbstractSparseArray{T,N}
40-
parent::Parent
41-
getunstoredindex::F
42-
end
43-
Base.parent(a::ReplacedUnstoredSparseArray) = a.parent
44-
Base.size(a::ReplacedUnstoredSparseArray) = size(parent(a))
45-
function isstored(a::ReplacedUnstoredSparseArray, I::Int...)
46-
return isstored(parent(a), I...)
47-
end
48-
function getstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
49-
return getstoredindex(parent(a), I...)
50-
end
51-
function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
52-
return a.getunstoredindex(a, I...)
28+
function Base.replace_in_print_matrix(
29+
A::AnyAbstractSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString
30+
)
31+
return isstored(A, CartesianIndex(i, j)) ? s : Base.replace_with_centered_mark(s)
5332
end
54-
eachstoredindex(a::ReplacedUnstoredSparseArray) = eachstoredindex(parent(a))
55-
@derive ReplacedUnstoredSparseArray AbstractArrayOps
5633

5734
# Special-purpose constructors
5835
# ----------------------------

src/abstractsparsearrayinterface.jl

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -384,26 +384,3 @@ struct SparseLayout <: AbstractSparseLayout end
384384
@interface ::AbstractSparseArrayInterface function ArrayLayouts.MemoryLayout(type::Type)
385385
return SparseLayout()
386386
end
387-
388-
# Like `Char` but prints without quotes.
389-
struct UnquotedChar <: AbstractChar
390-
char::Char
391-
end
392-
Base.show(io::IO, c::UnquotedChar) = print(io, c.char)
393-
Base.show(io::IO, ::MIME"text/plain", c::UnquotedChar) = show(io, c)
394-
395-
function getunstoredindex_show(a::AbstractArray, I::Int...)
396-
return UnquotedChar('')
397-
end
398-
399-
@interface ::AbstractSparseArrayInterface function Base.show(
400-
io::IO, mime::MIME"text/plain", a::AbstractArray
401-
)
402-
summary(io, a)
403-
isempty(a) && return nothing
404-
print(io, ":")
405-
println(io)
406-
a′ = ReplacedUnstoredSparseArray(a, getunstoredindex_show)
407-
Base.print_array(io, a′)
408-
return nothing
409-
end

test/test_sparsearraydok.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ arrayts = (Array,)
175175
# spacing so it isn't easy to make the test general.
176176
a = SparseArrayDOK{elt}(undef, 2, 2)
177177
a[1, 2] = 12
178-
@test sprint(show, "text/plain", a) == "$(summary(a)):\n$(eltype(a)(12))\n ⋅ ⋅"
178+
@test sprint(show, "text/plain", a) ==
179+
"$(summary(a)):\n$(eltype(a)(12))\n ⋅ ⋅ "
179180
end
180181

181182
# Regression test for:

0 commit comments

Comments
 (0)