From 6939c0f00a254e6a6c36dafdca89a0562e0dc3f7 Mon Sep 17 00:00:00 2001 From: kalmarek Date: Sun, 25 Mar 2018 23:46:13 +0200 Subject: [PATCH] introduce GroupWord{T} <: GWord{T<:GSymbol} This allows subtyping of GWord{T} with more specific group elements --- src/FPGroups.jl | 2 +- src/FreeGroup.jl | 5 ++--- src/Groups.jl | 20 +++++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/FPGroups.jl b/src/FPGroups.jl index 59389bd..3901597 100644 --- a/src/FPGroups.jl +++ b/src/FPGroups.jl @@ -9,7 +9,7 @@ struct FPSymbol <: GSymbol pow::Int end -FPGroupElem = GWord{FPSymbol} +FPGroupElem = GroupWord{FPSymbol} mutable struct FPGroup <: AbstractFPGroup gens::Vector{FPSymbol} diff --git a/src/FreeGroup.jl b/src/FreeGroup.jl index 42bf748..1d2d641 100644 --- a/src/FreeGroup.jl +++ b/src/FreeGroup.jl @@ -9,7 +9,7 @@ struct FreeSymbol <: GSymbol pow::Int end -FreeGroupElem = GWord{FreeSymbol} +FreeGroupElem = GroupWord{FreeSymbol} mutable struct FreeGroup <: AbstractFPGroup gens::Vector{FreeSymbol} @@ -58,8 +58,7 @@ function (G::FreeGroup)() return id end -function (G::FreeGroup)(w::GWord) - eltype(w.symbols) == FreeSymbol || throw("Can not coerce $w to FreeGroup $G.") +function (G::FreeGroup)(w::GroupWord{FreeSymbol}) if length(w) > 0 for s in w.symbols i = findfirst(g -> g.str == s.str, G.gens) diff --git a/src/Groups.jl b/src/Groups.jl index 16c0b6f..b325791 100644 --- a/src/Groups.jl +++ b/src/Groups.jl @@ -43,13 +43,15 @@ doc""" """ -mutable struct GWord{T<:GSymbol} <: GroupElem +abstract type GWord{T<:GSymbol} <:GroupElem end + +mutable struct GroupWord{T} <: GWord{T} symbols::Vector{T} savedhash::UInt modified::Bool parent::Group - function GWord{T}(symbols::Vector{T}) where {T} + function GroupWord{T}(symbols::Vector{T}) where {T} return new{T}(symbols, hash(symbols), true) end end @@ -70,8 +72,8 @@ parent{T<:GSymbol}(w::GWord{T}) = w.parent # ############################################################################### -GWord(s::T) where {T} = GWord{T}(T[s]) -convert(::Type{GWord{T}}, s::T) where {T<:GSymbol} = GWord{T}(T[s]) +GroupWord(s::T) where {T<:GSymbol} = GroupWord{T}(T[s]) +convert(::Type{GroupWord{T}}, s::T) where {T<:GSymbol} = GroupWord{T}(T[s]) ############################################################################### # @@ -85,9 +87,9 @@ function hash(W::GWord, h::UInt) return res end -function deepcopy_internal(W::GWord{T}, dict::ObjectIdDict) where {T<:GSymbol} +function deepcopy_internal(W::T, dict::ObjectIdDict) where {T<:GWord} G = parent(W) - return G(GWord{T}(deepcopy(W.symbols))) + return G(T(deepcopy(W.symbols))) end isone(s::GSymbol) = s.pow == 0 @@ -255,12 +257,12 @@ end # ############################################################################### -function inv(W::GWord{T}) where {T} +function inv(W::T) where {T<:GWord} if length(W) == 0 return W else G = parent(W) - w = GWord{T}(reverse([inv(s) for s in W.symbols])) + w = T(reverse([inv(s) for s in W.symbols])) w.modified = true return G(w) end @@ -377,7 +379,7 @@ end # ############################################################################### -function generate_balls{T<:GroupElem}(S::Vector{T}, Id::T=parent(first(S))(); radius=2, op=*) +function generate_balls(S::Vector{T}, Id::T=parent(first(S))(); radius=2, op=*) where T<:GWord sizes = Int[] B = [Id] for i in 1:radius