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

Type-stable constructors #45

Merged
merged 8 commits into from
Mar 2, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/permanent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- 'master'
pull_request:
jobs:
document:
runs-on: ubuntu-latest
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: test
on:
- push
- pull_request
push:
branches:
- 'master'
pull_request:
jobs:
test:
name: ${{ matrix.version }}
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ julia = "1.6"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Documenter", "JuliaFormatter", "Test"]
test = ["Aqua", "Documenter", "JuliaFormatter", "MetaGraphs", "Test"]
12 changes: 9 additions & 3 deletions docs/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "f82c2777847a88fb4979a0b1e71456c8301bca98"
project_hash = "580b7ffaff126fd1040a2c45d9e0166b2dd82fd9"

[[deps.ANSIColoredPrinters]]
git-tree-sha1 = "574baf8110975760d391c710b6341da1afa48d8c"
Expand Down Expand Up @@ -159,11 +159,17 @@ deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.0+0"

[[deps.MetaGraphs]]
deps = ["Graphs", "JLD2", "Random"]
git-tree-sha1 = "1130dbe1d5276cb656f6e1094ce97466ed700e5a"
uuid = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
version = "0.7.2"

[[deps.MetaGraphsNext]]
deps = ["Graphs", "JLD2"]
deps = ["Graphs", "JLD2", "SimpleTraits"]
path = ".."
uuid = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
version = "0.4.1"
version = "0.5.0"

[[deps.Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
3 changes: 3 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ makedocs(;
assets=String[],
edit_link=:commit,
),
checkdocs=:all,
linkcheck=true,
strict=true,
)

deploydocs(; repo="github.com/JuliaGraphs/MetaGraphsNext.jl.git")
3 changes: 2 additions & 1 deletion src/MetaGraphsNext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ using Graphs
using SimpleTraits

export MetaGraph
export label_for, code_for, set_data!
export label_for, code_for, labels
export set_data!
export weighttype, default_weight, get_weight_function
export MGFormat, DOTFormat

Expand Down
4 changes: 0 additions & 4 deletions src/dict_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,3 @@ function _copy_props!(old_meta_graph::MetaGraph, new_meta_graph::MetaGraph, code
end
return nothing
end

# TODO - It would be nice to be able to apply a function to properties.
# Not sure how this might work, but if the property is a vector,
# a generic way to append to it would be a good thing.
22 changes: 16 additions & 6 deletions src/directedness.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function Graphs.is_directed(::MetaGraph{Code,Graph}) where {Code,Graph<:AbstractGraph}
return is_directed(Graph)
end

function Graphs.is_directed(
::Type{<:MetaGraph{Code,Graph}}
) where {Code,Graph<:AbstractGraph}
return is_directed(Graph)
end

"""
arrange(graph, label_1, label_2)

Expand All @@ -6,14 +16,14 @@ Sort two vertex labels in a default order (useful to uniquely express undirected
function arrange end

@traitfn function arrange(
::AG::IsDirected, label_1, label_2, _drop...
) where {T,AG<:AbstractGraph{T}}
::MG, label_1, label_2, _drop...
) where {MG <: MetaGraph; IsDirected{MG}}
return label_1, label_2
end

@traitfn function arrange(
::AG::(!IsDirected), label_1, label_2, code_1, code_2
) where {T,AG<:AbstractGraph{T}}
::MG, label_1, label_2, code_1, code_2
) where {MG <: MetaGraph; !IsDirected{MG}}
if code_1 < code_2
(label_1, label_2)
else
Expand All @@ -22,8 +32,8 @@ end
end

@traitfn function arrange(
meta_graph::AG::(!IsDirected), label_1, label_2
) where {T,AG<:AbstractGraph{T}}
meta_graph::MG, label_1, label_2
) where {MG <: MetaGraph; !IsDirected{MG}}
return arrange(
meta_graph,
label_1,
Expand Down
60 changes: 23 additions & 37 deletions src/graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,35 @@ end
function Graphs.edgetype(meta_graph::MetaGraph)
return edgetype(meta_graph.graph)
end

function Graphs.nv(meta_graph::MetaGraph)
return nv(meta_graph.graph)
end

function Graphs.ne(meta_graph::MetaGraph)
return ne(meta_graph.graph)
end

function Graphs.vertices(meta_graph::MetaGraph)
return vertices(meta_graph.graph)
end

function Graphs.edges(meta_graph::MetaGraph)
return edges(meta_graph.graph)
end

function Graphs.has_vertex(meta_graph::MetaGraph, code::Integer)
return has_vertex(meta_graph.graph, code)
end

function Graphs.has_edge(meta_graph::MetaGraph, code_1::Integer, code_2::Integer)
return has_edge(meta_graph.graph, code_1, code_2)
end

function Graphs.inneighbors(meta_graph::MetaGraph, code::Integer)
return inneighbors(meta_graph.graph, code)
end

function Graphs.outneighbors(meta_graph::MetaGraph, code::Integer)
return outneighbors(meta_graph.graph, code)
end
Expand All @@ -41,42 +47,6 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
return issubset(meta_graph.graph, h.graph)
end

function Graphs.is_directed(
::MetaGraph{Code,Label,Graph}
) where {Code,Label,Graph<:AbstractGraph}
return is_directed(Graph)
end

function Graphs.is_directed(
::Type{<:MetaGraph{Code,Label,Graph}}
) where {Code,Label,Graph<:AbstractGraph}
return is_directed(Graph)
end

## Link between graph codes and metagraph labels

"""
code_for(meta_graph::MetaGraph, label)

Find the vertex code (or index) associated with label `label`.

This can be useful to pass to methods inherited from `Graphs`. Note, however, that vertex codes can be reassigned after vertex deletion.
"""
function code_for(meta_graph::MetaGraph, label)
return meta_graph.vertex_properties[label][1]
end

"""
label_for(meta_graph::MetaGraph, code)

Find the label associated with code `code`.

This can be useful to interpret the results of methods inherited from `Graphs`. Note, however, that vertex codes can be reassigned after vertex deletion.
"""
function label_for(meta_graph::MetaGraph, code::Integer)
return meta_graph.vertex_labels[code]
end

## Set vertex and edge data

"""
Expand Down Expand Up @@ -205,6 +175,22 @@ end

## Miscellaneous

function Base.copy(meta_graph::MetaGraph)
return deepcopy(meta_graph)
end

function Base.zero(meta_graph::MetaGraph)
return MetaGraph(
zero(meta_graph.graph),
empty(meta_graph.vertex_labels),
empty(meta_graph.vertex_properties),
empty(meta_graph.edge_data),
deepcopy(meta_graph.graph_data),
deepcopy(meta_graph.weight_function),
deepcopy(meta_graph.default_weight),
)
end

function Graphs.induced_subgraph(
meta_graph::MetaGraph, vertex_codes::AbstractVector{<:Integer}
)
Expand All @@ -222,7 +208,7 @@ function Graphs.induced_subgraph(
return new_graph, code_map
end

@traitfn function Graphs.reverse(meta_graph::MetaGraph::IsDirected)
@traitfn function Graphs.reverse(meta_graph::MG) where {MG <: MetaGraph; IsDirected{MG}}
edge_data = meta_graph.edge_data
reverse_edge_data = empty(edge_data)
for (label_1, label_2) in keys(edge_data)
Expand Down
Loading