@@ -143,23 +143,28 @@ end
143143# # Assign index to nodes of a tree
144144# This will mirror a Node struct, rather
145145# than adding a new attribute to Node.
146- struct NodeIndex{T,D} <: AbstractNode{D}
146+ mutable struct NodeIndex{T,D} <: AbstractNode{D}
147147 degree:: UInt8 # 0 for constant/variable, 1 for cos/sin, 2 for +/* etc.
148148 val:: T # If is a constant, this stores the actual value
149149 # ------------------- (possibly undefined below)
150150 children:: NTuple{D,Base.RefValue{NodeIndex{T,D}}}
151151
152- NodeIndex (:: Type{_T} , :: Val{_D} ) where {_T,_D} = new {_T,_D} (0 , zero (_T))
153- NodeIndex (:: Type{_T} , :: Val{_D} , val) where {_T,_D} = new {_T,_D} (0 , convert (_T, val))
152+ function NodeIndex (:: Type{_T} , :: Val{_D} , val) where {_T,_D}
153+ return new {_T,_D} (
154+ 0 , convert (_T, val), ntuple (_ -> Ref {NodeIndex{_T,_D}} (), Val (_D))
155+ )
156+ end
154157 function NodeIndex (
155158 :: Type{_T} , :: Val{_D} , children:: Vararg{NodeIndex{_T,_D},_D2}
156159 ) where {_T,_D,_D2}
157160 _children = ntuple (
158161 i -> i <= _D2 ? Ref (children[i]) : Ref {NodeIndex{_T,_D}} (), Val (_D)
159162 )
160- return new {_T,_D} (1 , zero (_T), _children)
163+ return new {_T,_D} (convert (UInt8, _D2) , zero (_T), _children)
161164 end
162165end
166+ NodeIndex (:: Type{T} , :: Val{D} ) where {T,D} = NodeIndex (T, Val (D), zero (T))
167+
163168# Sharing is never needed for NodeIndex,
164169# as we trace over the node we are indexing on.
165170preserve_sharing (:: Union{Type{<:NodeIndex},NodeIndex} ) = false
0 commit comments