Skip to content

Commit

Permalink
Add pretty-printing for BipartiteGraph
Browse files Browse the repository at this point in the history
Helps with debugging. Needs JuliaLang/julia#45751
for proper formatting, but works without it.
  • Loading branch information
Keno committed Jun 19, 2022
1 parent fd279c0 commit b1e8a7e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/bipartite_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,48 @@ function complete(g::BipartiteGraph{I}) where {I}
BipartiteGraph(g.ne, g.fadjlist, badjlist)
end

# Matrix whose only purpose is to pretty-print the bipartite graph
struct BipartiteAdjacencyList
u::Vector{Int}
end
function Base.show(io::IO, l::BipartiteAdjacencyList)
if isempty(l.u)
printstyled(io, '', color = :light_black)
else
print(io, l.u)
end
end

struct Label
s::String
end
Base.show(io::IO, l::Label) = print(io, l.s)

struct BipartiteGraphPrintMatrix <: AbstractMatrix{Union{Label, Int, BipartiteAdjacencyList}}
bpg::BipartiteGraph
end
Base.size(bgpm::BipartiteGraphPrintMatrix) = (length(bgpm.bpg.fadjlist)+1, 3)
function Base.getindex(bgpm::BipartiteGraphPrintMatrix, i::Integer, j::Integer)
checkbounds(bgpm, i, j)
if i == 1
return (Label.(("#", "src", "dst")))[j]
elseif j == 1
return i - 1
elseif j == 2
return BipartiteAdjacencyList(𝑠neighbors(bgpm.bpg, i - 1))
elseif j == 3
return BipartiteAdjacencyList(𝑑neighbors(bgpm.bpg, i - 1))
else
@assert false
end
end

function Base.show(io::IO, b::BipartiteGraph)
print(io, "BipartiteGraph with (", length(b.fadjlist), ", ",
isa(b.badjlist, Int) ? b.badjlist : length(b.badjlist), ") (𝑠,𝑑)-vertices\n")
Base.print_matrix(io, BipartiteGraphPrintMatrix(b))
end

"""
```julia
Base.isequal(bg1::BipartiteGraph{T}, bg2::BipartiteGraph{T}) where {T<:Integer}
Expand Down

0 comments on commit b1e8a7e

Please sign in to comment.