Skip to content

Simplify show implementation #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,11 @@ using LinearAlgebra: LinearAlgebra
# should go.
@derive AnyAbstractSparseArray AbstractArrayOps

# This type alias is a temporary workaround since `@derive`
# doesn't parse the `@MIME_str` macro properly at the moment.
const MIMEtextplain = MIME"text/plain"
@derive (T=AnyAbstractSparseArray,) begin
Base.show(::IO, ::MIMEtextplain, ::T)
end

# Wraps a sparse array but replaces the unstored values.
# This is used in printing in order to customize printing
# of zero/unstored values.
struct ReplacedUnstoredSparseArray{T,N,F,Parent<:AbstractArray{T,N}} <:
AbstractSparseArray{T,N}
parent::Parent
getunstoredindex::F
end
Base.parent(a::ReplacedUnstoredSparseArray) = a.parent
Base.size(a::ReplacedUnstoredSparseArray) = size(parent(a))
function isstored(a::ReplacedUnstoredSparseArray, I::Int...)
return isstored(parent(a), I...)
end
function getstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
return getstoredindex(parent(a), I...)
end
function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
return a.getunstoredindex(a, I...)
function Base.replace_in_print_matrix(
A::AnyAbstractSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString
)
return isstored(A, CartesianIndex(i, j)) ? s : Base.replace_with_centered_mark(s)
end
eachstoredindex(a::ReplacedUnstoredSparseArray) = eachstoredindex(parent(a))
@derive ReplacedUnstoredSparseArray AbstractArrayOps

# Special-purpose constructors
# ----------------------------
Expand Down
23 changes: 0 additions & 23 deletions src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,26 +384,3 @@ struct SparseLayout <: AbstractSparseLayout end
@interface ::AbstractSparseArrayInterface function ArrayLayouts.MemoryLayout(type::Type)
return SparseLayout()
end

# Like `Char` but prints without quotes.
struct UnquotedChar <: AbstractChar
char::Char
end
Base.show(io::IO, c::UnquotedChar) = print(io, c.char)
Base.show(io::IO, ::MIME"text/plain", c::UnquotedChar) = show(io, c)

function getunstoredindex_show(a::AbstractArray, I::Int...)
return UnquotedChar('⋅')
end

@interface ::AbstractSparseArrayInterface function Base.show(
io::IO, mime::MIME"text/plain", a::AbstractArray
)
summary(io, a)
isempty(a) && return nothing
print(io, ":")
println(io)
a′ = ReplacedUnstoredSparseArray(a, getunstoredindex_show)
Base.print_array(io, a′)
return nothing
end
3 changes: 2 additions & 1 deletion test/test_sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ arrayts = (Array,)
# spacing so it isn't easy to make the test general.
a = SparseArrayDOK{elt}(undef, 2, 2)
a[1, 2] = 12
@test sprint(show, "text/plain", a) == "$(summary(a)):\n ⋅ $(eltype(a)(12))\n ⋅ ⋅"
@test sprint(show, "text/plain", a) ==
"$(summary(a)):\n ⋅ $(eltype(a)(12))\n ⋅ ⋅ "
end

# Regression test for:
Expand Down
Loading