1
0
mirror of https://github.com/kalmarek/Groups.jl.git synced 2024-12-25 18:15:29 +01:00

clean-up iteration

This commit is contained in:
Marek Kaluba 2021-05-25 22:27:22 +02:00
parent ac3d7f0977
commit 2e560738f6
No known key found for this signature in database
GPG Key ID: 8BF1A3855328FC15
4 changed files with 43 additions and 40 deletions

View File

@ -5,10 +5,10 @@ version = "0.5.2"
[deps] [deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d" AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
GroupsCore = "d5909c97-4eac-4ecc-a3dc-fdd0858a4120" GroupsCore = "d5909c97-4eac-4ecc-a3dc-fdd0858a4120"
KnuthBendix = "c2604015-7b3d-4a30-8a26-9074551ec60a" KnuthBendix = "c2604015-7b3d-4a30-8a26-9074551ec60a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ThreadsX = "ac1d9e8a-700a-412c-b207-f0111f4b6c0d" ThreadsX = "ac1d9e8a-700a-412c-b207-f0111f4b6c0d"

View File

@ -23,7 +23,7 @@ include("arithmetic.jl")
include("findreplace.jl") include("findreplace.jl")
module New module New
using DataStructures import OrderedCollections: OrderedSet
include("new_types.jl") include("new_types.jl")
include("new_hashing.jl") include("new_hashing.jl")

View File

@ -1,53 +1,55 @@
mutable struct FPIterState{GEl,T} mutable struct FPGroupIter{S, T, GEl}
elts::OrderedSet{GEl} seen::S
u::GEl seen_iter_state::T
v::GEl current::GEl
elts_iter_state::T gen_idx::Int
end u_tmp::GEl
v_tmp::GEl
Base.in(x, itr::FPIterState) = x in itr.elts
Base.push!(itr::FPIterState, x) = push!(itr.elts, x)
function FPIterState(G::AbstractFPGroup)
S = OrderedSet([one(G)])
elt, state = iterate(S)
return elt, FPIterState(S, one(G), one(G), state)
end end
function Base.iterate(G::AbstractFPGroup) function Base.iterate(G::AbstractFPGroup)
elt, state = FPIterState(G) seen = OrderedSet([one(G)])
return one(G), (state, elt, 1) current, seen_state = iterate(seen)
gr_iter = FPGroupIter(seen, seen_state, current, 1, one(G), one(G))
return one(G), gr_iter
end
function _next_elt!(itr::FPGroupIter)
res = iterate(itr.seen, itr.seen_iter_state)
res === nothing && return true
itr.current = first(res)
itr.seen_iter_state = last(res)
itr.gen_idx = 1
return false
end end
function Base.iterate(G::AbstractFPGroup, state) function Base.iterate(G::AbstractFPGroup, state)
iter, elt, gen_idx = state if state.gen_idx > length(alphabet(G))
finished = _next_elt!(state)
if gen_idx > length(alphabet(G)) finished && return nothing
res = iterate(iter.elts, iter.elts_iter_state)
res === nothing && return nothing
gen_idx = 1
elt = first(res)
iter.elts_iter_state = last(res)
end end
res = let (u, v) = (iter.u, iter.v), elt = elt elt = let u = state.u_tmp, v = state.v_tmp, current = state.current
copyto!(v, elt) # this invalidates normalform of v copyto!(v, current)
push!(word(v), gen_idx) push!(word(v), state.gen_idx)
resize!(word(u), 0)
_setnormalform!(v, false) _setnormalform!(v, false)
_setvalidhash!(v, false) _setvalidhash!(v, false)
@assert !isnormalform(v) @assert !isnormalform(v)
resize!(word(u), 0)
normalform!(u, v) normalform!(u, v)
end end
if res in iter state.gen_idx += 1
return iterate(G, (iter, elt, gen_idx + 1))
if elt in state.seen
return iterate(G, state)
else else
w = deepcopy(res) @assert isnormalform(elt)
@assert isnormalform(w) push!(state.seen, deepcopy(elt))
push!(iter, w) return elt, state
return w, (iter, elt, gen_idx + 1)
end end
end end

View File

@ -33,11 +33,12 @@
return w return w
end end
k = test_iteration(F3, 10) @time k = test_iteration(F3, 10)
@test k == a*b^-1 @test k == a*b^-1
@time k = test_iteration(F3, 1000) k = test_iteration(F3, 1000)
@test k == (a^2)*c^2*a^-1 @test k == (a^2)*c^2*a^-1
@time k = test_iteration(F3, 1000)
end end
@testset "wl_ball" begin @testset "wl_ball" begin
@ -48,7 +49,7 @@
isnothing(res) && break isnothing(res) && break
g, state = res g, state = res
end end
elts = collect(first(state).elts) elts = collect(state.seen)
elts = resize!(elts, length(elts)-1) elts = resize!(elts, length(elts)-1)
return elts return elts
end end