Skip to content

Commit ef21985

Browse files
author
Pietro Vertechi
authored
document widen (#105)
1 parent b24ce59 commit ef21985

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/collect.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function _collect_structarray(itr, elem, len; initializer = default_initializer)
5454
el, st = elem
5555
S = typeof(el)
5656
dest = initializer(S, (len,))
57-
dest[1] = el
57+
@inbounds dest[1] = el
5858
return _collect_structarray!(dest, itr, st, Base.IteratorSize(itr))
5959
end
6060

@@ -78,7 +78,7 @@ function collect_to_structarray!(dest::AbstractArray, itr, offs, st)
7878
@inbounds dest[i] = el
7979
i += 1
8080
else
81-
new = widenstructarray(dest, i, el)
81+
new = widen(dest, i, el)
8282
@inbounds new[i] = el
8383
return collect_to_structarray!(new, itr, i+1, st)
8484
end
@@ -97,29 +97,30 @@ function grow_to_structarray!(dest::AbstractArray, itr, elem = iterate(itr))
9797
elem = iterate(itr, st)
9898
i += 1
9999
else
100-
new = widenstructarray(dest, i, el)
100+
new = widen(dest, i, el)
101101
push!(new, el)
102102
return grow_to_structarray!(new, itr, iterate(itr, st))
103103
end
104104
end
105105
return dest
106106
end
107107

108-
widenstructarray(dest::AbstractArray{S}, i, el::T) where {S, T} = widenstructarray(dest, i, _promote_typejoin(S, T))
108+
# Widen `dest` to contain `el` and copy until index `i-1`
109+
widen(dest::AbstractArray{S}, i, el::T) where {S, T} = _widenstructarray(dest, i, _promote_typejoin(S, T))
109110

110-
function widenstructarray(dest::StructArray, i, ::Type{T}) where {T}
111+
function _widenstructarray(dest::StructArray, i, ::Type{T}) where {T}
111112
sch = hasfields(T) ? staticschema(T) : nothing
112-
sch !== nothing && fieldnames(sch) == propertynames(dest) || return widenarray(dest, i, T)
113+
sch !== nothing && fieldnames(sch) == propertynames(dest) || return _widenarray(dest, i, T)
113114
types = ntuple(x -> fieldtype(sch, x), fieldcount(sch))
114115
cols = Tuple(fieldarrays(dest))
115-
newcols = map((a, b) -> widenstructarray(a, i, b), cols, types)
116+
newcols = map((a, b) -> _widenstructarray(a, i, b), cols, types)
116117
return StructArray{T}(newcols)
117118
end
118119

119-
widenstructarray(dest::AbstractArray, i, ::Type{T}) where {T} = widenarray(dest, i, T)
120+
_widenstructarray(dest::AbstractArray, i, ::Type{T}) where {T} = _widenarray(dest, i, T)
120121

121-
widenarray(dest::AbstractArray{T}, i, ::Type{T}) where {T} = dest
122-
function widenarray(dest::AbstractArray, i, ::Type{T}) where T
122+
_widenarray(dest::AbstractArray{T}, i, ::Type{T}) where {T} = dest
123+
function _widenarray(dest::AbstractArray, i, ::Type{T}) where T
123124
new = similar(dest, T, length(dest))
124125
copyto!(new, 1, dest, 1, i-1)
125126
new
@@ -148,7 +149,7 @@ function _append!!(dest::AbstractVector, itr, ::Union{Base.HasShape, Base.HasLen
148149
fr === nothing && return dest
149150
el, st = fr
150151
i = lastindex(dest) + 1
151-
new = iscompatible(el, dest) ? dest : widenstructarray(dest, i, el)
152+
new = iscompatible(el, dest) ? dest : widen(dest, i, el)
152153
resize!(new, length(dest) + n)
153154
@inbounds new[i] = el
154155
return collect_to_structarray!(new, itr, i + 1, st)

0 commit comments

Comments
 (0)