1
- default_array (:: Type{S} , d) where {S} = Array {S} (undef , d)
1
+ default_array (:: Type{S} , d:: NTuple{N, Any} ) where {S, N } = similar ( Array{S, N} , d)
2
2
3
3
struct StructArrayInitializer{F, G}
4
4
unwrap:: F
@@ -21,14 +21,10 @@ ArrayInitializer(unwrap = t->false) = ArrayInitializer(unwrap, default_array)
21
21
22
22
(s:: ArrayInitializer )(S, d) = s. unwrap (S) ? buildfromschema (typ -> s (typ, d), S) : s. default_array (S, d)
23
23
24
- _reshape (v, itr) = _reshape (v, itr, Base. IteratorSize (itr))
25
- _reshape (v, itr, :: Base.HasShape ) = reshapestructarray (v, axes (itr))
26
- _reshape (v, itr, :: Union{Base.HasLength, Base.SizeUnknown} ) = v
27
-
28
- # temporary workaround before it gets easier to support reshape with offset axis
29
- reshapestructarray (v:: AbstractArray , d) = reshape (v, d)
30
- reshapestructarray (v:: StructArray{T} , d) where {T} =
31
- StructArray {T} (map (x -> reshapestructarray (x, d), fieldarrays (v)))
24
+ _axes (itr) = _axes (itr, Base. IteratorSize (itr))
25
+ _axes (itr, :: Base.SizeUnknown ) = nothing
26
+ _axes (itr, :: Base.HasLength ) = (Base. OneTo (length (itr)),)
27
+ _axes (itr, :: Base.HasShape ) = axes (itr)
32
28
33
29
"""
34
30
`collect_structarray(itr; initializer = default_initializer)`
@@ -39,31 +35,31 @@ and size `d`. By default `initializer` returns a `StructArray` of `Array` but cu
39
35
may be used.
40
36
"""
41
37
function collect_structarray (itr; initializer = default_initializer)
42
- len = Base . IteratorSize (itr) === Base . SizeUnknown () ? 1 : length (itr)
38
+ ax = _axes (itr)
43
39
elem = iterate (itr)
44
- _collect_structarray (itr, elem, len ; initializer = initializer)
40
+ _collect_structarray (itr, elem, ax ; initializer = initializer)
45
41
end
46
42
47
- function _collect_structarray (itr:: T , :: Nothing , len ; initializer = default_initializer) where {T}
43
+ function _collect_structarray (itr:: T , :: Nothing , ax ; initializer = default_initializer) where {T}
48
44
S = Core. Compiler. return_type (first, Tuple{T})
49
- res = initializer (S, (0 ,))
50
- _reshape (res, itr)
45
+ return initializer (S, something (ax, (Base. OneTo (0 ),)))
51
46
end
52
47
53
- function _collect_structarray (itr, elem, len ; initializer = default_initializer)
48
+ function _collect_structarray (itr, elem, ax ; initializer = default_initializer)
54
49
el, st = elem
55
50
S = typeof (el)
56
- dest = initializer (S, (len,))
57
- @inbounds dest[1 ] = el
58
- return _collect_structarray! (dest, itr, st, Base. IteratorSize (itr))
51
+ dest = initializer (S, something (ax, (Base. OneTo (1 ),)))
52
+ offs = first (LinearIndices (dest))
53
+ @inbounds dest[offs] = el
54
+ return _collect_structarray! (dest, itr, st, ax)
59
55
end
60
56
61
- function _collect_structarray! (dest, itr, st, :: Union{Base.HasShape, Base.HasLength} )
62
- v = collect_to_structarray! ( dest, itr, 2 , st)
63
- return _reshape (v , itr)
57
+ function _collect_structarray! (dest, itr, st, ax )
58
+ offs = first ( LinearIndices ( dest)) + 1
59
+ return collect_to_structarray! (dest , itr, offs, st )
64
60
end
65
61
66
- _collect_structarray! (dest, itr, st, :: Base.SizeUnknown ) =
62
+ _collect_structarray! (dest, itr, st, :: Nothing ) =
67
63
grow_to_structarray! (dest, itr, iterate (itr, st))
68
64
69
65
function collect_to_structarray! (dest:: AbstractArray , itr, offs, st)
@@ -122,7 +118,7 @@ _widenstructarray(dest::AbstractArray, i, ::Type{T}) where {T} = _widenarray(des
122
118
_widenarray (dest:: AbstractArray{T} , i, :: Type{T} ) where {T} = dest
123
119
function _widenarray (dest:: AbstractArray , i, :: Type{T} ) where T
124
120
new = similar (dest, T, length (dest))
125
- copyto! (new, 1 , dest, 1 , i- 1 )
121
+ copyto! (new, firstindex (new) , dest, firstindex (dest) , i- 1 )
126
122
new
127
123
end
128
124
0 commit comments