@@ -35,8 +35,23 @@ julia> ArrayInterfaceCore.map_tuple_type(sqrt, Tuple{1,4,16})
35
35
36
36
```
37
37
"""
38
- @inline function map_tuple_type (f, @nospecialize (T:: Type ))
39
- ntuple (i -> f (fieldtype (T, i)), Val {fieldcount(T)} ())
38
+ function map_tuple_type end
39
+ if VERSION >= v " 1.8"
40
+ @inline function map_tuple_type (f, @nospecialize (T:: Type ))
41
+ ntuple (i -> f (fieldtype (T, i)), Val {fieldcount(T)} ())
42
+ end
43
+ else
44
+ function map_tuple_type (f:: F , :: Type{T} ) where {F,T<: Tuple }
45
+ if @generated
46
+ t = Expr (:tuple )
47
+ for i in 1 : fieldcount (T)
48
+ push! (t. args, :(f ($ (fieldtype (T, i)))))
49
+ end
50
+ Expr (:block , Expr (:meta , :inline ), t)
51
+ else
52
+ Tuple (f (fieldtype (T, i)) for i in 1 : fieldcount (T))
53
+ end
54
+ end
40
55
end
41
56
42
57
"""
@@ -58,20 +73,45 @@ julia> ArrayInterfaceCore.flatten_tuples((1, (2, (3,))))
58
73
59
74
```
60
75
"""
61
- function flatten_tuples (t:: Tuple )
62
- fields = _new_field_positions (t)
63
- ntuple (Val {nfields(fields)} ()) do k
64
- i, j = getfield (fields, k)
65
- i = length (t) - i
66
- @inbounds j === 0 ? getfield (t, i) : getfield (getfield (t, i), j)
76
+ function flatten_tuples end
77
+ if VERSION >= v " 1.8"
78
+ function flatten_tuples (t:: Tuple )
79
+ fields = _new_field_positions (t)
80
+ ntuple (Val {nfields(fields)} ()) do k
81
+ i, j = getfield (fields, k)
82
+ i = length (t) - i
83
+ @inbounds j === 0 ? getfield (t, i) : getfield (getfield (t, i), j)
84
+ end
85
+ end
86
+ _new_field_positions (:: Tuple{} ) = ()
87
+ @nospecialize
88
+ _new_field_positions (x:: Tuple ) = (_fl1 (x, x[1 ])... , _new_field_positions (Base. tail (x))... )
89
+ _fl1 (x:: Tuple , x1:: Tuple ) = ntuple (Base. Fix1 (tuple, length (x) - 1 ), Val (length (x1)))
90
+ _fl1 (x:: Tuple , x1) = ((length (x) - 1 , 0 ),)
91
+ @specialize
92
+ else
93
+ @inline function flatten_tuples (t:: Tuple )
94
+ if @generated
95
+ texpr = Expr (:tuple )
96
+ for i in 1 : fieldcount (t)
97
+ p = fieldtype (t, i)
98
+ if p <: Tuple
99
+ for j in 1 : fieldcount (p)
100
+ push! (texpr. args, :(@inbounds (getfield (getfield (t, $ i), $ j))))
101
+ end
102
+ else
103
+ push! (texpr. args, :(@inbounds (getfield (t, $ i))))
104
+ end
105
+ end
106
+ Expr (:block , Expr (:meta , :inline ), texpr)
107
+ else
108
+ _flatten (t)
109
+ end
67
110
end
111
+ _flatten (:: Tuple{} ) = ()
112
+ @inline _flatten (t:: Tuple{Any,Vararg{Any}} ) = (getfield (t, 1 ), _flatten (Base. tail (t))... )
113
+ @inline _flatten (t:: Tuple{Tuple,Vararg{Any}} ) = (getfield (t, 1 )... , _flatten (Base. tail (t))... )
68
114
end
69
- _new_field_positions (:: Tuple{} ) = ()
70
- @nospecialize
71
- _new_field_positions (x:: Tuple ) = (_fl1 (x, x[1 ])... , _new_field_positions (Base. tail (x))... )
72
- _fl1 (x:: Tuple , x1:: Tuple ) = ntuple (Base. Fix1 (tuple, length (x) - 1 ), Val (length (x1)))
73
- _fl1 (x:: Tuple , x1) = ((length (x) - 1 , 0 ),)
74
- @specialize
75
115
76
116
"""
77
117
parent_type(::Type{T}) -> Type
0 commit comments