Skip to content

Commit bad26f4

Browse files
author
Pietro Vertechi
authored
simplify collect a bit more and update docs (#104)
1 parent 2554538 commit bad26f4

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/collect.jl

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,41 @@ reshapestructarray(v::AbstractArray, d) = reshape(v, d)
3030
reshapestructarray(v::StructArray{T}, d) where {T} =
3131
StructArray{T}(map(x -> reshapestructarray(x, d), fieldarrays(v)))
3232

33-
function collect_empty_structarray(itr::T; initializer = default_initializer) where {T}
34-
S = Core.Compiler.return_type(first, Tuple{T})
35-
res = initializer(S, (0,))
36-
_reshape(res, itr)
37-
end
38-
3933
"""
40-
`collect_structarray(itr, fr=iterate(itr); initializer = default_initializer)`
34+
`collect_structarray(itr; initializer = default_initializer)`
4135
4236
Collects `itr` into a `StructArray`. The user can optionally pass a `initializer`, that is to say
4337
a function `(S, d) -> v` that associates to a type and a size an array of eltype `S`
4438
and size `d`. By default `initializer` returns a `StructArray` of `Array` but custom array types
45-
may be used. `fr` represents the moment in the iteration of `itr` from which to start collecting.
39+
may be used.
4640
"""
47-
collect_structarray(itr; initializer = default_initializer) =
48-
_collect_structarray(itr, Base.IteratorSize(itr); initializer = initializer)
49-
50-
function _collect_structarray(itr, sz::Union{Base.HasShape, Base.HasLength};
51-
initializer = default_initializer)
52-
len = length(itr)
41+
function collect_structarray(itr; initializer = default_initializer)
42+
len = Base.IteratorSize(itr) === Base.SizeUnknown() ? 1 : length(itr)
5343
elem = iterate(itr)
54-
elem === nothing && return collect_empty_structarray(itr, initializer = initializer)
44+
_collect_structarray(itr, elem, len; initializer = initializer)
45+
end
46+
47+
function _collect_structarray(itr::T, ::Nothing, len; initializer = default_initializer) where {T}
48+
S = Core.Compiler.return_type(first, Tuple{T})
49+
res = initializer(S, (0,))
50+
_reshape(res, itr)
51+
end
52+
53+
function _collect_structarray(itr, elem, len; initializer = default_initializer)
5554
el, st = elem
5655
S = typeof(el)
5756
dest = initializer(S, (len,))
5857
dest[1] = el
58+
return _collect_structarray!(dest, itr, st, Base.IteratorSize(itr))
59+
end
60+
61+
function _collect_structarray!(dest, itr, st, ::Union{Base.HasShape, Base.HasLength})
5962
v = collect_to_structarray!(dest, itr, 2, st)
60-
_reshape(v, itr, sz)
63+
return _reshape(v, itr)
6164
end
6265

63-
function _collect_structarray(itr, ::Base.SizeUnknown; initializer = default_initializer)
64-
elem = iterate(itr)
65-
elem === nothing && return collect_empty_structarray(itr, initializer = initializer)
66-
el, st = elem
67-
S = typeof(el)
68-
dest = initializer(S, (1,))
69-
dest[1] = el
66+
_collect_structarray!(dest, itr, st, ::Base.SizeUnknown) =
7067
grow_to_structarray!(dest, itr, iterate(itr, st))
71-
end
7268

7369
function collect_to_structarray!(dest::AbstractArray, itr, offs, st)
7470
# collect to dest array, checking the type of each result. if a result does not

0 commit comments

Comments
 (0)