Skip to content

Commit 69aa23b

Browse files
committed
Use @eval loop in place of union type AbstractVertexOrEdgeDataGraph.
1 parent d6a58e0 commit 69aa23b

1 file changed

Lines changed: 71 additions & 70 deletions

File tree

src/abstractedgeorvertexdatagraph.jl

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,78 @@ using NamedGraphs: NamedGraphs, Vertices, similar_graph, subgraph_edges, to_grap
55
abstract type AbstractVertexDataGraph{T, V} <: AbstractDataGraph{V, T, Nothing} end
66
abstract type AbstractEdgeDataGraph{T, V} <: AbstractDataGraph{V, Nothing, T} end
77

8-
const AbstractVertexOrEdgeDataGraph{T, V} =
9-
Union{AbstractVertexDataGraph{T, V}, AbstractEdgeDataGraph{T, V}}
10-
11-
Graphs.edgetype(graph::AbstractVertexOrEdgeDataGraph) = edgetype(typeof(graph))
12-
13-
function NamedGraphs.similar_graph(
14-
graph::AbstractVertexOrEdgeDataGraph
15-
)
16-
return similar_graph(graph, valtype(graph))
17-
end
18-
19-
function NamedGraphs.similar_graph(
20-
graph::AbstractVertexOrEdgeDataGraph,
21-
vertices
22-
)
23-
return similar_graph(graph, valtype(graph), vertices)
24-
end
25-
26-
function Base.copy(graph::AbstractVertexOrEdgeDataGraph)
27-
graph_dst = similar_graph(graph)
28-
# Allow copies of graphs with undefined data.
29-
copyto!(graph_dst, graph, filter(key -> isassigned(graph, key), keys(graph)))
30-
return graph_dst
31-
end
32-
33-
function Base.copyto!(graph_dst::AbstractVertexOrEdgeDataGraph, src)
34-
copyto!(graph_dst, src, keys(src))
35-
return graph_dst
36-
end
37-
38-
Base.iterate(graph::AbstractVertexOrEdgeDataGraph) = iterate(index_data(graph))
39-
function Base.iterate(graph::AbstractVertexOrEdgeDataGraph, state)
40-
return iterate(index_data(graph), state)
41-
end
42-
43-
Base.keytype(graph::AbstractVertexOrEdgeDataGraph) = keytype(typeof(graph))
44-
45-
Base.valtype(graph::AbstractVertexOrEdgeDataGraph) = valtype(typeof(graph))
46-
Base.valtype(::Type{<:AbstractVertexOrEdgeDataGraph{T}}) where {T} = T
47-
48-
Base.eltype(graph::AbstractVertexOrEdgeDataGraph) = eltype(typeof(graph))
49-
Base.eltype(::Type{<:AbstractVertexOrEdgeDataGraph{T}}) where {T} = T
50-
51-
Base.length(graph::AbstractVertexOrEdgeDataGraph) = length(index_data(graph))
52-
Base.keys(graph::AbstractVertexOrEdgeDataGraph) = keys(index_data(graph))
53-
Base.values(graph::AbstractVertexOrEdgeDataGraph) = values(index_data(graph))
54-
55-
Dictionaries.issettable(::AbstractVertexOrEdgeDataGraph) = true
56-
Dictionaries.isinsertable(::AbstractVertexOrEdgeDataGraph) = false
57-
58-
function Base.insert!(graph::AbstractVertexOrEdgeDataGraph, ind, data)
59-
isinsertable(graph) || throw(ArgumentError("Graph does not support insertion."))
60-
insert!_datagraph(graph, to_graph_index(graph, ind), data)
61-
return graph
62-
end
63-
64-
function Base.delete!(graph::AbstractVertexOrEdgeDataGraph, ind)
65-
delete!_datagraph(graph, to_graph_index(graph, ind))
66-
return graph
67-
end
68-
69-
function Dictionaries.set!(graph::AbstractVertexOrEdgeDataGraph, ind, data)
70-
set!_datagraph(graph, to_graph_index(graph, ind), data)
71-
return graph
72-
end
73-
74-
function Base.merge!(graph::AbstractVertexOrEdgeDataGraph, other)
75-
for key in keys(other)
76-
set!(graph, key, other[key])
8+
for GType in (:AbstractVertexDataGraph, :AbstractEdgeDataGraph)
9+
@eval begin
10+
Graphs.edgetype(graph::$GType) = edgetype(typeof(graph))
11+
12+
function NamedGraphs.similar_graph(
13+
graph::$GType
14+
)
15+
return similar_graph(graph, valtype(graph))
16+
end
17+
18+
function NamedGraphs.similar_graph(
19+
graph::$GType,
20+
vertices
21+
)
22+
return similar_graph(graph, valtype(graph), vertices)
23+
end
24+
25+
function Base.copy(graph::$GType)
26+
graph_dst = similar_graph(graph)
27+
# Allow copies of graphs with undefined data.
28+
copyto!(graph_dst, graph, filter(key -> isassigned(graph, key), keys(graph)))
29+
return graph_dst
30+
end
31+
32+
function Base.copyto!(graph_dst::$GType, src)
33+
copyto!(graph_dst, src, keys(src))
34+
return graph_dst
35+
end
36+
37+
Base.iterate(graph::$GType) = iterate(index_data(graph))
38+
function Base.iterate(graph::$GType, state)
39+
return iterate(index_data(graph), state)
40+
end
41+
42+
Base.keytype(graph::$GType) = keytype(typeof(graph))
43+
44+
Base.valtype(graph::$GType) = valtype(typeof(graph))
45+
Base.valtype(::Type{<:$GType{T}}) where {T} = T
46+
47+
Base.eltype(graph::$GType) = eltype(typeof(graph))
48+
Base.eltype(::Type{<:$GType{T}}) where {T} = T
49+
50+
Base.length(graph::$GType) = length(index_data(graph))
51+
Base.keys(graph::$GType) = keys(index_data(graph))
52+
Base.values(graph::$GType) = values(index_data(graph))
53+
54+
Dictionaries.issettable(::$GType) = true
55+
Dictionaries.isinsertable(::$GType) = false
56+
57+
function Base.insert!(graph::$GType, ind, data)
58+
isinsertable(graph) || throw(ArgumentError("Graph does not support insertion."))
59+
insert!_datagraph(graph, to_graph_index(graph, ind), data)
60+
return graph
61+
end
62+
63+
function Base.delete!(graph::$GType, ind)
64+
delete!_datagraph(graph, to_graph_index(graph, ind))
65+
return graph
66+
end
67+
68+
function Dictionaries.set!(graph::$GType, ind, data)
69+
set!_datagraph(graph, to_graph_index(graph, ind), data)
70+
return graph
71+
end
72+
73+
function Base.merge!(graph::$GType, other)
74+
for key in keys(other)
75+
set!(graph, key, other[key])
76+
end
77+
return graph
78+
end
7779
end
78-
return graph
7980
end
8081

8182
# ================================== vertex data graph =================================== #

0 commit comments

Comments
 (0)