Skip to content

Commit

Permalink
Define rename_vertices for SimpleEdge (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Apr 17, 2024
1 parent 254e4cd commit 32442b8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 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.4.0"
version = "0.4.1"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
8 changes: 7 additions & 1 deletion src/abstractnamededge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Base.eltype(::Type{<:AbstractNamedEdge{V}}) where {V} = V
Graphs.src(e::AbstractNamedEdge) = not_implemented()
Graphs.dst(e::AbstractNamedEdge) = not_implemented()

AbstractNamedEdge(e::AbstractNamedEdge) = e

function GraphsExtensions.convert_vertextype(
::Type{V}, E::Type{<:AbstractNamedEdge{V}}
) where {V}
Expand Down Expand Up @@ -49,6 +51,10 @@ function GraphsExtensions.rename_vertices(f::Function, e::AbstractNamedEdge)
return set_vertices(e, f(src(e)), f(dst(e)))
end

function GraphsExtensions.rename_vertices(e::AbstractNamedEdge, name_map)
function GraphsExtensions.rename_vertices(e::AbstractEdge, name_map)
return rename_vertices(v -> name_map[v], e)
end

function GraphsExtensions.rename_vertices(f::Function, e::AbstractEdge)
return rename_vertices(f, AbstractNamedEdge(e))
end
23 changes: 14 additions & 9 deletions src/namededge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ using .GraphsExtensions: GraphsExtensions
struct NamedEdge{V} <: AbstractNamedEdge{V}
src::V
dst::V
NamedEdge{V}(src::V, dst::V) where {V} = new{V}(src, dst)
NamedEdge{V}(src, dst) where {V} = new{V}(src, dst)
end
NamedEdge(src::V, dst::V) where {V} = NamedEdge{V}(src, dst)
NamedEdge(src::S, dst::D) where {S,D} = NamedEdge{promote_type(S, D)}(src, dst)
NamedEdge(src, dst) = NamedEdge{promote_type(typeof(src), typeof(dst))}(src, dst)

GraphsExtensions.convert_vertextype(V::Type, ::Type{<:NamedEdge}) = NamedEdge{V}
function GraphsExtensions.convert_vertextype(vertextype::Type, ::Type{<:NamedEdge})
return NamedEdge{vertextype}
end

Graphs.src(e::NamedEdge) = e.src
Graphs.dst(e::NamedEdge) = e.dst

NamedEdge{V}(e::NamedEdge{V}) where {V} = e
NamedEdge(e::NamedEdge) = e

NamedEdge{V}(e::AbstractNamedEdge) where {V} = NamedEdge{V}(e.src, e.dst)
NamedEdge{V}(e::AbstractEdge) where {V} = NamedEdge{V}(src(e), dst(e))
NamedEdge(e::AbstractEdge) = NamedEdge(src(e), dst(e))

AbstractNamedEdge(e::AbstractEdge) = NamedEdge(e)

Base.convert(E::Type{<:NamedEdge}, e::NamedEdge) = E(e)
Base.convert(edgetype::Type{<:NamedEdge}, e::AbstractEdge) = edgetype(e)

NamedEdge(t::Tuple) = NamedEdge(t[1], t[2])
NamedEdge(p::Pair) = NamedEdge(p.first, p.second)
NamedEdge{V}(p::Pair) where {V} = NamedEdge{V}(p.first, p.second)
NamedEdge{V}(t::Tuple) where {V} = NamedEdge{V}(t[1], t[2])
NamedEdge(p::Tuple) = NamedEdge(p...)
NamedEdge(p::Pair) = NamedEdge(p...)
NamedEdge{V}(p::Pair) where {V} = NamedEdge{V}(p...)
NamedEdge{V}(p::Tuple) where {V} = NamedEdge{V}(p...)

# TODO: Define generic `set_vertices` in `GraphsExtensions`.
set_vertices(e::NamedEdge, src, dst) = NamedEdge(src, dst)
13 changes: 11 additions & 2 deletions test/test_namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ using Graphs:
topological_sort_by_dfs,
vertices,
yen_k_shortest_paths
using Graphs.SimpleGraphs: SimpleDiGraph
using Graphs.SimpleGraphs: SimpleDiGraph, SimpleEdge
using GraphsFlows: GraphsFlows
using NamedGraphs: NamedEdge, NamedDiGraph, NamedGraph
using NamedGraphs: AbstractNamedEdge, NamedEdge, NamedDiGraph, NamedGraph
using NamedGraphs.GraphsExtensions:
GraphsExtensions,
,
Expand All @@ -87,8 +87,17 @@ using SymRCM: SymRCM
using Test: @test, @test_broken, @testset

@testset "NamedEdge" begin
@test NamedEdge(SimpleEdge(1, 2)) == NamedEdge(1, 2)
@test AbstractNamedEdge(SimpleEdge(1, 2)) == NamedEdge(1, 2)
@test is_ordered(NamedEdge("A", "B"))
@test !is_ordered(NamedEdge("B", "A"))
@test rename_vertices(NamedEdge("A", "B"), Dict(["A" => "C", "B" => "D"])) ==
NamedEdge("C", "D")
@test rename_vertices(SimpleEdge(1, 2), Dict([1 => "C", 2 => "D"])) == NamedEdge("C", "D")
@test rename_vertices(v -> Dict(["A" => "C", "B" => "D"])[v], NamedEdge("A", "B")) ==
NamedEdge("C", "D")
@test rename_vertices(v -> Dict([1 => "C", 2 => "D"])[v], SimpleEdge(1, 2)) ==
NamedEdge("C", "D")
end

@testset "NamedGraph" begin
Expand Down

2 comments on commit 32442b8

@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/105057

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.4.1 -m "<description of version>" 32442b813febd9d6619bc7cc5ee0958193850c9c
git push origin v0.4.1

Please sign in to comment.