Skip to content

Commit

Permalink
Show inds and axes consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Apr 5, 2024
1 parent e130fc0 commit ba819b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,23 @@ end
# show
_maybesize(t::Tuple{Base.OneTo{Int}, Vararg{Base.OneTo{Int}}}) = size.(t,1)
_maybesize(t) = t
Base.show(io::IO, A::OneElement) = print(io, OneElement, "(", A.val, ", ", A.ind, ", ", _maybesize(axes(A)), ")")
function Base.show(io::IO, A::OneElement{<:Any,1,Tuple{Int},Tuple{Base.OneTo{Int}}})
function Base.show(io::IO, @nospecialize(A::OneElement))
# We always print the inds and axes (or size, for Base.OneTo axes)
# We print the value only if it isn't 1
# this way, we have at least two arguments displayed that are unambiguous
print(io, OneElement)
if eltype(A) != Int
isvector = ndims(A) == 1
sz = _maybesize(axes(A))
hasstandardaxes = sz isa Tuple{Vararg{Integer}}
isstandardvector = isvector & hasstandardaxes
if hasstandardaxes && eltype(A) != Int && isone(A.val)
print(io, "{", eltype(A), "}")
end
print(io, "(")
if !isone(A.val)
if !(hasstandardaxes && isone(A.val))
print(io, A.val, ", ")
end
print(io, A.ind[1])
if A.ind[1] != size(A,1)
print(io, ", ", size(A,1))
end
print(io, isstandardvector ? A.ind[1] : A.ind, ", ")
print(io, isstandardvector ? sz[1] : sz)
print(io, ")")
end
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,31 @@ end
@test repr(B) == "OneElement(2, 1, 3)"
B = OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))
@test repr(B) == "OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))"

B = OneElement(2.0, (1, 2), (3, 4))
@test repr(B) == "OneElement(2.0, (1, 2), (3, 4))"
B = OneElement(2.0, 1, 3)
@test repr(B) == "OneElement(2.0, 1, 3)"
B = OneElement(2.0, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))
@test repr(B) == "OneElement(2.0, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))"

B = OneElement((1, 2), (3, 4))
@test repr(B) == "OneElement((1, 2), (3, 4))"
B = OneElement((1, 2))
@test repr(B) == "OneElement((1, 2), (1, 2))"
B = OneElement(2, 3)
@test repr(B) == "OneElement(2, 3)"
B = OneElement(2)
@test repr(B) == "OneElement(2, 2)"

B = OneElement{Bool}((1, 2), (3, 4))
@test repr(B) == "OneElement{Bool}((1, 2), (3, 4))"
B = OneElement(1.0, (1, 2), (3, 4))
@test repr(B) == "OneElement{Float64}((1, 2), (3, 4))"
B = OneElement{Bool}(2, 3)
@test repr(B) == "OneElement{Bool}(2, 3)"
B = OneElement(1.0, 2, 3)
@test repr(B) == "OneElement{Float64}(2, 3)"
end
end

Expand Down

0 comments on commit ba819b8

Please sign in to comment.