Skip to content
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

One-line show for SparseVector #427

Merged
merged 22 commits into from
Oct 11, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Manifest.toml
docs/build
40 changes: 18 additions & 22 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1037,43 +1037,39 @@ end

### show and friends

function show(io::IO, x::AbstractSparseVector)
print(io, "sparsevec(", rowvals(x), ", ", nonzeros(x), ", ", length(x), ")")
end
function show(io::IO, ::MIME"text/plain", x::AbstractSparseVector)
xnnz = length(nonzeros(x))
nzind = rowvals(x)
nzval = nonzeros(x)
xnnz = length(nzval)
print(io, length(x), "-element ", typeof(x), " with ", xnnz,
" stored ", xnnz == 1 ? "entry" : "entries")
if xnnz != 0
println(io, ":")
show(IOContext(io, :typeinfo => eltype(x)), x)
end
end

show(io::IO, x::AbstractSparseVector) = show(convert(IOContext, io), x)
function show(io::IOContext, x::AbstractSparseVector)
# TODO: make this a one-line form
nzind = nonzeroinds(x)
nzval = nonzeros(x)
if isempty(nzind)
return show(io, MIME("text/plain"), x)
end
limit = get(io, :limit, false)::Bool
half_screen_rows = limit ? div(displaysize(io)[1] - 8, 2) : typemax(Int)
pad = ndigits(nzind[end])
if !haskey(io, :compact)
io = IOContext(io, :compact => true)
ioctxt = IOContext(io, :typeinfo => eltype(x))
limit = get(ioctxt, :limit, false)::Bool
half_screen_rows = limit ? div(displaysize(ioctxt)[1] - 8, 2) : typemax(Int)
pad = isempty(nzind) ? 0 : ndigits(nzind[end])
if !haskey(ioctxt, :compact)
ioctxt = IOContext(ioctxt, :compact => true)
end
for k = eachindex(nzind)
if k < half_screen_rows || k > length(nzind) - half_screen_rows
print(io, " ", '[', rpad(nzind[k], pad), "] = ")
print(ioctxt, " ", '[', rpad(nzind[k], pad), "] = ")
if isassigned(nzval, Int(k))
show(io, nzval[k])
show(ioctxt, nzval[k])
else
print(io, Base.undef_ref_str)
print(ioctxt, Base.undef_ref_str)
end
k != length(nzind) && println(io)
k != length(nzind) && println(ioctxt)
elseif k == half_screen_rows
println(io, " ", " "^pad, " \u22ee")
println(ioctxt, " ", " "^pad, " \u22ee")
end
end
return nothing
end

### Conversion to matrix
Expand Down
12 changes: 11 additions & 1 deletion test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1563,10 +1563,20 @@ mutable struct t20488 end
show(io, MIME"text/plain"(), spzeros(Float64, Int64, 2))
@test String(take!(io)) == "2-element $(SparseArrays.SparseVector){Float64, Int64} with 0 stored entries"
show(io, similar(sparsevec(rand(3) .+ 0.1), t20488))
@test String(take!(io)) == " [1] = #undef\n [2] = #undef\n [3] = #undef"
@test String(take!(io)) == "sparsevec([1, 2, 3], $t20488[#undef, #undef, #undef], 3)"
show(io, MIME"text/plain"(), similar(sparsevec(rand(3) .+ 0.1), t20488))
@test String(take!(io)) == "3-element SparseVector{$t20488, $Int} with 3 stored entries:\n [1] = #undef\n [2] = #undef\n [3] = #undef"
# Test that we don't introduce unnecessary padding for long sparse arrays
show(io, MIME"text/plain"(), SparseVector(div(typemax(Int32), 2), Int32[1], Int32[1]))
@test String(take!(io)) == "1073741823-element $(SparseArrays.SparseVector){Int32, Int32} with 1 stored entry:\n [1] = 1"
# ensure that :limit=>true leads to truncation
show(IOContext(io, :limit=>true), MIME"text/plain"(), sparsevec([1:20;], [1:20;]))
@test contains(String(take!(io)), "\n \u22ee\n")

# ensure that a vector of sparsevecs doesn't use pretty printing for elements
S = sparsevec(Int64[1,4], Int64[2,3])
@test repr(S) == "sparsevec($(Int64[1,4]), $(Int64[2,3]), 4)"
@test repr([S]) == "$(SparseArrays.SparseVector){Int64, Int64}[$(repr(S))]"
end

@testset "spzeros with index type" begin
Expand Down