diff --git a/.gitignore b/.gitignore index ba39cc53..1209b4f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ Manifest.toml +docs/build diff --git a/src/sparsevector.jl b/src/sparsevector.jl index fc555e69..8cdf7d53 100644 --- a/src/sparsevector.jl +++ b/src/sparsevector.jl @@ -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 diff --git a/test/sparsevector.jl b/test/sparsevector.jl index 55bc94a1..814752db 100644 --- a/test/sparsevector.jl +++ b/test/sparsevector.jl @@ -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