Skip to content

Commit

Permalink
Define convert_vertextype for SimpleGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 24, 2024
1 parent e064b67 commit b49577b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/lib/GraphsExtensions/src/simplegraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ function graph_from_vertices(graph_type::Type{<:AbstractSimpleGraph}, vertices)
return graph_type(length(vertices))
end

function convert_vertextype(vertextype::Type, graph::AbstractSimpleGraph)
return not_implemented()
end

using Graphs.SimpleGraphs: SimpleDiGraph, SimpleGraph

function convert_vertextype(vertextype::Type, graph::SimpleGraph)
return SimpleGraph{vertextype}(graph)
end
function convert_vertextype(vertextype::Type, graph::SimpleDiGraph)
return SimpleDiGraph{vertextype}(graph)
end

directed_graph_type(G::Type{<:SimpleGraph}) = SimpleDiGraph{vertextype(G)}
# TODO: Use traits to make this more general.
undirected_graph_type(G::Type{<:SimpleGraph}) = G
Expand Down
10 changes: 10 additions & 0 deletions src/lib/GraphsExtensions/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ using NamedGraphs.GraphsExtensions:
all_edges,
child_edges,
child_vertices,
convert_vertextype,
degrees,
directed_graph,
directed_graph_type,
Expand Down Expand Up @@ -97,6 +98,15 @@ using Test: @test, @test_broken, @test_throws, @testset
# - random_bfs_tree

@testset "NamedGraphs.GraphsExtensions" begin
# convert_vertextype
for g in (path_graph(4), path_digraph(4))
g_uint16 = convert_vertextype(UInt16, g)
@test g_uint16 == g
@test vertextype(g_uint16) == UInt16
@test issetequal(vertices(g_uint16), vertices(g))
@test issetequal(edges(g_uint16), edges(g))
end

# is_self_loop
@test is_self_loop(SimpleEdge(1, 1))
@test !is_self_loop(SimpleEdge(1, 2))
Expand Down
8 changes: 4 additions & 4 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function GraphsExtensions.rename_vertices(f::Function, g::AbstractSimpleGraph)
)
end

function GraphsExtensions.convert_vertextype(V::Type, graph::GenericNamedGraph)
function GraphsExtensions.convert_vertextype(vertextype::Type, graph::GenericNamedGraph)
return GenericNamedGraph(
parent_graph(graph), convert(Vector{V}, graph.parent_vertex_to_vertex)
parent_graph(graph), convert(Vector{vertextype}, graph.parent_vertex_to_vertex)
)
end

Expand All @@ -92,8 +92,8 @@ function to_vertices(vertices::Tuple{Vararg{Integer}})
return vec(Tuple.(CartesianIndices(vertices)))
end
to_vertices(vertices::Integer) = to_vertices(Base.OneTo(vertices))
function to_vertices(V::Type, vertices)
return convert(Vector{V}, to_vertices(vertices))
function to_vertices(vertextype::Type, vertices)
return convert(Vector{vertextype}, to_vertices(vertices))
end

#
Expand Down

0 comments on commit b49577b

Please sign in to comment.