@@ -47,35 +47,35 @@ using Base.Broadcast: Broadcasted
47
47
return _broadcast (f, Size (map (length, ax)), argsizes, as... )
48
48
end
49
49
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
+
50
63
@inline function _broadcast (f, sz:: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where {newsize}
51
64
first_staticarray = first_statictype (a... )
52
65
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 )
54
67
eltys = Tuple{map (eltype, a)... }
55
68
(), Core. Compiler. return_type (f, eltys)
56
69
else
57
70
temp = __broadcast (f, sz, s, a... )
58
71
temp, eltype (temp)
59
72
end
60
73
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)))
69
76
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)). .. )
77
77
else
78
- return map (Base . Fix2 (getfield, i), x )
78
+ @inbounds return similar_type (first_staticarray, ET, sz)(elements )
79
79
end
80
80
end
81
81
0 commit comments