From 0750a47853c5defc1a34af87b1fe3c7a5342b6e8 Mon Sep 17 00:00:00 2001 From: kalmarek Date: Fri, 21 Sep 2018 18:14:48 +0200 Subject: [PATCH] rework DirectProdIter to the new iteration protocol --- src/DirectProducts.jl | 32 +++++++++++--------------------- src/Groups.jl | 4 ++-- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/DirectProducts.jl b/src/DirectProducts.jl index caaac22..8c87801 100644 --- a/src/DirectProducts.jl +++ b/src/DirectProducts.jl @@ -118,7 +118,7 @@ parent(g::DirectProductGroupElem) = # ############################################################################### -Base.size(g::DirectProductGroupElem) = size(g.elts) +size(g::DirectProductGroupElem) = size(g.elts) Base.IndexStyle(::Type{DirectProductGroupElem}) = Base.LinearFast() Base.getindex(g::DirectProductGroupElem, i::Int) = g.elts[i] @@ -276,10 +276,7 @@ end # ############################################################################### -import Base: size, length, start, next, done, eltype - -struct DirectPowerIter{Gr<:AbstractAlgebra.Group, GrEl<:AbstractAlgebra.GroupElem} - G::Gr +struct DirectPowerIter{GrEl<:AbstractAlgebra.GroupElem} N::Int elts::Vector{GrEl} totalorder::Int @@ -287,27 +284,20 @@ struct DirectPowerIter{Gr<:AbstractAlgebra.Group, GrEl<:AbstractAlgebra.GroupEle end function DirectPowerIter(G::Gr, N::Integer) where {Gr<:AbstractAlgebra.Group} - return DirectPowerIter{Gr, elem_type(G)}( - G, - N, - vec(collect(elements(G))), - Int(order(G))^N, - Int(order(G)) - ) + return DirectPowerIter{elem_type(G)}(N, collect(G), order(G)^N, order(G)) end -Base.size(DPIter::DirectPowerIter) = ntuple(i -> DPIter.orderG, DPIter.N) -Base.length(DPIter::DirectPowerIter) = DPIter.totalorder -Base.start(::DirectPowerIter) = 0 +length(DPIter::DirectPowerIter) = DPIter.totalorder -function Base.next(DPIter::DirectPowerIter, state) - idx = ind2sub(size(DPIter), state+1) - return DirectProductGroupElem([DPIter.elts[i] for i in idx]), state+1 +function iterate(DPIter::DirectPowerIter, state=0) + if state >= DPIter.totalorder + return nothing + end + idx = Tuple(CartesianIndices(ntuple(i -> DPIter.orderG, DPIter.N))[state+1]) + return DirectProductGroupElem([DPIter.elts[i] for i in idx]), state+1 end -Base.done(DPIter::DirectPowerIter, state) = DPIter.totalorder <= state - -Base.eltype(::Type{DirectPowerIter{Gr, GrEl}}) where {Gr, GrEl} = DirectProductGroupElem{GrEl} +eltype(::Type{DirectPowerIter{GrEl}}) where {GrEl} = DirectProductGroupElem{GrEl} @doc doc""" elements(G::DirectProductGroup) diff --git a/src/Groups.jl b/src/Groups.jl index 3b52cd8..432185c 100644 --- a/src/Groups.jl +++ b/src/Groups.jl @@ -4,9 +4,9 @@ module Groups using AbstractAlgebra import AbstractAlgebra: Group, GroupElem, Ring import AbstractAlgebra: parent, parent_type, elem_type -import AbstractAlgebra: elements, order, gens, matrix_repr +import AbstractAlgebra: order, gens, matrix_repr -import Base: length, ==, hash, show, convert +import Base: length, ==, hash, show, convert, eltype, iterate import Base: inv, reduce, *, ^, power_by_squaring import Base: findfirst, findnext import Base: deepcopy_internal