Skip to content

Commit

Permalink
Define convert_vertextype for Simple[Di]Graph (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Apr 24, 2024
1 parent e064b67 commit 3cca2b1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
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

2 comments on commit 3cca2b1

@mtfishman
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105508

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" 3cca2b102f36993f1461c058eaedcbf48dceaad6
git push origin v0.5.1

Please sign in to comment.