Skip to content

Commit fe2a5bb

Browse files
authored
Merge pull request #40 from filchristou/usetraits
Use traits for (un)directed types
2 parents f1175c1 + a1ef56f commit fe2a5bb

File tree

11 files changed

+100
-90
lines changed

11 files changed

+100
-90
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
name = "MetaGraphsNext"
22
uuid = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
3-
version = "0.4.1"
3+
version = "0.5.0"
44

55
[deps]
66
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
77
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
8+
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
89

910
[compat]
1011
Graphs = "1.4.1"
1112
JLD2 = "0.1.11, 0.2, 0.3, 0.4"
13+
SimpleTraits = "0.9"
1214
julia = "1.6"
1315

1416
[extras]

src/MetaGraphsNext.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module MetaGraphsNext
22

33
using JLD2
44
using Graphs
5+
using SimpleTraits
56

6-
export MetaGraph, MetaDiGraph, MetaUndirectedGraph
7+
export MetaGraph
78
export label_for, code_for, set_data!
89
export weighttype, default_weight, get_weight_function
910
export MGFormat, DOTFormat
1011

1112
include("metagraph.jl")
12-
include("metaundigraph.jl")
13-
include("metadigraph.jl")
13+
include("directedness.jl")
1414
include("graphs.jl")
1515
include("dict_utils.jl")
1616
include("weights.jl")

src/directedness.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
arrange(graph, label_1, label_2)
3+
4+
Sort two vertex labels in a default order (useful to uniquely express undirected edges).
5+
"""
6+
function arrange end
7+
8+
@traitfn function arrange(
9+
::AG::IsDirected, label_1, label_2, _drop...
10+
) where {T,AG<:AbstractGraph{T}}
11+
return label_1, label_2
12+
end
13+
14+
@traitfn function arrange(
15+
::AG::(!IsDirected), label_1, label_2, code_1, code_2
16+
) where {T,AG<:AbstractGraph{T}}
17+
if code_1 < code_2
18+
(label_1, label_2)
19+
else
20+
(label_2, label_1)
21+
end
22+
end
23+
24+
@traitfn function arrange(
25+
meta_graph::AG::(!IsDirected), label_1, label_2
26+
) where {T,AG<:AbstractGraph{T}}
27+
return arrange(
28+
meta_graph,
29+
label_1,
30+
label_2,
31+
code_for(meta_graph, label_1),
32+
code_for(meta_graph, label_2),
33+
)
34+
end

src/graphs.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
4141
return issubset(meta_graph.graph, h.graph)
4242
end
4343

44+
function Graphs.is_directed(
45+
::MetaGraph{Code,Label,Graph}
46+
) where {Code,Label,Graph<:AbstractGraph}
47+
return is_directed(Graph)
48+
end
49+
50+
function Graphs.is_directed(
51+
::Type{<:MetaGraph{Code,Label,Graph}}
52+
) where {Code,Label,Graph<:AbstractGraph}
53+
return is_directed(Graph)
54+
end
55+
4456
## Link between graph codes and metagraph labels
4557

4658
"""
@@ -210,7 +222,7 @@ function Graphs.induced_subgraph(
210222
return new_graph, code_map
211223
end
212224

213-
function Graphs.reverse(meta_graph::MetaDiGraph)
225+
@traitfn function Graphs.reverse(meta_graph::MetaGraph::IsDirected)
214226
edge_data = meta_graph.edge_data
215227
reverse_edge_data = empty(edge_data)
216228
for (label_1, label_2) in keys(edge_data)

src/metadigraph.jl

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/metagraph.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
"""
2-
MetaGraph{Code<:Integer,Label,Graph,VertexData,EdgeData,GraphData,WeightFunction,Weight<:Real} <: AbstractGraph{Code}
2+
MetaGraph{
3+
Code<:Integer,
4+
Label,
5+
Graph,
6+
VertexData,
7+
EdgeData,
8+
GraphData,
9+
WeightFunction,
10+
Weight<:Real
11+
} <: AbstractGraph{Code}
312
413
A graph type with custom vertex labels containing vertex-, edge- and graph-level metadata.
514
@@ -65,13 +74,6 @@ function MetaGraph(
6574
)
6675
end
6776

68-
"""
69-
arrange(graph, label_1, label_2)
70-
71-
Sort two vertex labels in a default order (useful to uniquely express undirected edges).
72-
"""
73-
function arrange end
74-
7577
function Base.zero(
7678
meta_graph::MetaGraph{Code,Label,Graph,VertexData,EdgeData}
7779
) where {Code,Label,Graph,VertexData,EdgeData}

src/metaundigraph.jl

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/labels_codes.jl

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/labels_codes_directedness.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# undirected MetaGraph
2+
colors = MetaGraph(
3+
Graph(); VertexData=String, EdgeData=Symbol, graph_data="graph_of_colors"
4+
)
5+
@test istrait(IsDirected{typeof(colors)}) == is_directed(colors) == false
6+
labels = [:red, :yellow, :blue]
7+
values = ["warm", "warm", "cool"]
8+
for (label, value) in zip(labels, values)
9+
colors[label] = value
10+
end
11+
for label in labels
12+
@test label_for(colors, code_for(colors, label)) == label
13+
end
14+
#delete an entry and test again
15+
rem_vertex!(colors, 1)
16+
popfirst!(labels)
17+
popfirst!(values)
18+
for label in labels
19+
@test label_for(colors, code_for(colors, label)) == label
20+
end
21+
@test MetaGraphsNext.arrange(colors, :yellow, :blue) == (:blue, :yellow)
22+
23+
# directed MetaGraph
24+
dcolors = MetaGraph(
25+
SimpleDiGraph(); VertexData=String, EdgeData=Symbol, graph_data="graph_of_colors"
26+
)
27+
@test istrait(IsDirected{typeof(dcolors)}) == is_directed(dcolors) == true
28+
labels = [:red, :yellow, :blue]
29+
values = ["warm", "warm", "cool"]
30+
for (label, value) in zip(labels, values)
31+
dcolors[label] = value
32+
end
33+
dcolors[:red, :yellow] = :redyellow
34+
@test MetaGraphsNext.arrange(dcolors, :yellow, :blue) == (:yellow, :blue)

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Documenter
33
using Graphs
44
using JuliaFormatter
55
using MetaGraphsNext
6+
using SimpleTraits
67
using Test
78

89
@testset verbose = true "MetaGraphsNext" begin
@@ -26,7 +27,7 @@ using Test
2627
include(joinpath("tutorial", "3_files.jl"))
2728
end
2829
end
29-
@testset verbose = true "Labels and codes" begin
30-
include("labels_codes.jl")
30+
@testset verbose = true "Labels, codes, directedness" begin
31+
include("labels_codes_directedness.jl")
3132
end
3233
end

0 commit comments

Comments
 (0)