diff --git a/.gitignore b/.gitignore index ba39cc5..05ccaaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ Manifest.toml +settings.json \ No newline at end of file diff --git a/Project.toml b/Project.toml index cf0f880..b4f4229 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PeriodicGraphs" uuid = "18c5b727-b240-4874-878a-f2e242435bab" authors = ["Lionel Zoubritzky lionel.zoubritzky@gmail.com"] -version = "0.3.0" +version = "0.4.0" [deps] Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" diff --git a/src/PeriodicGraphs.jl b/src/PeriodicGraphs.jl index 454d410..eaab352 100644 --- a/src/PeriodicGraphs.jl +++ b/src/PeriodicGraphs.jl @@ -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} @@ -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) @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 17db415..849d602 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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))]" @@ -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() @@ -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) @@ -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) @@ -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))] @@ -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 @@ -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