@@ -22,11 +22,13 @@ const EMPTY_HASH = RefValue(UInt(0))
22
22
const EMPTY_DICT = sdict ()
23
23
const EMPTY_DICT_T = typeof (EMPTY_DICT)
24
24
const ENABLE_HASHCONSING = Ref (true )
25
+ const TID = Union{IDType, Nothing}
26
+ const DID = nothing
25
27
26
28
@compactify show_methods= false begin
27
29
@abstract mutable struct BasicSymbolic{T} <: Symbolic{T}
28
30
metadata:: Metadata = NO_METADATA
29
- id:: RefValue{UInt64 } = Ref {UInt64} ( 0 )
31
+ id:: RefValue{TID } = Ref {TID} (DID )
30
32
end
31
33
mutable struct Sym{T} <: BasicSymbolic{T}
32
34
name:: Symbol = :OOF
@@ -88,8 +90,6 @@ function exprtype(x::BasicSymbolic)
88
90
end
89
91
end
90
92
91
- const wvd = TaskLocalValue {WeakValueDict{UInt, BasicSymbolic}} (WeakValueDict{UInt, BasicSymbolic})
92
-
93
93
# Same but different error messages
94
94
@noinline error_on_type () = error (" Internal error: unreachable reached!" )
95
95
@noinline error_sym () = error (" Sym doesn't have a operation or arguments!" )
@@ -108,11 +108,11 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
108
108
# Call outer constructor because hash consing cannot be applied in inner constructor
109
109
@compactified obj:: BasicSymbolic begin
110
110
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 ))
116
116
_ => Unityper. rt_constructor (obj){T}(;nt_new... )
117
117
end
118
118
end
@@ -516,11 +516,11 @@ end
516
516
# ## Constructors
517
517
# ##
518
518
519
- mutable struct AtomicIDCounter
520
- @atomic x:: UInt64
521
- end
519
+ const wvd = TaskLocalValue {WeakValueDict{UInt, BasicSymbolic}} (WeakValueDict{UInt, BasicSymbolic})
522
520
523
- const ID_COUNTER = AtomicIDCounter (0 )
521
+ function generate_id ()
522
+ return IDType ()
523
+ end
524
524
525
525
"""
526
526
$(TYPEDSIGNATURES)
@@ -552,27 +552,27 @@ function BasicSymbolic(s::BasicSymbolic)::BasicSymbolic
552
552
h = hash2 (s)
553
553
k = get! (cache, h, s)
554
554
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 ()
557
557
end
558
558
return k
559
559
else
560
- if iszero (s. id[])
561
- s. id[] = @atomic ID_COUNTER . x += 1
560
+ if isnothing (s. id[])
561
+ s. id[] = generate_id ()
562
562
end
563
563
return s
564
564
end
565
565
end
566
566
567
567
function 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 ))
569
569
BasicSymbolic (s)
570
570
end
571
571
572
572
function Term {T} (f, args; kw... ) where T
573
573
args = SmallV {Any} (args)
574
574
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 ))
576
576
BasicSymbolic (s)
577
577
end
578
578
@@ -602,7 +602,7 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
602
602
end
603
603
end
604
604
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 ))
606
606
BasicSymbolic (s)
607
607
end
608
608
@@ -620,7 +620,7 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
620
620
else
621
621
coeff = a
622
622
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 ))
624
624
BasicSymbolic (s)
625
625
end
626
626
end
@@ -688,7 +688,7 @@ function Div{T}(n, d, simplified=false; metadata=nothing, kwargs...) where {T}
688
688
end
689
689
end
690
690
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 ))
692
692
BasicSymbolic (s)
693
693
end
694
694
@@ -708,7 +708,7 @@ function Pow{T}(a, b; metadata=NO_METADATA, kwargs...) where {T}
708
708
b = unwrap (b)
709
709
_iszero (b) && return 1
710
710
_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 ))
712
712
BasicSymbolic (s)
713
713
end
714
714
0 commit comments