Skip to content

Commit

Permalink
Minor rewrite and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Liozou committed May 20, 2022
1 parent 8f0567b commit 3a82471
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 0 additions & 2 deletions src/PeriodicGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import Base: (==), isless, convert, show, showerror, eltype, iterate, zero,
@static if VERSION < v"1.7.0-"
# copied from Base without the bound checking
function keepat!(a::Vector, inds)
local prev
i = firstindex(a)
for k in inds
if i != k
@inbounds a[i] = a[k]
end
prev = k
i = nextind(a, i)
end
deleteat!(a, i:lastindex(a))
Expand Down
30 changes: 14 additions & 16 deletions src/algorithms/rings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,10 @@ function _reorderinit(first_indices, verti, vertj::PeriodicVertex{D}) where D
return verti.v, hash_position(PeriodicVertex{D}(vertj.v, vertj.ofs .- verti.ofs), n), counterij[1]
end

function init_distance_record!(dist, i, j)
function init_distance_record!(dist::DistanceRecord{D}, i, j) where D
dist.first_indices[i] = 1
g = dist.g
dist.Q_lists[i] = [(x, one(SmallIntType)) for x in neighbors(g, i)]
g::PeriodicGraph{D} = dist.g
dist.Q_lists[i] = Tuple{PeriodicVertex{D},SmallIntType}[(x, one(SmallIntType)) for x in neighbors(g, i)]
_seen = Set{Int}(i)
encountered = false
n = nv(g)
Expand Down Expand Up @@ -1131,23 +1131,21 @@ function gaussian_elimination!(gauss::IterativeGaussianElimination{T}, r::Vector
buffer1::Vector{Int} = gauss.buffer1
buffer2::Vector{Int} = gauss.buffer2
lenshort = length(shortcuts)
tracklengths = T == Vector{UInt8}
dotrack = T == Tuple{Vector{Int32},Vector{Vector{Int32}}}
if tracklengths
if gauss isa IterativeGaussianEliminationLength
len = length(r) % UInt8
lengths = gauss.track
elseif dotrack
track = first(gauss.track)
elseif gauss isa IterativeGaussianEliminationDecomposition
track::Vector{Int32} = first(gauss.track)
empty!(track)
end
r1 = r[1]

idx::Int32 = r1 > lenshort ? zero(Int32) : shortcuts[r1]
if !iszero(idx)
ridx = rings[idx]
if tracklengths
if gauss isa IterativeGaussianEliminationLength
maxlen = lengths[idx]
elseif dotrack
elseif gauss isa IterativeGaussianEliminationDecomposition
push!(track, idx)
end
symdiff_cycles!(buffer1, r, ridx)
Expand All @@ -1159,9 +1157,9 @@ function gaussian_elimination!(gauss::IterativeGaussianElimination{T}, r::Vector
end
while !iszero(idx)
ridx = rings[idx]
if tracklengths
if gauss isa IterativeGaussianEliminationLength
maxlen = max(lengths[idx], maxlen)
elseif dotrack
elseif gauss isa IterativeGaussianEliminationDecomposition
push!(track, idx)
end
symdiff_cycles!(buffer2, buffer1, ridx)
Expand All @@ -1174,17 +1172,17 @@ function gaussian_elimination!(gauss::IterativeGaussianElimination{T}, r::Vector
push!(rings, copy(buffer1))
r1 > length(shortcuts) && append!(shortcuts, zero(Int32) for _ in 1:(r1-length(shortcuts)))
shortcuts[r1] = length(rings) % Int32
if tracklengths
if gauss isa IterativeGaussianEliminationLength
# the ring was not a sum of strictly smaller ones (since it's not a sum of previous ones at all)
push!(lengths, len)
elseif dotrack
elseif gauss isa IterativeGaussianEliminationDecomposition
push!(last(gauss.track), copy(sort!(track)))
end
return false # the new ring was independent from the other ones

@label notindependentreturn
tracklengths && return maxlen < len
if dotrack
gauss isa IterativeGaussianEliminationLength && return maxlen < len
if gauss isa IterativeGaussianEliminationDecomposition
push!(rings, buffer2) # dummy, used to keep track of dependent rings
push!(last(gauss.track), track) # also dummy
end
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,8 @@ end
for symm in symmweaks
image = symm(str)
@test image == first(PeriodicGraphs.normalize_cycle!(copy(image), nv(lta), Val(3)))
imagevertices = [PeriodicGraphs.reverse_hash_position(x, lta) for x in image]
@test image == PeriodicGraphs.hash_position.(first(PeriodicGraphs.normalize_cycle!(imagevertices)), nv(lta))
@test image weaks
@test symmweaks(image) == id
end
Expand All @@ -1026,6 +1028,8 @@ end
for symm in symmstrs
image = symm(str)
@test image == first(PeriodicGraphs.normalize_cycle!(copy(image), nv(lta), Val(3)))
imagevertices = [PeriodicGraphs.reverse_hash_position(x, lta) for x in image]
@test image == PeriodicGraphs.hash_position.(first(PeriodicGraphs.normalize_cycle!(imagevertices)), nv(lta))
@test image strs
@test symmstrs(image) == id
end
Expand Down

0 comments on commit 3a82471

Please sign in to comment.