@@ -47,35 +47,35 @@ using Base.Broadcast: Broadcasted
4747 return _broadcast (f, Size (map (length, ax)), argsizes, as... )
4848end
4949
50+ # A functor generates the ith component of StructStaticBroadcast.
51+ struct Similar_ith{SA, E<: Tuple }
52+ elements:: E
53+ Similar_ith {SA} (elements:: Tuple ) where {SA} = new {SA, typeof(elements)} (elements)
54+ end
55+ function (s:: Similar_ith{SA} )(i:: Int ) where {SA}
56+ ith_elements = ntuple (Val (length (s. elements))) do j
57+ getfield (s. elements[j], i)
58+ end
59+ ith_SA = similar_type (SA, fieldtype (eltype (SA), i))
60+ return @inbounds ith_SA (ith_elements)
61+ end
62+
5063@inline function _broadcast (f, sz:: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where {newsize}
5164 first_staticarray = first_statictype (a... )
5265 elements, ET = if prod (newsize) == 0
53- # Use inference to get eltype in empty case (see also comments in _map )
66+ # Use inference to get eltype in empty case (following StaticBroadcast defined in StaticArrays.jl )
5467 eltys = Tuple{map (eltype, a)... }
5568 (), Core. Compiler. return_type (f, eltys)
5669 else
5770 temp = __broadcast (f, sz, s, a... )
5871 temp, eltype (temp)
5972 end
6073 if isnonemptystructtype (ET)
61- @static if VERSION >= v " 1.7"
62- arrs = ntuple (Val (fieldcount (ET))) do i
63- @inbounds similar_type (first_staticarray, fieldtype (ET, i), sz)(_getfields (elements, i))
64- end
65- else
66- similarET (:: Type{SA} , :: Type{T} ) where {SA, T} = i -> @inbounds similar_type (SA, fieldtype (T, i), sz)(_getfields (elements, i))
67- arrs = ntuple (similarET (first_staticarray, ET), Val (fieldcount (ET)))
68- end
74+ SA = similar_type (first_staticarray, ET, sz)
75+ arrs = ntuple (Similar_ith {SA} (elements), Val (fieldcount (ET)))
6976 return StructArray {ET} (arrs)
70- end
71- @inbounds return similar_type (first_staticarray, ET, sz)(elements)
72- end
73-
74- @inline function _getfields (x:: Tuple , i:: Int )
75- if @generated
76- return Expr (:tuple , (:(getfield (x[$ j], i)) for j in 1 : fieldcount (x)). .. )
7777 else
78- return map (Base . Fix2 (getfield, i), x )
78+ @inbounds return similar_type (first_staticarray, ET, sz)(elements )
7979 end
8080end
8181
0 commit comments