Skip to content

Commit 0a62a31

Browse files
authored
Merge pull request #171 from JuliaSymbolics/myb/hash
Only cache hash when salt is 0
2 parents f562129 + ced1829 commit 0a62a31

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.7.5"
4+
version = "0.7.6"
55

66
[deps]
77
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"

src/types.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,10 @@ hashvec(xs, z) = foldr(hash, xs, init=z)
297297

298298
function Base.hash(t::Term{T}, salt::UInt) where {T}
299299
h = t.hash[]
300-
!iszero(h) && return h
301-
t.hash[] = hashvec(arguments(t), hash(operation(t), hash(T, salt)))
300+
(iszero(salt) && !iszero(h)) && return h
301+
h′ = hashvec(arguments(t), hash(operation(t), hash(T, salt)))
302+
iszero(salt) && (t.hash[] = h′)
303+
return h′
302304
end
303305

304306
_promote_symtype(f::Sym, args) = promote_symtype(f, map(symtype, args)...)
@@ -454,12 +456,6 @@ function arguments(a::Add)
454456
a.sorted_args_cache[] = iszero(a.coeff) ? args : vcat(a.coeff, args)
455457
end
456458

457-
function Base.hash(a::Add, u::UInt64)
458-
h = a.hash[]
459-
!iszero(h) && return h
460-
a.hash[] = hash(0xaddaddaddaddadda, hash(a.coeff, hash(a.dict, u)))
461-
end
462-
463459
Base.isequal(a::Add, b::Add) = isequal(a.coeff, b.coeff) && isequal(a.dict, b.dict)
464460

465461
Base.show(io::IO, a::Add) = show_term(io, a)
@@ -582,15 +578,8 @@ function arguments(a::Mul)
582578
a.sorted_args_cache[] = isone(a.coeff) ? args : vcat(a.coeff, args)
583579
end
584580

585-
function Base.hash(m::Mul, u::UInt64)
586-
h = m.hash[]
587-
!iszero(h) && return h
588-
m.hash[] = hash(0xaaaaaaaaaaaaaaa, hash(m.coeff, hash(m.dict, u)))
589-
end
590-
591581
Base.isequal(a::Mul, b::Mul) = isequal(a.coeff, b.coeff) && isequal(a.dict, b.dict)
592582

593-
594583
Base.show(io::IO, a::Mul) = show_term(io, a)
595584

596585
function makemul(coeff, xs...; d=sdict())
@@ -729,3 +718,12 @@ function similarterm(p::Union{Mul, Add, Pow}, f, args)
729718
f(args...)
730719
end
731720
end
721+
722+
function Base.hash(t::Union{Add,Mul}, u::UInt64)
723+
h = t.hash[]
724+
(iszero(u) && !iszero(h)) && return h
725+
hashoffset = t isa Add ? 0xaddaddaddaddadda : 0xaddaddaddaddadda
726+
h′= hash(hashoffset, hash(t.coeff, hash(t.dict, u)))
727+
iszero(u) && (t.hash[] = h′)
728+
return h′
729+
end

test/basics.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,9 @@ end
103103
@test repr(2/(2*a)) == "a^-1"
104104
@test repr(Term(*, [1, 1])) == "1*1"
105105
end
106+
107+
@testset "hash" begin
108+
@syms a b
109+
@test hash(a + b, UInt(0)) === hash(a + b) === hash(a + b, UInt(0)) # test caching
110+
@test hash(a + b, UInt(2)) !== hash(a + b)
111+
end

0 commit comments

Comments
 (0)