Skip to content

Commit

Permalink
Add implicit conversions to PeriodicEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
Liozou committed Nov 16, 2021
1 parent ea9dfa0 commit 35354b3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Manifest.toml
settings.json
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PeriodicGraphs"
uuid = "18c5b727-b240-4874-878a-f2e242435bab"
authors = ["Lionel Zoubritzky [email protected]"]
version = "0.3.0"
version = "0.4.0"

[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Expand Down
8 changes: 7 additions & 1 deletion src/PeriodicGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,15 @@ function PeriodicEdge((src, dst, offset)::Tuple{Any,Any,Union{SVector{N,T},NTupl
PeriodicEdge{N}(src, dst, offset)
end

function convert(::Type{PeriodicEdge{N}}, (src, dst, offset)::Tuple) where {N}
function convert(::Type{PeriodicEdge{N}}, (src, dst, offset)::Tuple{Any,Any,Any}) where {N}
PeriodicEdge{N}(src, dst, offset)
end
function convert(::Type{PeriodicEdge}, (src, dst, offset)::Tuple{Any,Any,Union{SVector{N,T},NTuple{N,T}}}) where {N,T<:Integer}
PeriodicEdge{N}(src, dst, offset)
end
function convert(::Union{Type{PeriodicEdge},Type{PeriodicEdge{N}}}, (src, v)::Tuple{Any,PeriodicVertex{N}}) where N
PeriodicEdge{N}(src, v)
end

function show(io::IO, x::PeriodicEdge{N}) where N
if get(io, :typeinfo, Any) != PeriodicEdge{N}
Expand Down Expand Up @@ -466,6 +469,7 @@ function Graphs.has_edge(g::PeriodicGraph, e::PeriodicEdge)
return i <= length(g.nlist[s]) && g.nlist[s][i] == d
end
end
Graphs.has_edge(g::PeriodicGraph, i, x::PeriodicVertex) = has_edge(g, PeriodicEdge(i, x))
Graphs.outneighbors(g::PeriodicGraph, v::Integer) = g.nlist[v]
Graphs.inneighbors(g::PeriodicGraph, v::Integer) = outneighbors(g, v)
zero(::Type{PeriodicGraph{N}}) where N = PeriodicGraph{N}(0)
Expand Down Expand Up @@ -512,6 +516,7 @@ function Graphs.add_edge!(g::PeriodicGraph, e::PeriodicEdge)
end
return success
end
Graphs.add_edge!(g::PeriodicGraph, i, x::PeriodicVertex) = add_edge!(g, PeriodicEdge(i, x))

function _rem_edge!(g::PeriodicGraph, e::PeriodicEdge, ::Val{check}) where check
#=@inbounds=# begin
Expand All @@ -538,6 +543,7 @@ function Graphs.rem_edge!(g::PeriodicGraph, e::PeriodicEdge)
end
return success
end
Graphs.rem_edge!(g::PeriodicGraph, i, x::PeriodicVertex) = rem_edge!(g, PeriodicEdge(i, x))


function Graphs.SimpleGraphs.rem_vertices!(g::PeriodicGraph{N}, t::AbstractVector{<:Integer}, keep_order::Bool=false) where N
Expand Down
41 changes: 21 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ end
@test PeriodicEdge(1, 1, SVector{1,Int}(2)) == PeriodicEdge1D(1, 1, SVector{1,Int}(2))
@test PeriodicEdge(2, 1, ()) == PeriodicEdge{0}((2, 1, SVector{0,Int}()))
@test PeriodicEdge((1, 2, (1,0,0))) == PeriodicEdge3D((1, 2, (1,0,0)))
@test convert(PeriodicEdge, (3, PeriodicVertex{1}(2, (1,)))) == convert(PeriodicEdge1D, (3, PeriodicVertex{1}(2, (1,))))
@test PeriodicEdge1D[(1,2,SVector{1,Int}(3))] == PeriodicEdge[(1,2,(3,))] == [PeriodicEdge(1, 2, (3,))]
@static if VERSION < v"1.6.0-DEV"
@test string(PeriodicEdge3D[(1, 2, (1,0,0))]) == "PeriodicEdge{3}[(1, 2, (1,0,0))]"
Expand Down Expand Up @@ -173,7 +174,7 @@ end
@testset "Single edge" begin
@test isempty(edges(g))
@test isempty(vertices(g))
@test !rem_edge!(g, PeriodicEdge(1, 2, (0,0,0)))
@test !rem_edge!(g, 1, PeriodicVertex(2, (0,0,0)))
@test g == PeriodicGraph3D()
@test !add_edge!(g, PeriodicEdge(1, 1, (-1,0,0)))
@test g == PeriodicGraph3D()
Expand All @@ -182,26 +183,26 @@ end
@test add_vertex!(g)
@test !has_edge(g, 1, 1)
@test !has_edge(g, PeriodicEdge3D(1, 1, (0,1,1)))
@test add_edge!(g, PeriodicEdge(1, 1, (-1,0,0)))
@test add_edge!(g, 1, PeriodicVertex(1, (-1,0,0)))
@test has_edge(g, 1, 1)
@test !add_edge!(g, PeriodicEdge(1, 1, (-1,0,0)))
@test has_edge(g, 1, 1)
@test has_edge(g, PeriodicEdge3D(1, 1, (1,0,0)))
@test has_edge(g, PeriodicEdge3D(1, 1, (-1,0,0)))
@test has_edge(g, 1, PeriodicVertex(1, (-1,0,0)))
@test g == PeriodicGraph3D(PeriodicEdge3D[(1, 1, (1,0,0))])
@test g != PeriodicGraph3D(1)
@test rem_edge!(g, PeriodicEdge(1, 1, (-1,0,0)))
@test g == PeriodicGraph3D(1)
@test !has_edge(g, 1, 1)
@test !has_edge(g, PeriodicEdge3D(1, 1, (1,0,0)))
@test !has_edge(g, 1, PeriodicVertex(1, (1,0,0)))
@test !has_edge(g, PeriodicEdge3D(1, 1, (-1,0,0)))
@test add_edge!(g, PeriodicEdge(1, 1, (2,-1,0)))
@test add_edge!(g, 1, PeriodicVertex(1, (2,-1,0)))
@test !add_edge!(g, PeriodicEdge(1, 1, (-2,1,0)))
@test g == PeriodicGraph3D(PeriodicEdge3D[(1, 1, (-2,1,0))])
@test has_edge(g, 1, 1)
@test has_edge(g, PeriodicEdge3D(1, 1, (2,-1,0)))
@test !has_edge(g, PeriodicEdge3D(1, 1, (1,0,0)))
@test rem_edge!(g, PeriodicEdge(1, 1, (2,-1,0)))
@test rem_edge!(g, 1, PeriodicVertex(1, (2,-1,0)))
@test g == PeriodicGraph3D(1)
@test add_edge!(g, PeriodicEdge(1, 1, (1,0,0)))
@test rem_vertex!(g, 1)
Expand All @@ -218,12 +219,12 @@ end
@test rem_edge!(g, PeriodicEdge(1, 2, (0,0,0)))
@test !has_edge(g, 1, 2)
@test g == PeriodicGraph3D(2)
@test add_edge!(g, PeriodicEdge(1, 2, (-1,0,0)))
@test add_edge!(g, 1, PeriodicVertex(2, (-1,0,0)))
@test g == PeriodicGraph3D(PeriodicEdge3D[(1, 2, (-1,0,0))])
@test g == PeriodicGraph3D(PeriodicEdge3D[(2, 1, (1,0,0))])
@test g != PeriodicGraph3D(PeriodicEdge3D[(1, 2, (1,0,0))])
@test has_edge(g, 1, 2)
@test has_edge(g, PeriodicEdge(1, 2, (-1,0,0)))
@test has_edge(g, 1, PeriodicVertex(2, (-1,0,0)))
@test has_edge(g, PeriodicEdge(2, 1, (1,0,0)))
@test rem_vertex!(g, 1)
@test g == PeriodicGraph3D(1)
Expand Down Expand Up @@ -274,7 +275,7 @@ end
rem_vertex!(g1, 1)
@test g1 != g2 == g3
@test add_edge!(g2, PeriodicEdge(1, 2, (-1,0)))
@test add_edge!(g2, PeriodicEdge(1, 1, (1,1)))
@test add_edge!(g2, 1, PeriodicVertex(1, (1,1)))
@test add_edge!(g2, PeriodicEdge(1, 3, (0,0)))
@test find_edges(g2, 1, 1) == PeriodicVertex2D[(1, (-1,-1)), (1, (1,1))]
@test find_edges(g2, 1, 2) == PeriodicVertex2D[(2, (-1,0)), (2, (0,0))]
Expand Down Expand Up @@ -342,23 +343,23 @@ end

g = PeriodicGraph{0}(4)
@test add_edge!(g, PeriodicEdge{0}(1, 2, ()))
@test add_edge!(g, PeriodicEdge{0}(1, 3, ()))
@test add_edge!(g, PeriodicEdge{0}(2, 4, ()))
@test add_edge!(g, PeriodicEdge(1, 3, ()))
@test add_edge!(g, 2, PeriodicVertex(4, ()))
@test coordination_sequence(g, 1, 10) == [2, 1, 0, 0, 0, 0, 0, 0, 0, 0,]

g = PeriodicGraph1D(3)
@test add_edge!(g, PeriodicEdge(1, 1, (1,)))
@test add_edge!(g, PeriodicEdge1D(1, 1, (1,)))
@test add_edge!(g, PeriodicEdge(1, 2, (0,)))
@test add_edge!(g, PeriodicEdge(2, 3, (0,)))
@test add_edge!(g, PeriodicEdge(3, 3, (1,)))
@test add_edge!(g, 2, PeriodicVertex(3, (0,)))
@test add_edge!(g, 3, PeriodicVertex1D(3, (1,)))
@test coordination_sequence(g, 1, 5) == coordination_sequence(g, 3, 5) == [3, 5, 6, 6, 6]
@test coordination_sequence(g, 2, 5) == [2, 4, 6, 6, 6]

g = PeriodicGraph{4}(1)
@test add_edge!(g, PeriodicEdge(1, 1, (1,0,0,0)))
@test add_edge!(g, PeriodicEdge{4}(1, 1, (1,0,0,0)))
@test add_edge!(g, PeriodicEdge(1, 1, (0,1,0,0)))
@test add_edge!(g, PeriodicEdge(1, 1, (0,0,1,0)))
@test add_edge!(g, PeriodicEdge(1, 1, (0,0,0,1)))
@test add_edge!(g, 1, PeriodicVertex(1, (0,0,1,0)))
@test add_edge!(g, 1, PeriodicVertex{4}(1, (0,0,0,1)))
@test coordination_sequence(g, 1, 30) == [8i*(i^2+2)/3 for i in 1:30] # sequence A008412 of the OEIS
end

Expand Down Expand Up @@ -518,13 +519,13 @@ end
@test equivalent_dict(dimensionality(g), Dict(1 => [[4]], 2 => [[1,2,3]]))
@test rem_edge!(g, PeriodicEdge(1, 3, (0,0,1)))
@test equivalent_dict(dimensionality(g), Dict(0 => [[1]], 1 => [[4]], 2 => [[2,3]]))
@test rem_edge!(g, PeriodicEdge(3, 2, (0,0,0)))
@test rem_edge!(g, 3, PeriodicVertex(2, (0,0,0)))
@test equivalent_dict(dimensionality(g), Dict(0 => [[1]], 1 => [[4]], 2 => [[2,3]]))
@test rem_edge!(g, PeriodicEdge(2, 3, (0,1,0)))
@test equivalent_dict(dimensionality(g), Dict(0 => [[1]], 1 => [[2,3],[4]]))
@test add_edge!(g, PeriodicEdge(2, 3, (1,0,-1)))
@test add_edge!(g, 2, PeriodicVertex3D(3, (1,0,-1)))
@test equivalent_dict(dimensionality(g), Dict(0 => [[1]], 1 => [[4]], 2 => [[2,3]]))
@test add_edge!(g, PeriodicEdge(2, 3, (2,0,-1)))
@test add_edge!(g, 2, PeriodicVertex(3, (2,0,-1)))
@test equivalent_dict(dimensionality(g), Dict(0 => [[1]], 1 => [[4]], 3 => [[2,3]]))
end

Expand Down

2 comments on commit 35354b3

@Liozou
Copy link
Owner Author

@Liozou Liozou commented on 35354b3 Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/48880

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" 35354b3b3d0935d91642d0db84652e408fe11ff3
git push origin v0.4.0

Please sign in to comment.