@@ -22,11 +22,13 @@ const EMPTY_HASH = RefValue(UInt(0))
2222const EMPTY_DICT = sdict ()
2323const EMPTY_DICT_T = typeof (EMPTY_DICT)
2424const ENABLE_HASHCONSING = Ref (true )
25+ const TID = Union{IDType, Nothing}
26+ const DID = nothing
2527
2628@compactify show_methods= false begin
2729 @abstract mutable struct BasicSymbolic{T} <: Symbolic{T}
2830 metadata:: Metadata = NO_METADATA
29- id:: RefValue{UInt64 } = Ref {UInt64} ( 0 )
31+ id:: RefValue{TID } = Ref {TID} (DID )
3032 end
3133 mutable struct Sym{T} <: BasicSymbolic{T}
3234 name:: Symbol = :OOF
@@ -88,8 +90,6 @@ function exprtype(x::BasicSymbolic)
8890 end
8991end
9092
91- const wvd = TaskLocalValue {WeakValueDict{UInt, BasicSymbolic}} (WeakValueDict{UInt, BasicSymbolic})
92-
9393# Same but different error messages
9494@noinline error_on_type () = error (" Internal error: unreachable reached!" )
9595@noinline error_sym () = error (" Sym doesn't have a operation or arguments!" )
@@ -108,11 +108,11 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
108108 # Call outer constructor because hash consing cannot be applied in inner constructor
109109 @compactified obj:: BasicSymbolic begin
110110 Sym => Sym {T} (nt_new. name; nt_new... )
111- Term => Term {T} (nt_new. f, nt_new. arguments; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {UInt64} ( 0 ))
112- Add => Add (T, nt_new. coeff, nt_new. dict; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {UInt64} ( 0 ))
113- Mul => Mul (T, nt_new. coeff, nt_new. dict; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {UInt64} ( 0 ))
114- Div => Div {T} (nt_new. num, nt_new. den, nt_new. simplified; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {UInt64} ( 0 ))
115- Pow => Pow {T} (nt_new. base, nt_new. exp; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {UInt64} ( 0 ))
111+ Term => Term {T} (nt_new. f, nt_new. arguments; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {TID} (DID ))
112+ Add => Add (T, nt_new. coeff, nt_new. dict; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {TID} (DID ))
113+ Mul => Mul (T, nt_new. coeff, nt_new. dict; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {TID} (DID ))
114+ Div => Div {T} (nt_new. num, nt_new. den, nt_new. simplified; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {TID} (DID ))
115+ Pow => Pow {T} (nt_new. base, nt_new. exp; nt_new... , hash = RefValue (UInt (0 )), hash2 = RefValue (UInt (0 )), id = Ref {TID} (DID ))
116116 _ => Unityper. rt_constructor (obj){T}(;nt_new... )
117117 end
118118end
@@ -516,11 +516,11 @@ end
516516# ## Constructors
517517# ##
518518
519- mutable struct AtomicIDCounter
520- @atomic x:: UInt64
521- end
519+ const wvd = TaskLocalValue {WeakValueDict{UInt, BasicSymbolic}} (WeakValueDict{UInt, BasicSymbolic})
522520
523- const ID_COUNTER = AtomicIDCounter (0 )
521+ function generate_id ()
522+ return IDType ()
523+ end
524524
525525"""
526526$(TYPEDSIGNATURES)
@@ -552,27 +552,27 @@ function BasicSymbolic(s::BasicSymbolic)::BasicSymbolic
552552 h = hash2 (s)
553553 k = get! (cache, h, s)
554554 if isequal_with_metadata (k, s)
555- if iszero (k. id[])
556- k. id[] = @atomic ID_COUNTER . x += 1
555+ if isnothing (k. id[])
556+ k. id[] = generate_id ()
557557 end
558558 return k
559559 else
560- if iszero (s. id[])
561- s. id[] = @atomic ID_COUNTER . x += 1
560+ if isnothing (s. id[])
561+ s. id[] = generate_id ()
562562 end
563563 return s
564564 end
565565end
566566
567567function Sym {T} (name:: Symbol ; kw... ) where {T}
568- s = Sym {T} (; name, kw... , id = Ref {UInt} ( 0 ))
568+ s = Sym {T} (; name, kw... , id = Ref {TID} (DID ))
569569 BasicSymbolic (s)
570570end
571571
572572function Term {T} (f, args; kw... ) where T
573573 args = SmallV {Any} (args)
574574
575- s = Term {T} (;f= f, arguments= args, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), kw... , id = Ref {UInt64} ( 0 ))
575+ s = Term {T} (;f= f, arguments= args, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), kw... , id = Ref {TID} (DID ))
576576 BasicSymbolic (s)
577577end
578578
@@ -602,7 +602,7 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
602602 end
603603 end
604604
605- s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= SmallV {Any} (), kw... , id = Ref {UInt64} ( 0 ))
605+ s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= SmallV {Any} (), kw... , id = Ref {TID} (DID ))
606606 BasicSymbolic (s)
607607end
608608
@@ -620,7 +620,7 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
620620 else
621621 coeff = a
622622 dict = b
623- s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= SmallV {Any} (), kw... , id = Ref {UInt64} ( 0 ))
623+ s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= SmallV {Any} (), kw... , id = Ref {TID} (DID ))
624624 BasicSymbolic (s)
625625 end
626626end
@@ -688,7 +688,7 @@ function Div{T}(n, d, simplified=false; metadata=nothing, kwargs...) where {T}
688688 end
689689 end
690690
691- s = Div {T} (; num= n, den= d, simplified, arguments= SmallV {Any} (), metadata, id = Ref {UInt64} ( 0 ))
691+ s = Div {T} (; num= n, den= d, simplified, arguments= SmallV {Any} (), metadata, id = Ref {TID} (DID ))
692692 BasicSymbolic (s)
693693end
694694
@@ -708,7 +708,7 @@ function Pow{T}(a, b; metadata=NO_METADATA, kwargs...) where {T}
708708 b = unwrap (b)
709709 _iszero (b) && return 1
710710 _isone (b) && return a
711- s = Pow {T} (; base= a, exp= b, arguments= SmallV {Any} (), metadata, id = Ref {UInt64} ( 0 ))
711+ s = Pow {T} (; base= a, exp= b, arguments= SmallV {Any} (), metadata, id = Ref {TID} (DID ))
712712 BasicSymbolic (s)
713713end
714714
0 commit comments