1
0
mirror of https://github.com/kalmarek/Groups.jl.git synced 2024-11-19 14:35:28 +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
#
function hash_internal(g::Automorphism, images = freereduce!.(evaluate(g)),
h::UInt = 0x7d28276b01874b19) # hash(Automorphism)
return hash(images, hash(parent(g), h))
function hash_internal(
g::Automorphism,
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
function compute_images(g::Automorphism)
images = reduce!.(evaluate(g))
savehash!(g, hash_internal(g, images))
unsetmodified!(g)
images = evaluate(g)
for im in images
reduce!(im)
end
return images
end
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)
img = compute_images(g)
img_c = true
img = compute_images(g) # sets modified bit
hash(g, images=img)
img_computed = true
end
if ismodified(h)
imh = compute_images(h)
imh_c = true
imh = compute_images(h) # sets modified bit
hash(h, images=imh)
imh_computed = true
end
@assert !ismodified(g) && !ismodified(h)

View File

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