1
0
mirror of https://github.com/kalmarek/Groups.jl.git synced 2024-12-26 02:20:30 +01:00

rework hashing using hints from FNV-1a algorithm

This commit is contained in:
kalmarek 2020-10-07 02:37:29 +02:00
parent 873361af73
commit 8532767170
No known key found for this signature in database
GPG Key ID: 8BF1A3855328FC15
2 changed files with 22 additions and 13 deletions

View File

@ -222,29 +222,38 @@ evaluate(f::Automorphism) = f(domain(parent(f)))
# hashing && equality # hashing && equality
# #
function hash_internal(g::Automorphism, images = freereduce!.(evaluate(g)), function hash_internal(
h::UInt = 0x7d28276b01874b19) # hash(Automorphism) g::Automorphism,
return hash(images, hash(parent(g), h)) h::UInt = 0x7d28276b01874b19; # hash(Automorphism)
# alternatively: 0xcbf29ce484222325 from FNV-1a algorithm
images = compute_images(g),
prime = 0x00000100000001b3, # prime from FNV-1a algorithm
)
return foldl((h,x) -> hash(x, h)*prime, images, init = hash(parent(g), h))
end end
function compute_images(g::Automorphism) function compute_images(g::Automorphism)
images = reduce!.(evaluate(g)) images = evaluate(g)
savehash!(g, hash_internal(g, images)) for im in images
unsetmodified!(g) reduce!(im)
end
return images return images
end end
function (==)(g::Automorphism{N}, h::Automorphism{N}) where N function (==)(g::Automorphism{N}, h::Automorphism{N}) where N
img_c, imh_c = false, false syllables(g) == syllables(h) && return true
img_computed, imh_computed = false, false
if ismodified(g) if ismodified(g)
img = compute_images(g) img = compute_images(g) # sets modified bit
img_c = true hash(g, images=img)
img_computed = true
end end
if ismodified(h) if ismodified(h)
imh = compute_images(h) imh = compute_images(h) # sets modified bit
imh_c = true hash(h, images=imh)
imh_computed = true
end end
@assert !ismodified(g) && !ismodified(h) @assert !ismodified(g) && !ismodified(h)

View File

@ -9,9 +9,9 @@ function hash_internal(W::GWord)
return hash(syllables(W), hash(typeof(W), h)) return hash(syllables(W), hash(typeof(W), h))
end end
function hash(W::GWord, h::UInt) function hash(W::GWord, h::UInt=UInt(0); kwargs...)
if ismodified(W) if ismodified(W)
savehash!(W, hash_internal(W)) savehash!(W, hash_internal(W; kwargs...))
unsetmodified!(W) unsetmodified!(W)
end end
return xor(savedhash(W), h) return xor(savedhash(W), h)