Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define convert_vertextype for Simple[Di]Graph #72

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NamedGraphs"
uuid = "678767b0-92e7-4007-89e4-4527a8725b19"
authors = ["Matthew Fishman <[email protected]> and contributors"]
version = "0.5.0"
version = "0.5.1"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
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
Loading