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

rework hash, ==

This commit is contained in:
kalmarek 2018-03-28 12:28:47 +02:00
parent e6b1b2a663
commit eed218da3c

View File

@ -94,9 +94,12 @@ convert(::Type{GroupWord{T}}, s::T) where {T<:GSymbol} = GroupWord{T}(T[s])
############################################################################### ###############################################################################
function hash(W::GWord, h::UInt) function hash(W::GWord, h::UInt)
W.modified && reduce!(W) if W.modified
res = xor(W.savedhash, h) reduce!(W)
return res W.savedhash = hash(W.symbols, hash(typeof(W), hash(parent(W), zero(UInt))))
W.modified = false
end
return xor(W.savedhash, h)
end end
function deepcopy_internal(W::T, dict::ObjectIdDict) where {T<:GWord} function deepcopy_internal(W::T, dict::ObjectIdDict) where {T<:GWord}
@ -132,9 +135,9 @@ function reduce!(W::GWord)
reduced = free_reduce!(W) reduced = free_reduce!(W)
end end
end end
deleteat!(W.symbols, find(x -> x.pow == 0, W.symbols))
W.modified = true
W.modified = false
W.savedhash = hash(W.symbols, hash(typeof(W)))
return W return W
end end
@ -193,9 +196,15 @@ end
function (==)(W::GWord, Z::GWord) function (==)(W::GWord, Z::GWord)
parent(W) == parent(Z) || return false parent(W) == parent(Z) || return false
W.modified && reduce!(W) # reduce clears the flag and calculates savedhash
Z.modified && reduce!(Z) W.modified && hash(W)
return W.savedhash == Z.savedhash && W.symbols == Z.symbols Z.modified && hash(Z)
if W.savedhash != Z.savedhash
return false
end
return W.symbols == Z.symbols
end end
############################################################################### ###############################################################################