Skip to content

Commit

Permalink
for
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 17, 2024
1 parent 254e4cd commit ec77a4b
Show file tree
Hide file tree
Showing 4 changed files with 28 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
21 changes: 12 additions & 9 deletions src/namededge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ 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

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)
10 changes: 8 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,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
Expand Down

0 comments on commit ec77a4b

Please sign in to comment.