Skip to content

Commit f3930fa

Browse files
committed
Simplify similar_graph via new struct VertexEdgeDataTypes.
1 parent fae5876 commit f3930fa

6 files changed

Lines changed: 48 additions & 59 deletions

File tree

src/abstractdatagraph.jl

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ using NamedGraphs: NamedGraphs, AbstractEdges, AbstractNamedEdge, AbstractNamedG
1010
similar_graph, subgraph_edges
1111
using SimpleTraits: SimpleTraits, @traitfn, Not
1212

13+
struct VertexEdgeDataTypes{VD, ED}
14+
vertex_data_type::VD
15+
edge_data_type::ED
16+
end
17+
1318
abstract type AbstractDataGraph{V, VD, ED} <: AbstractNamedGraph{V} end
1419

1520
vertex_data_type(::Type{<:AbstractGraph}) = Any
@@ -145,21 +150,19 @@ end
145150
"""
146151
similar_graph(datagraph::AbstractDataGraph, D::Type)
147152
similar_graph(datagraph::AbstractDataGraph, D::Type, vertices)
148-
similar_graph(datagraph::AbstractDataGraph, VD::Type, ED::Type)
149-
similar_graph(datagraph::AbstractDataGraph, VD::Type, ED::Type, vertices)
153+
similar_graph(datagraph::AbstractDataGraph, D::VertexEdgeDataTypes)
154+
similar_graph(datagraph::AbstractDataGraph, D::VertexEdgeDataTypes, vertices)
150155
151156
Create an uninitialized data graph, similar to the provided `datagraph`, but with vertices
152157
defined by `vertices` and a vertex and edge data type `D`. One may also provide separate
153-
vertex and edge data types `VD` and `ED`.
158+
vertex and edge data types `VD` and `ED` by using the wrapper `VertexEdgeDataTypes(VD, ED)`.
154159
If vertices are not provided, then the graph is constructed with the same vertices and edges
155160
as the input graph.
156161
"""
157-
function NamedGraphs.similar_graph(
158-
graph::AbstractDataGraph
159-
)
160-
VD = vertex_data_type(graph)
161-
ED = edge_data_type(graph)
162-
return similar_graph(graph, VD, ED)
162+
163+
NamedGraphs.similar_graph(graph::AbstractDataGraph, D::Type) = similar_datagraph(graph, D)
164+
function NamedGraphs.similar_graph(graph::AbstractDataGraph, D::VertexEdgeDataTypes)
165+
return similar_datagraph(graph, D)
163166
end
164167

