diff --git a/Project.toml b/Project.toml index b4a46af..b86c17f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DataGraphs" uuid = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" authors = ["Matthew Fishman and contributors"] -version = "0.2.1" +version = "0.2.2" [deps] Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" @@ -20,7 +20,7 @@ DataGraphsGraphsFlowsExt = "GraphsFlows" Dictionaries = "0.4" Graphs = "1" GraphsFlows = "0.1.1" -NamedGraphs = "0.4" +NamedGraphs = "0.5.1" PackageExtensionCompat = "1" SimpleTraits = "0.9" julia = "1.7" diff --git a/src/abstractdatagraph.jl b/src/abstractdatagraph.jl index 389fa95..41f798c 100644 --- a/src/abstractdatagraph.jl +++ b/src/abstractdatagraph.jl @@ -2,11 +2,13 @@ using Dictionaries: set!, unset! using Graphs: Graphs, AbstractEdge, AbstractGraph, IsDirected, add_edge!, edges, ne, nv, vertices using NamedGraphs.GraphsExtensions: GraphsExtensions, incident_edges, vertextype -using SimpleTraits: SimpleTraits, @traitfn +using NamedGraphs.SimilarType: similar_type +using SimpleTraits: SimpleTraits, Not, @traitfn abstract type AbstractDataGraph{V,VD,ED} <: AbstractGraph{V} end # Minimal interface +# TODO: Define for `AbstractGraph` as a `DataGraphInterface`. underlying_graph(::AbstractDataGraph) = not_implemented() underlying_graph_type(::Type{<:AbstractDataGraph}) = not_implemented() vertex_data(::AbstractDataGraph) = not_implemented() @@ -273,7 +275,7 @@ end function map_vertex_data(f, graph::AbstractDataGraph; vertices=nothing) graph′ = copy(graph) - vs = isnothing(vertices) ? vertices(graph) : vertices + vs = isnothing(vertices) ? Graphs.vertices(graph) : vertices for v in vs graph′[v] = f(graph[v]) end @@ -282,7 +284,7 @@ end function map_edge_data(f, graph::AbstractDataGraph; edges=nothing) graph′ = copy(graph) - es = isnothing(edges) ? edges(graph) : edges + es = isnothing(edges) ? Graphs.edges(graph) : edges for e in es if isassigned(graph, e) graph′[e] = f(graph[e]) @@ -368,7 +370,7 @@ end function Graphs.induced_subgraph(graph::AbstractDataGraph, subvertices) underlying_subgraph, vlist = Graphs.induced_subgraph(underlying_graph(graph), subvertices) - subgraph = typeof(graph)(underlying_subgraph) + subgraph = similar_type(graph)(underlying_subgraph) for v in vertices(subgraph) if isassigned(graph, v) subgraph[v] = graph[v] diff --git a/src/arrange.jl b/src/arrange.jl index 8e861d1..3948340 100644 --- a/src/arrange.jl +++ b/src/arrange.jl @@ -1,5 +1,5 @@ using Graphs: IsDirected, src, dst -using SimpleTraits: SimpleTraits, @traitfn +using SimpleTraits: SimpleTraits, Not, @traitfn # TODO: Use a function `arrange` like in MetaGraphsNext: # https://github.com/JuliaGraphs/MetaGraphsNext.jl/blob/1539095ee6088aba0d5b1cb057c339ad92557889/src/metagraph.jl#L75-L80 diff --git a/src/datagraph.jl b/src/datagraph.jl index b6d1784..665a1f1 100644 --- a/src/datagraph.jl +++ b/src/datagraph.jl @@ -71,14 +71,17 @@ DataGraph{V,VD,ED,G}(graph::DataGraph{V,VD,ED,G}) where {V,VD,ED,G} = copy(graph DataGraph{V,VD,ED}(graph::DataGraph{V,VD,ED}) where {V,VD,ED} = copy(graph) DataGraph{V,VD}(graph::DataGraph{V,VD}) where {V,VD} = copy(graph) DataGraph{V}(graph::DataGraph{V}) where {V} = copy(graph) + function DataGraph{V}(graph::DataGraph) where {V} # TODO: Make sure this properly copies converted_underlying_graph = convert_vertextype(V, underlying_graph(graph)) converted_vertex_data = Dictionary{V}(vertex_data(graph)) - converted_edge_data = Dictionary{edgetype(converted_underlying_graph)}(edge_data(graph)) - return DataGraph{V}( - converted_underlying_graph, converted_vertex_data, converted_edge_data + # This doesn't convert properly. + # converted_edge_data = Dictionary{edgetype(converted_underlying_graph)}(edge_data(graph)) + converted_edge_data = Dictionary( + edgetype(converted_underlying_graph).(keys(edge_data(graph))), values(edge_data(graph)) ) + return _DataGraph(converted_underlying_graph, converted_vertex_data, converted_edge_data) end GraphsExtensions.convert_vertextype(::Type{V}, graph::DataGraph{V}) where {V} = graph diff --git a/src/traits/isunderlyinggraph.jl b/src/traits/isunderlyinggraph.jl index 6afb1dd..c83c811 100644 --- a/src/traits/isunderlyinggraph.jl +++ b/src/traits/isunderlyinggraph.jl @@ -1,4 +1,4 @@ -using SimpleTraits: SimpleTraits, @traitdef, @traitimpl +using SimpleTraits: SimpleTraits, Not, @traitdef, @traitimpl @traitdef IsUnderlyingGraph{X} #! format: off diff --git a/test/runtests.jl b/test/runtests.jl index da410d1..22a3b4f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -142,13 +142,27 @@ using DataGraphs: is_arranged @test dg[2] == "V2" @test dg[3] == "V3" @test dg[4] == "V4" - @test dg[1 => 2] == "E12" @test dg[2 => 3] == "E23" @test dg[3 => 4] == "E34" @test DataGraph(g) isa DataGraph{Int,Any,Any,SimpleGraph{Int},SimpleEdge{Int}} + dg_uint16 = DataGraph{UInt16}(dg) + @test dg_uint16 isa + DataGraph{UInt16,String,String,SimpleGraph{UInt16},SimpleEdge{UInt16}} + @test vertextype(dg_uint16) === UInt16 + @test edgetype(dg_uint16) === SimpleEdge{UInt16} + @test vertex_data_eltype(dg_uint16) === String + @test edge_data_eltype(dg_uint16) === String + @test dg_uint16[1] == "V1" + @test dg_uint16[2] == "V2" + @test dg_uint16[3] == "V3" + @test dg_uint16[4] == "V4" + @test dg_uint16[1 => 2] == "E12" + @test dg_uint16[2 => 3] == "E23" + @test dg_uint16[3 => 4] == "E34" + # Vertices with mixed types dg = DataGraph(NamedGraph(grid((4,)), [1, "X", 2, "Y"])) @test nv(dg) == 4