Skip to content

Commit 06e865d

Browse files
committed
Add verification to ValGraphCollection constructor
1 parent 1499cef commit 06e865d

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

src/valgraphcollection.jl

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,73 @@ struct ValGraphCollection{V <: Integer, V_VALS <: AbstractTuple, E_VALS <: Abstr
3434

3535
function ValGraphCollection(graph_ids, vertex_ids, edge_ids, vertexvals, edgevals, graphvals)
3636

37-
@assert length(graph_ids) == length(graphvals) + 1
38-
@assert length(vertex_ids) == length(vertexvals) + 1
39-
@assert length(edge_ids) == length(edgevals)
40-
41-
# TODO more invariant checks should be done here
42-
4337
V = eltype(edge_ids)
4438
V_VALS = eltype(vertexvals)
4539
E_VALS = eltype(edgevals)
4640
G_VALS = eltype(graphvals)
4741

48-
return new{V, V_VALS, E_VALS, G_VALS}(graph_ids, vertex_ids, edge_ids, vertexvals, edgevals, graphvals)
42+
result = new{V, V_VALS, E_VALS, G_VALS}(graph_ids, vertex_ids, edge_ids, vertexvals, edgevals, graphvals)
43+
_verify(result)
44+
return result
45+
end
46+
end
47+
48+
function _verify(coll::ValGraphCollection{V, V_VALS, E_VALS, G_VALS}) where {V, V_VALS, E_VALS, G_VALS}
49+
50+
graph_ids = coll.graph_ids
51+
vertex_ids = coll.vertex_ids
52+
edge_ids = coll.edge_ids
53+
vertexvals = coll.vertexvals
54+
edgevals = coll.edgevals
55+
graphvals = coll.graphvals
56+
57+
# when there is no edge
58+
if length(coll.graph_ids) <= 1
59+
@assert length(vertex_ids) == 0
60+
@assert length(edge_ids) == 0
61+
@assert length(vertevals) == 0
62+
@assert length(edgevals) == 0
63+
@assert length(graphvals) == 0
64+
65+
return nothing
4966
end
5067

68+
@assert length(graph_ids) == length(graphvals) + 1
69+
@assert length(vertex_ids) == length(vertexvals) + 1
70+
@assert length(edge_ids) == length(edgevals)
71+
72+
@assert length(graph_ids) >= 1
73+
@assert graph_ids[begin] >= 1
74+
@assert graph_ids[end] == length(vertex_ids)
75+
@assert issorted(graph_ids)
76+
77+
@assert issorted(vertex_ids)
5178

79+
N = length(graph_ids) - 1
80+
for k in 1:N
81+
v1 = graph_ids[k]
82+
v2 = graph_ids[k + 1]
83+
84+
nvg = v2 - v1
85+
86+
edges = Vector{@NamedTuple{src::Int, dst::Int, edge_index::Int}}()
87+
88+
for v in v1:v2-1
89+
90+
for e_id in vertex_ids[v]:vertex_ids[v+1]-1
91+
@assert edge_ids[e_id] 1:nvg
92+
end
93+
94+
# TODO we should also verify here that the graph contains reverse edges
95+
# and that the weights of the reverse edges are the same
96+
end
97+
98+
end
99+
100+
return nothing
52101
end
53102

103+
54104
## ----------------------------------------------
55105
## ng
56106
## ----------------------------------------------
@@ -197,7 +247,7 @@ function outneighbors(g::ValGraphCollectionView, u::Integer)
197247
v_id1 = g.collection.vertex_ids[graph_id + (u - 1)]
198248
v_id2 = g.collection.vertex_ids[graph_id + u]
199249

200-
# TODO maybe we should return an immutable view
250+
# TODO maybe return an immutable view
201251
return @view g.collection.edge_ids[v_id1:v_id2-1]
202252
end
203253

@@ -208,7 +258,7 @@ function outedgevals(g::ValGraphCollectionView, u::Integer, ::Colon)
208258
v_id1 = g.collection.vertex_ids[graph_id + (u - 1)]
209259
v_id2 = g.collection.vertex_ids[graph_id + u]
210260

211-
# TODO maybe we should return an immutable view
261+
# TODO maybe return an immutable view
212262
return @view g.collection.edgevals[v_id1:v_id2-1]
213263
end
214264

@@ -241,7 +291,7 @@ function SimpleGraph{T}(gv::ValGraphCollectionView) where {T}
241291
end
242292

243293
ValGraph(gv::ValGraphCollectionView) = ValGraph{eltype(gv)}(gv)
244-
# TODO this ugly constructor should probably be moved to SimpleValueGraphs.jl
294+
# TODO this ugly constructor should be moved to SimpleValueGraphs.jl
245295
function ValGraph{V}(gv::ValGraphCollectionView) where {V}
246296

247297
nvg = Int(nv(gv))

0 commit comments

Comments
 (0)