From 7139322d052109eb03f681d9a3bb038af956028c Mon Sep 17 00:00:00 2001 From: kalmarek Date: Tue, 27 Mar 2018 20:14:24 +0200 Subject: [PATCH] New version of hash and == evaluation of Automorphism on the standard basis is expensive in hash(g::Automorphism, h::UInt) we 1. compute and store savedhash (evaluating f, if necessary) with a fixed value HASHINGCONST 2. xor h with savedhash in == we evaluate f only at hash-colision (or when This way we evaluate f multiple times only in ~2% of cases (for SAut(F_4)) --- src/AutGroup.jl | 59 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/AutGroup.jl b/src/AutGroup.jl index f349c46..3856244 100644 --- a/src/AutGroup.jl +++ b/src/AutGroup.jl @@ -218,20 +218,54 @@ end ############################################################################### # -# Basic manipulation +# Comparison # ############################################################################### +const HASHINGCONST = 0x314a5215305f3ec1 # more or less random + hash(s::AutSymbol, h::UInt) = hash(s.str, hash(s.pow, hash(:AutSymbol, h))) function hash(g::Automorphism, h::UInt) - if g.modified - g.savedhash = hash(g(domain(parent(g))), hash(typeof(g), hash(parent(g), h))) - g.modified = false - end - return g.savedhash + if g.modified + g.savedhash = hash(reduce!.(g(domain(parent(g)))), + hash(typeof(g), hash(parent(g), HASHINGCONST))) + g.modified = false + end + return xor(g.savedhash, h) end +(==)(s::AutSymbol, t::AutSymbol) = s.str == t.str && s.pow == t.pow + +function (==)(g::Automorphism{N}, h::Automorphism{N}) where N + parent(g) == parent(h) || return false + + if !g.modified && !h.modified + if g.savedhash != h.savedhash + return false + end + end + + # expensive: + g_im = reduce!.(g(domain(parent(g)))) + h_im = reduce!.(h(domain(parent(h)))) + # cheap: + g.savedhash = hash(g_im, + hash(typeof(g), hash(parent(g), HASHINGCONST))) + g.modified = false + h.savedhash = hash(h_im, + hash(typeof(h), hash(parent(h), HASHINGCONST))) + h.modified = false + + return g_im == h_im +end + +############################################################################### +# +# Basic manipulation +# +############################################################################### + function change_pow(s::AutSymbol, n::Int) if n == 0 return id_autsymbol() @@ -266,19 +300,6 @@ function show(io::IO, G::AutGroup) print(io, "Generated by $(join(G.gens, ","))") end -############################################################################### -# -# Comparison -# -############################################################################### - -(==)(s::AutSymbol, t::AutSymbol) = s.str == t.str && s.pow == t.pow - -function (==)(g::Automorphism, h::Automorphism) - parent(g) == parent(h) || return false - return g(domain(parent(g))) == h(domain(parent(h))) -end - ############################################################################### # # Binary operators