165168
function NamedGraphs.similar_graph(
@@ -168,37 +171,36 @@ function NamedGraphs.similar_graph(
168171
)
169172
VD = vertex_data_type(graph)
170173
ED = edge_data_type(graph)
171-
return similar_graph(graph, VD, ED, vertices)
172-
end
173-
function NamedGraphs.similar_graph(
174-
graph::AbstractDataGraph,
175-
D::Type
176-
)
177-
return similar_graph(graph, D, D)
174+
175+
return similar_graph(graph, VertexEdgeDataTypes(VD, ED), vertices)
178176
end
179177

178+
# Base case(s) (overload these if fallback not wanted).
180179
function NamedGraphs.similar_graph(
181180
graph::AbstractDataGraph,
182181
D::Type,
183182
vertices
184183
)
185-
return similar_graph(graph, D, D, vertices)
184+
return similar_datagraph(graph, D, D, vertices)
186185
end
187-
188186
function NamedGraphs.similar_graph(
189187
graph::AbstractDataGraph,
190-
VD::Type,
191-
ED::Type
188+
D::VertexEdgeDataTypes,
189+
vertices
192190
)
193-
new_graph = similar_graph(graph, VD, ED, vertices(graph))
191+
return similar_datagraph(graph, D.vertex_data_type, D.edge_data_type, vertices)
192+
end
193+
194+
# Internal implementation functions,
195+
function similar_datagraph(graph::AbstractGraph, D)
196+
new_graph = similar_graph(graph, D, vertices(graph))
194197
add_edges!(new_graph, edges(graph))
195198

196199
return new_graph
197200
end
198201

199-
# Base case(s) (overload these if fallback not wanted).
200-
@traitfn function NamedGraphs.similar_graph(
201-
graph::AbstractDataGraph::(!IsDirected),
202+
@traitfn function similar_datagraph(
203+
graph::AbstractGraph::(!IsDirected),
202204
VD::Type,
203205
ED::Type,
204206
vertices
@@ -207,8 +209,8 @@ end
207209

208210
return DataGraph(underlying_graph; vertex_data_type = VD, edge_data_type = ED)
209211
end
210-
@traitfn function NamedGraphs.similar_graph(
211-
graph::AbstractDataGraph::IsDirected,
212+
@traitfn function similar_datagraph(
213+
graph::AbstractGraph::IsDirected,
212214
VD::Type,
213215
ED::Type,
214216
vertices

src/abstractedgeorvertexdatagraph.jl

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,6 @@ function NamedGraphs.similar_graph(
1818
)
1919
return similar_graph(graph, valtype(graph), vertices)
2020
end
21-
function NamedGraphs.similar_graph(
22-
graph::AbstractVertexOrEdgeDataGraph,
23-
T::Type,
24-
vertices
25-
)
26-
return similar_graph(graph, T, Vertices(vertices))
27-
end
28-
29-
# For ambiguity resolution only.
30-
function NamedGraphs.similar_graph(
31-
graph::AbstractVertexOrEdgeDataGraph,
32-
VD::Type,
33-
ED::Type
34-
)
35-
# No notion of both vertex and edge data, will go to `AbstractDataGraph` fallback.
36-
new_graph = similar_graph(graph, VD, ED, vertices(graph)) # -> DataGraph
37-
add_edges!(new_graph, edges(graph))
38-
return new_graph
39-
end
4021

4122
function Base.copy(graph::AbstractVertexOrEdgeDataGraph)
4223
graph_dst = similar_graph(graph)
@@ -94,6 +75,11 @@ Base.keytype(::Type{<:AbstractVertexDataGraph{T, V}}) where {T, V} = V
9475

9576
is_edge_assigned(::AbstractVertexDataGraph, _edge) = false
9677

78+
# For method ambiguity resolution,
79+
# TODO: remove this method once generic `AbstractDataGraph` method is upgraded.
80+
function set_index_data!(graph::AbstractVertexDataGraph, data, vertex::AbstractEdge)
81+
return throw(MethodError(graph, (data, vertex)))
82+
end
9783
# `setindex!`
9884
function set_index_data!(graph::AbstractVertexDataGraph, data, vertex)
9985
if !has_vertex(graph, vertex)
@@ -145,7 +131,7 @@ end
145131
function NamedGraphs.similar_graph(
146132
::AbstractVertexDataGraph,
147133
T::Type,
148-
vertices::Vertices
134+
vertices
149135
)
150136
return similar_graph(VertexDataGraph{T}, vertices)
151137
end
@@ -251,9 +237,9 @@ end
251237
function NamedGraphs.similar_graph(
252238
::AbstractEdgeDataGraph,
253239
T::Type,
254-
vertices::Vertices
240+
vertices
255241
)
256-
return similar_graph(EdgeDataGraph{T}, collect(vertices))
242+
return similar_graph(EdgeDataGraph{T}, vertices)
257243
end
258244

259245
function NamedGraphs.induced_subgraph_from_vertices(

src/datagraph.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ edge_data_type(G::Type{<:DataGraph}) = eltype(fieldtype(G, :edge_data))
5454
# Extras
5555

5656
# Overwrite the `AbstractDataGraph` fallback (even though they coincide for `DataGraph`)
57-
function NamedGraphs.similar_graph(
57+
function similar_datagraph(
5858
graph::DataGraph,
5959
vertex_data_type::Type,
6060
edge_data_type::Type,

src/edgedatagraph.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ for GType in (:EdgeDataGraph, :EdgeDataDiGraph)
110110
return position_graph(graph.underlying_graph)
111111
end
112112

113-
function NamedGraphs.similar_graph(graph::$GType, T::Type, vertices::Vertices)
114-
new_graph = $GType{T}(undef, collect(vertices))
113+
function NamedGraphs.similar_graph(graph::$GType, T::Type, vertices)
114+
new_graph = $GType{T}(undef, vertices)
115115
return new_graph
116116
end
117117

src/vertexdatagraph.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ for GType in (:VertexDataGraph, :VertexDataDiGraph)
104104
return position_graph(graph.underlying_graph)
105105
end
106106

107-
function NamedGraphs.similar_graph(graph::$GType, T::Type, vertices::Vertices)
108-
new_graph = $GType{T}(undef, collect(vertices))
107+
function NamedGraphs.similar_graph(graph::$GType, T::Type, vertices)
108+
new_graph = $GType{T}(undef, vertices)
109109
return new_graph
110110
end
111111

test/test_basics.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using DataGraphs: DataGraphs, DataGraph, EdgeDataView, VertexDataView, edge_data,
2-
edge_data_type, underlying_graph, vertex_data, vertex_data_type
1+
using DataGraphs: DataGraphs, DataGraph, EdgeDataView, VertexDataView, VertexEdgeDataTypes,
2+
edge_data, edge_data_type, underlying_graph, vertex_data, vertex_data_type
33
using Dictionaries: Dictionaries, AbstractDictionary, AbstractIndices, Dictionary,
44
IndexError, Indices, dictionary, unset!
55
using Graphs: SimpleDiGraph, a_star, add_edge!, bfs_tree, connected_components, degree,
66
dfs_tree, dijkstra_shortest_paths, dst, edges, edgetype, grid, has_edge, has_vertex,
77
indegree, is_directed, ne, nv, outdegree, path_graph, src, steiner_tree, vertices
88
using GraphsFlows: GraphsFlows
9-
using NamedGraphs.GraphsExtensions: directed_graph, rename_vertices, subgraph, vertextype,
9+
using NamedGraphs.GraphsExtensions:
10+
directed_graph, empty_graph, rename_vertices, subgraph, vertextype,
1011
using NamedGraphs.NamedGraphGenerators: named_grid, named_path_graph
1112
using NamedGraphs.OrdinalIndexing: nd, rd, st, th
12-
using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph, empty_graph, similar_graph
13+
using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph, similar_graph
1314
using Test: @test, @test_broken, @testset
1415

1516
@testset "DataGraphs.jl" begin
@@ -626,13 +627,13 @@ using Test: @test, @test_broken, @testset
626627
@test has_vertex(g2, "v")
627628
@test ne(g2) == 0
628629

629-
g2 = similar_graph(g, Float64, Int)
630+
g2 = similar_graph(g, VertexEdgeDataTypes(Float64, Int))
630631
@test has_vertex(g2, "a")
631632
@test has_edge(g2, "a" => "b")
632633
@test vertex_data_type(g2) === Float64
633634
@test edge_data_type(g2) === Int
634635

635-
g2 = similar_graph(g, String, Tuple, [:a, :b])
636+
g2 = similar_graph(g, VertexEdgeDataTypes(String, Tuple), [:a, :b])
636637
@test vertex_data_type(g2) === String
637638
@test edge_data_type(g2) === Tuple
638639
@test has_vertex(g2, :a)

0 commit comments

Comments
 (0)