Skip to content

Commit

Permalink
Apply hash consing to Mul
Browse files Browse the repository at this point in the history
  • Loading branch information
Blablablanca committed Nov 12, 2024
1 parent 25d37f1 commit e44a457
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
Sym => Sym{T}(nt_new.name; nt_new...)
Term => Term{T}(nt_new.f, nt_new.arguments; nt_new...)
Add => Add(T, nt_new.coeff, nt_new.dict; nt_new...)
Mul => Mul(T, nt_new.coeff, nt_new.dict; nt_new...)
_ => Unityper.rt_constructor(obj){T}(;nt_new...)
end
end
Expand Down Expand Up @@ -435,7 +436,8 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
else
coeff = a
dict = b
Mul{T}(; coeff, dict, hash=Ref(UInt(0)), metadata, arguments=[], issorted=RefValue(false), kw...)
s = Mul{T}(; coeff, dict, hash=Ref(UInt(0)), metadata, arguments=[], issorted=RefValue(false), kw...)
BasicSymbolic(s)
end
end

Expand Down
16 changes: 15 additions & 1 deletion test/hash_consing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SymbolicUtils, Test
using SymbolicUtils: Term, Add
using SymbolicUtils: Term, Add, Mul

struct Ctx1 end
struct Ctx2 end
Expand Down Expand Up @@ -54,3 +54,17 @@ end
dm1 = setmetadata(d1,Ctx1,"meta_1")
@test d1 !== dm1
end

@testset "Mul" begin
m1 = a*b
m2 = b*a
@test m1 === m2
m3 = 6*a*b
m4 = 3*a*2*b
@test m3 === m4
m5 = Mul(Int, 1, Dict(a => 1, b => 1))
@test m5 !== m1

mm1 = setmetadata(m1, Ctx1, "meta_1")
@test m1 !== mm1
end

0 comments on commit e44a457

Please sign in to comment.