From 3cca2b102f36993f1461c058eaedcbf48dceaad6 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Wed, 24 Apr 2024 00:27:11 -0400 Subject: [PATCH] Define `convert_vertextype` for `Simple[Di]Graph` (#72) --- Project.toml | 2 +- src/lib/GraphsExtensions/src/simplegraph.jl | 11 +++++++++++ src/lib/GraphsExtensions/test/runtests.jl | 10 ++++++++++ src/namedgraph.jl | 8 ++++---- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 9614857..2b8a015 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "NamedGraphs" uuid = "678767b0-92e7-4007-89e4-4527a8725b19" authors = ["Matthew Fishman and contributors"] -version = "0.5.0" +version = "0.5.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" diff --git a/src/lib/GraphsExtensions/src/simplegraph.jl b/src/lib/GraphsExtensions/src/simplegraph.jl index 3c0ecd4..af3fbf2 100644 --- a/src/lib/GraphsExtensions/src/simplegraph.jl +++ b/src/lib/GraphsExtensions/src/simplegraph.jl @@ -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 diff --git a/src/lib/GraphsExtensions/test/runtests.jl b/src/lib/GraphsExtensions/test/runtests.jl index c724dd7..36db807 100644 --- a/src/lib/GraphsExtensions/test/runtests.jl +++ b/src/lib/GraphsExtensions/test/runtests.jl @@ -45,6 +45,7 @@ using NamedGraphs.GraphsExtensions: all_edges, child_edges, child_vertices, + convert_vertextype, degrees, directed_graph, directed_graph_type, @@ -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)) diff --git a/src/namedgraph.jl b/src/namedgraph.jl index c00cc5b..6c6e075 100644 --- a/src/namedgraph.jl +++ b/src/namedgraph.jl @@ -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 @@ -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 #