Skip to content

Commit

Permalink
Apply hash consing to Add
Browse files Browse the repository at this point in the history
  • Loading branch information
Blablablanca committed Nov 11, 2024
1 parent b1cee9e commit b656dd1
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 @@ -98,6 +98,7 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
@compactified obj::BasicSymbolic begin
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...)
_ => Unityper.rt_constructor(obj){T}(;nt_new...)
end
end
Expand Down Expand Up @@ -418,7 +419,8 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
end
end

Add{T}(; coeff, dict, hash=Ref(UInt(0)), metadata, arguments=[], issorted=RefValue(false), kw...)
s = Add{T}(; coeff, dict, hash=Ref(UInt(0)), metadata, arguments=[], issorted=RefValue(false), kw...)
BasicSymbolic(s)
end

function Mul(T, a, b; metadata=NO_METADATA, kw...)
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
using SymbolicUtils: Term, Add

struct Ctx1 end
struct Ctx2 end
Expand Down Expand Up @@ -40,3 +40,17 @@ end
tm1 = setmetadata(t1, Ctx1, "meta_1")
@test t1 !== tm1
end

# @testset "Add" begin
# d1 = a + b
# d2 = b + a
# @test d1 === d2
# d3 = b - 2 + a
# d4 = a + b - 2
# @test d3 === d4
# d5 = Add({Int},a,b)
# @test d5 !== d1

# dm1 = setmetadata(d1,Ctx1,"meta_1")
# @test d1 !== dm1
# end

0 comments on commit b656dd1

Please sign in to comment.