From ec77a4b35f97e4b3c74c234c4ad3883a3864bb50 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Tue, 16 Apr 2024 20:05:51 -0400 Subject: [PATCH 1/4] for --- Project.toml | 2 +- src/abstractnamededge.jl | 8 +++++++- src/namededge.jl | 21 ++++++++++++--------- test/test_namedgraph.jl | 10 ++++++++-- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Project.toml b/Project.toml index c25031d..d926dbb 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.4.0" +version = "0.4.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" diff --git a/src/abstractnamededge.jl b/src/abstractnamededge.jl index 1c3d88b..2607d03 100644 --- a/src/abstractnamededge.jl +++ b/src/abstractnamededge.jl @@ -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} @@ -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 diff --git a/src/namededge.jl b/src/namededge.jl index ed793d6..a8ac502 100644 --- a/src/namededge.jl +++ b/src/namededge.jl @@ -4,12 +4,12 @@ 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} +GraphsExtensions.convert_vertextype(vertextype::Type, ::Type{<:NamedEdge}) = NamedEdge{vertextype} Graphs.src(e::NamedEdge) = e.src Graphs.dst(e::NamedEdge) = e.dst @@ -17,14 +17,17 @@ 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)) -Base.convert(E::Type{<:NamedEdge}, e::NamedEdge) = E(e) +AbstractNamedEdge(e::AbstractEdge) = NamedEdge(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]) +Base.convert(edgetype::Type{<:NamedEdge}, e::AbstractEdge) = edgetype(e) + +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) diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index cd008c1..cee63cc 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -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, ⊔, @@ -87,8 +87,14 @@ 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 From 903737c156e3502e107d7de56b66ad102accae74 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Tue, 16 Apr 2024 20:10:38 -0400 Subject: [PATCH 2/4] Update test/test_namedgraph.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/test_namedgraph.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index cee63cc..e5b6125 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -91,7 +91,8 @@ using Test: @test, @test_broken, @testset @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(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") From b2368a2c7af2849f24b812c7a05982e6e42c1495 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Tue, 16 Apr 2024 20:10:44 -0400 Subject: [PATCH 3/4] Update src/namededge.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/namededge.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/namededge.jl b/src/namededge.jl index a8ac502..8928f1c 100644 --- a/src/namededge.jl +++ b/src/namededge.jl @@ -9,7 +9,9 @@ end NamedEdge(src::V, dst::V) where {V} = NamedEdge{V}(src, dst) NamedEdge(src, dst) = NamedEdge{promote_type(typeof(src), typeof(dst))}(src, dst) -GraphsExtensions.convert_vertextype(vertextype::Type, ::Type{<:NamedEdge}) = NamedEdge{vertextype} +function GraphsExtensions.convert_vertextype(vertextype::Type, ::Type{<:NamedEdge}) + return NamedEdge{vertextype} +end Graphs.src(e::NamedEdge) = e.src Graphs.dst(e::NamedEdge) = e.dst From b7e1638c088f01358280970f0f82e859d0c5abed Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Tue, 16 Apr 2024 20:10:49 -0400 Subject: [PATCH 4/4] Update test/test_namedgraph.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/test_namedgraph.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index e5b6125..99e3304 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -94,8 +94,10 @@ using Test: @test, @test_broken, @testset @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") + @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