@@ -66,13 +66,13 @@ badjlist = [[1,2,5,6],[3,4,6]]
66
66
bg = BipartiteGraph(7, fadjlist, badjlist)
67
67
```
68
68
"""
69
- mutable struct BipartiteGraph{I<: Integer ,F<: Vector{Vector{I}} ,B<: Union{Vector{Vector{I}},Nothing } ,M} <: LightGraphs.AbstractGraph{I}
69
+ mutable struct BipartiteGraph{I<: Integer ,F<: Vector{Vector{I}} ,B<: Union{Vector{Vector{I}},I } ,M} <: LightGraphs.AbstractGraph{I}
70
70
ne:: Int
71
71
fadjlist:: F # `fadjlist[src] => dsts`
72
- badjlist:: B # `badjlist[dst] => srcs`
72
+ badjlist:: B # `badjlist[dst] => srcs` or `ndsts`
73
73
metadata:: M
74
74
end
75
- BipartiteGraph (ne:: Integer , fadj:: AbstractVector , badj:: Union{AbstractVector} = nothing ) = BipartiteGraph (ne, fadj, badj, nothing )
75
+ BipartiteGraph (ne:: Integer , fadj:: AbstractVector , badj:: Union{AbstractVector} = maximum (maximum, fadj); metadata = nothing ) = BipartiteGraph (ne, fadj, badj, metadata )
76
76
77
77
"""
78
78
```julia
@@ -84,9 +84,7 @@ Test whether two [`BipartiteGraph`](@ref)s are equal.
84
84
function Base. isequal (bg1:: BipartiteGraph{T} , bg2:: BipartiteGraph{T} ) where {T<: Integer }
85
85
iseq = (bg1. ne == bg2. ne)
86
86
iseq &= (bg1. fadjlist == bg2. fadjlist)
87
- if bg1. badjlist != = nothing && bg2. badjlist != = nothing
88
- iseq &= (bg1. badjlist == bg2. badjlist)
89
- end
87
+ iseq &= (bg1. badjlist == bg2. badjlist)
90
88
iseq
91
89
end
92
90
@@ -97,14 +95,18 @@ Build an empty `BipartiteGraph` with `nsrcs` sources and `ndsts` destinations.
97
95
"""
98
96
function BipartiteGraph (nsrcs:: T , ndsts:: T , backedge:: Val{B} = Val (true ); metadata= nothing ) where {T,B}
99
97
fadjlist = map (_-> T[], 1 : nsrcs)
100
- badjlist = B ? map (_-> T[], 1 : ndsts) : nothing
98
+ badjlist = B ? map (_-> T[], 1 : ndsts) : ndsts
101
99
BipartiteGraph (0 , fadjlist, badjlist, metadata)
102
100
end
103
101
104
102
Base. eltype (:: Type{<:BipartiteGraph{I}} ) where I = I
105
103
function Base. empty! (g:: BipartiteGraph )
106
104
foreach (empty!, g. fadjlist)
107
- g. badjlist === nothing || foreach (empty!, g. badjlist)
105
+ if g. badjlist isa AbstractVector
106
+ foreach (empty!, g. badjlist)
107
+ else
108
+ g. badjlist = 0
109
+ end
108
110
g. ne = 0
109
111
if g. metadata != = nothing
110
112
foreach (empty!, g. metadata)
@@ -121,12 +123,12 @@ end
121
123
LightGraphs. is_directed (:: Type{<:BipartiteGraph} ) = false
122
124
LightGraphs. vertices (g:: BipartiteGraph ) = (𝑠vertices (g), 𝑑vertices (g))
123
125
𝑠vertices (g:: BipartiteGraph ) = axes (g. fadjlist, 1 )
124
- 𝑑vertices (g:: BipartiteGraph ) = g. badjlist === nothing ? throw_no_back_edges ( ) : axes (g. badjlist, 1 )
126
+ 𝑑vertices (g:: BipartiteGraph ) = g. badjlist isa AbstractVector ? axes (g . badjlist, 1 ) : Base . OneTo (g. badjlist)
125
127
has_𝑠vertex (g:: BipartiteGraph , v:: Integer ) = v in 𝑠vertices (g)
126
128
has_𝑑vertex (g:: BipartiteGraph , v:: Integer ) = v in 𝑑vertices (g)
127
129
𝑠neighbors (g:: BipartiteGraph , i:: Integer , with_metadata:: Val{M} = Val (false )) where M = M ? zip (g. fadjlist[i], g. metadata[i]) : g. fadjlist[i]
128
130
function 𝑑neighbors (g:: BipartiteGraph , j:: Integer , with_metadata:: Val{M} = Val (false )) where M
129
- g. badjlist === nothing && throw_no_back_edges ()
131
+ g. badjlist isa AbstractVector || throw_no_back_edges ()
130
132
M ? zip (g. badjlist[j], (g. metadata[i][j] for i in g. badjlist[j])) : g. badjlist[j]
131
133
end
132
134
LightGraphs. ne (g:: BipartiteGraph ) = g. ne
@@ -152,7 +154,6 @@ const NO_METADATA = NoMetadata()
152
154
LightGraphs. add_edge! (g:: BipartiteGraph , i:: Integer , j:: Integer , md= NO_METADATA) = add_edge! (g, BipartiteEdge (i, j), md)
153
155
function LightGraphs. add_edge! (g:: BipartiteGraph , edge:: BipartiteEdge , md= NO_METADATA)
154
156
@unpack fadjlist, badjlist = g
155
- verts = vertices (g)
156
157
s, d = src (edge), dst (edge)
157
158
(has_𝑠vertex (g, s) && has_𝑑vertex (g, d)) || error (" edge ($edge ) out of range." )
158
159
@inbounds list = fadjlist[s]
@@ -164,7 +165,7 @@ function LightGraphs.add_edge!(g::BipartiteGraph, edge::BipartiteEdge, md=NO_MET
164
165
end
165
166
166
167
g. ne += 1
167
- if badjlist != = nothing
168
+ if badjlist isa AbstractVector
168
169
@inbounds list = badjlist[d]
169
170
index = searchsortedfirst (list, s)
170
171
insert! (list, index, s)
@@ -173,8 +174,12 @@ function LightGraphs.add_edge!(g::BipartiteGraph, edge::BipartiteEdge, md=NO_MET
173
174
end
174
175
175
176
function LightGraphs. add_vertex! (g:: BipartiteGraph{T} , type:: VertType ) where T
176
- if type === DST && g. badjlist != = nothing
177
- push! (g. badjlist, T[])
177
+ if type === DST
178
+ if g. badjlist isa AbstractVector
179
+ push! (g. badjlist, T[])
180
+ else
181
+ g. badjlist += 1
182
+ end
178
183
elseif type === SRC
179
184
push! (g. fadjlist, T[])
180
185
else
0 commit comments