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 rename_vertices for SimpleEdge #65

Merged
merged 4 commits into from
Apr 17, 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.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
Loading