Skip to content

Commit

Permalink
allocate less in limited sparse printing
Browse files Browse the repository at this point in the history
iterate and print in forwards order instead of saving to an array and reversing
use searchsortedfirst to find which column to start in

fix tests for 32 bit
  • Loading branch information
tkelman committed Jun 29, 2017
1 parent db9d70a commit ff72420
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
22 changes: 10 additions & 12 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)

function _format_line(r, col)
pad = ndigits(max(S.m, S.n))
print(ioc, " ", '[', rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
print(ioc, " [", rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
if isassigned(S.nzval, Int(r))
show(ioc, S.nzval[r])
else
Expand All @@ -171,19 +171,17 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)
count == print_count && break
end

lines_bottom = String[]
if !will_fit
count = 0
for col = reverse(1:S.n), r = reverse(nzrange(S, col))
count += 1
push!(lines_bottom, _format_line(r, col))
count == print_count && break
print(io, "\n \u22ee")
# find the column to start printing in for the last print_count elements
nextcol = searchsortedfirst(S.colptr, nnz(S) - print_count + 1)
for r = nnz(S) - print_count + 1 : S.colptr[nextcol] - 1
print(io, "\n", _format_line(r, nextcol - 1))
end
# print all of the remaining columns
for col = nextcol:S.n, r = nzrange(S, col)
print(io, "\n", _format_line(r, col))
end
end

if !will_fit
print(io, "\n ", '\u22ee', "\n")
print(io, join(reverse(lines_bottom), '\n'))
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ end
" = 1.0\n\n [5, 3] = 5.0")

show(ioc, MIME"text/plain"(), sparse(ones(5,3)))
@test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,Int64} with 15 stored entries:\n [1, 1]",
@test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,$Int} with 15 stored entries:\n [1, 1]",
" = 1.0\n\n [5, 3] = 1.0")

# odd number of rows
Expand All @@ -1804,7 +1804,7 @@ end
" = 1.0\n [2, 1] = 2.0\n\n [5, 3] = 5.0\n [6, 3] = 6.0")

show(ioc, MIME"text/plain"(), sparse(ones(6,3)))
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,Int64} with 18 stored entries:\n [1, 1]",
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,$Int} with 18 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 1.0\n\n [5, 3] = 1.0\n [6, 3] = 1.0")

ioc = IOContext(io, :displaysize => (9, 80))
Expand Down

0 comments on commit ff72420

Please sign in to comment.