Skip to content

Commit 3cca2b1

Browse files
authored
Define convert_vertextype for Simple[Di]Graph (#72)
1 parent e064b67 commit 3cca2b1

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NamedGraphs"
22
uuid = "678767b0-92e7-4007-89e4-4527a8725b19"
33
authors = ["Matthew Fishman <[email protected]> and contributors"]
4-
version = "0.5.0"
4+
version = "0.5.1"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/lib/GraphsExtensions/src/simplegraph.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ function graph_from_vertices(graph_type::Type{<:AbstractSimpleGraph}, vertices)
66
return graph_type(length(vertices))
77
end
88

9+
function convert_vertextype(vertextype::Type, graph::AbstractSimpleGraph)
10+
return not_implemented()
11+
end
12+
913
using Graphs.SimpleGraphs: SimpleDiGraph, SimpleGraph
1014

15+
function convert_vertextype(vertextype::Type, graph::SimpleGraph)
16+
return SimpleGraph{vertextype}(graph)
17+
end
18+
function convert_vertextype(vertextype::Type, graph::SimpleDiGraph)
19+
return SimpleDiGraph{vertextype}(graph)
20+
end
21+
1122
directed_graph_type(G::Type{<:SimpleGraph}) = SimpleDiGraph{vertextype(G)}
1223
# TODO: Use traits to make this more general.
1324
undirected_graph_type(G::Type{<:SimpleGraph}) = G

src/lib/GraphsExtensions/test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ using NamedGraphs.GraphsExtensions:
4545
all_edges,
4646
child_edges,
4747
child_vertices,
48+
convert_vertextype,
4849
degrees,
4950
directed_graph,
5051
directed_graph_type,
@@ -97,6 +98,15 @@ using Test: @test, @test_broken, @test_throws, @testset
9798
# - random_bfs_tree
9899

99100
@testset "NamedGraphs.GraphsExtensions" begin
101+
# convert_vertextype
102+
for g in (path_graph(4), path_digraph(4))
103+
g_uint16 = convert_vertextype(UInt16, g)
104+
@test g_uint16 == g
105+
@test vertextype(g_uint16) == UInt16
106+
@test issetequal(vertices(g_uint16), vertices(g))
107+
@test issetequal(edges(g_uint16), edges(g))
108+
end
109+
100110
# is_self_loop
101111
@test is_self_loop(SimpleEdge(1, 1))
102112
@test !is_self_loop(SimpleEdge(1, 2))

src/namedgraph.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ function GraphsExtensions.rename_vertices(f::Function, g::AbstractSimpleGraph)
7272
)
7373
end
7474

75-
function GraphsExtensions.convert_vertextype(V::Type, graph::GenericNamedGraph)
75+
function GraphsExtensions.convert_vertextype(vertextype::Type, graph::GenericNamedGraph)
7676
return GenericNamedGraph(
77-
parent_graph(graph), convert(Vector{V}, graph.parent_vertex_to_vertex)
77+
parent_graph(graph), convert(Vector{vertextype}, graph.parent_vertex_to_vertex)
7878
)
7979
end
8080

@@ -92,8 +92,8 @@ function to_vertices(vertices::Tuple{Vararg{Integer}})
9292
return vec(Tuple.(CartesianIndices(vertices)))
9393
end
9494
to_vertices(vertices::Integer) = to_vertices(Base.OneTo(vertices))
95-
function to_vertices(V::Type, vertices)
96-
return convert(Vector{V}, to_vertices(vertices))
95+
function to_vertices(vertextype::Type, vertices)
96+
return convert(Vector{vertextype}, to_vertices(vertices))
9797
end
9898

9999
#

0 commit comments

Comments
 (0)