From 0feb5396d9aa0142cd4292f138ccf17d83f8339b Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 26 Apr 2024 14:12:15 -0400 Subject: [PATCH] `NamedGraph` conversion (#76) --- src/namedgraph.jl | 21 +++++++++++++++++++++ test/test_namedgraph.jl | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/namedgraph.jl b/src/namedgraph.jl index b58a273..d076d46 100644 --- a/src/namedgraph.jl +++ b/src/namedgraph.jl @@ -169,6 +169,27 @@ end GenericNamedGraph() = GenericNamedGraph(Any[]) +function GenericNamedGraph(graph::GenericNamedGraph) + return GenericNamedGraph{vertextype(graph),position_graph_type(graph_type)}(graph) +end +function GenericNamedGraph{V}(graph::GenericNamedGraph) where {V} + return GenericNamedGraph{V,position_graph_type(graph_type)}(graph) +end +function GenericNamedGraph{<:Any,G}( + graph::GenericNamedGraph +) where {G<:AbstractSimpleGraph{Int}} + return GenericNamedGraph{vertextype(graph),G}(graph) +end +function GenericNamedGraph{V,G}( + graph::GenericNamedGraph +) where {V,G<:AbstractSimpleGraph{Int}} + return GenericNamedGraph{V,G}(copy(position_graph(graph)), copy(vertices(graph))) +end + +function Base.convert(graph_type::Type{<:GenericNamedGraph}, graph::GenericNamedGraph) + return graph_type(graph) +end + # TODO: implement as: # graph = set_position_graph(graph, copy(position_graph(graph))) # graph = set_vertices(graph, copy(vertices(graph))) diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index 02fe7bd..2d9e6c0 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -149,6 +149,22 @@ end rem_vertex!(g, "E") @test !has_vertex(g, "E") + g = NamedGraph(grid((4,)), ["A", "B", "C", "D"]) + for gc in (NamedGraph(g), convert(NamedGraph, g)) + @test gc == g + @test gc isa NamedGraph{String} + @test vertextype(gc) === vertextype(g) + @test issetequal(vertices(gc), vertices(g)) + @test issetequal(edges(gc), edges(g)) + end + for gc in (NamedGraph{Any}(g), convert(NamedGraph{Any}, g)) + @test gc == g + @test gc isa NamedGraph{Any} + @test vertextype(gc) === Any + @test issetequal(vertices(gc), vertices(g)) + @test issetequal(edges(gc), edges(g)) + end + io = IOBuffer() show(io, "text/plain", g) @test String(take!(io)) isa String