-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnzip.jl
357 lines (320 loc) · 9.14 KB
/
Unzip.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
module Unzip
using Base:
_collect,
collect_to_with_first!,
@default_eltype,
grow_to!,
EltypeUnknown,
HasEltype,
HasLength,
HasShape,
IteratorEltype,
IteratorSize,
isabstracttype,
isvatuple,
OneTo,
push_widen,
@propagate_inbounds,
@pure,
setindex_widen_up_to,
_similar_for,
_similar_shape,
SizeUnknown,
tail,
vect
include("get_val_fieldtypes.jl")
struct Rows{Row, Dimensions, ModelColumn, Columns} <: AbstractArray{Row, Dimensions}
model_column::ModelColumn
columns::Columns
end
function check_axes(column, model_axes)
column_axes = axes(column)
if column_axes != model_axes
throw(DimensionMismatch("$column does not have model axes $model_axes"))
end
nothing
end
@inline @propagate_inbounds function Rows(
first_column,
other_columns...;
model_column = similar(first_column, Nothing),
)
model_axes = axes(model_column)
columns = (first_column, other_columns...)
@boundscheck map(let model_axes = model_axes
column -> check_axes(column, model_axes)
end, columns)
Rows{
Tuple{map(eltype, columns)...},
length(model_axes),
typeof(model_column),
typeof(columns),
}(
model_column,
columns,
)
end
# must specify a model column if no other columns
function Rows(; model_column)
Rows{Tuple{}, ndims(model_column), typeof(model_column), Tuple{}}(model_column, ())
end
function Base.axes(rows::Rows)
axes(rows.model_column)
end
function Base.size(rows::Rows)
size(rows.model_column)
end
@inline @propagate_inbounds function Base.getindex(rows::Rows, index...)
map(let index = index
@inline @propagate_inbounds function (column)
column[index...]
end
end, rows.columns)
end
@inline @propagate_inbounds function Base.setindex!(rows::Rows, row, index...)
map(let index = index
@inline @propagate_inbounds function (column, value)
column[index...] = value
end
end, rows.columns, row)
nothing
end
function default_similar(rows, ::Type{ARow}, dimensions) where {ARow}
@inbounds Rows(
map(
let model = rows.model_column, dimensions = dimensions
function (::Val{Value},) where {Value}
similar(model, Value, dimensions)
end
end,
get_val_fieldtypes(ARow),
)...;
model_column = similar(rows.model_column, Nothing, dimensions),
)
end
function Base.similar(rows::Rows, ::Type{ARow}, dimensions) where {ARow}
default_similar(rows, ARow, dimensions)
end
# disambiguation methods
const SomeOf{AType} = Tuple{AType, Vararg{AType}} where {AType}
function Base.similar(
rows::Rows,
::Type{ARow},
dimensions::Union{Integer, AbstractUnitRange},
) where {ARow}
default_similar(rows, ARow, dimensions)
end
function Base.similar(rows::Rows, ::Type{ARow}, dimensions::SomeOf{Int64}) where {ARow}
default_similar(rows, ARow, dimensions)
end
function Base.similar(
rows::Rows,
::Type{ARow},
dimensions::SomeOf{Union{Integer, OneTo}},
) where {ARow}
default_similar(rows, ARow, dimensions)
end
function Base.similar(
rows::Rows,
::Type{ARow},
dimensions::SomeOf{Union{Integer, AbstractUnitRange}},
) where {ARow}
default_similar(rows, ARow, dimensions)
end
function zip_missing(::Tuple{}, ::Tuple{})
()
end
function zip_missing(::Tuple{}, longer)
map(function (second_one)
(missing, second_one)
end, longer)
end
function zip_missing(longer, ::Tuple{})
map(function (first_one)
(first_one, missing)
end, longer)
end
function zip_missing(tuple1, tuple2)
(first(tuple1), first(tuple2)), zip_missing(tail(tuple1), tail(tuple2))...
end
function widen_column(
_,
next_index,
column::Array{OldItem},
item::Item,
) where {OldItem, Item <: OldItem}
@inbounds column[next_index] = item
column
end
function widen_column(_, next_index, column::Array, item)
setindex_widen_up_to(column, item, next_index)
end
function widen_column(rows, next_index, ::Missing, item::Item) where {Item}
new_column = similar(rows.model_column, Union{Missing, Item})
@inbounds new_column[next_index] = item
new_column
end
function widen_column(rows, __, ::Missing, ::Missing)
similar(rows.model_column, Missing)
end
function Base.setindex_widen_up_to(rows::Rows, row, next_index)
@inbounds Rows(
map(
let rows = rows, next_index = next_index
function (column_item,)
widen_column(rows, next_index, column_item...)
end
end,
zip_missing(rows.columns, row),
)...,
)
end
function push_widen_column(
_,
column::Array{OldItem},
item::Item,
) where {OldItem, Item <: OldItem}
push!(column, item)
column
end
function push_widen_column(_, column::Array, item)
push_widen(column, item)
end
function push_widen_column(rows, ::Missing, item::Item) where {Item}
new_index = length(rows) + 1
new_column = Array{Union{Missing, Item}}(missing, new_index)
@inbounds new_column[new_index] = item
new_column
end
function push_widen_column(rows, ::Missing, ::Missing)
Array{Missing}(missing, length(rows) + 1)
end
function Base.push_widen(rows::Rows, row)
model_column = rows.model_column
# do this before we push into the model column
columns = map(let rows = rows
function (column_item,)
push_widen_column(rows, column_item...)
end
end, zip_missing(rows.columns, row))
model_column = rows.model_column
push!(model_column, nothing)
@inbounds Rows(columns...; model_column = model_column)
end
"""
unzip(rows)
Collect into columns.
Will be most performant if each row is a tuple.
If each row is not a tuple, consider using `unzip(Iterators.map(Tuple, rows))`.
```jldoctest
julia> using Unzip
julia> using Test: @inferred
julia> stable(x) = (x, x + 0.0, x, x + 0.0, x, x + 0.0);
julia> @inferred unzip(Iterators.map(stable, 1:4))
([1, 2, 3, 4], [1.0, 2.0, 3.0, 4.0], [1, 2, 3, 4], [1.0, 2.0, 3.0, 4.0], [1, 2, 3, 4], [1.0, 2.0, 3.0, 4.0])
julia> unstable(x) =
if x == 2
(x, x + 0.0, x, x + 0.0)
else
(x, x + 0.0)
end;
julia> unzip(Iterators.map(unstable, 1:3))
([1, 2, 3], [1.0, 2.0, 3.0], Union{Missing, Int64}[missing, 2, missing], Union{Missing, Float64}[missing, 2.0, missing])
```
"""
function unzip(row_iterator)
(
collect_rows(
row_iterator,
IteratorEltype(row_iterator),
IteratorSize(row_iterator),
)::Rows
).columns
end
# add can guess typetypes to dispatch
function collect_rows(row_iterator, iterator_eltype::EltypeUnknown, iterator_size)
Item = @default_eltype(row_iterator)
collect_rows(
row_iterator,
iterator_eltype,
iterator_size,
Item,
can_guess_fieldtypes(Item),
)
end
function collect_rows(row_iterator, iterator_eltype::HasEltype, iterator_size)
Item = eltype(row_iterator)
collect_rows(
row_iterator,
iterator_eltype,
iterator_size,
Item,
can_guess_fieldtypes(Item),
)
end
# we can fall back to base if we can guess the fieldtypes
function collect_rows(row_iterator, iterator_eltype, iterator_size, _, ::Val{true})
# don't want to allocate Nothing[]
# invalid model column, but that's ok, because this dummy rows won't get used
_collect(Rows(; model_column = 1:0), row_iterator, iterator_eltype, iterator_size)
end
# otherwise, we need to make sure that the eltype never gets used
function eltype_error(Item)
throw(
ArgumentError(
"Cannot guess the fieldtypes from eltype $Item and the iterator is empty",
),
)
end
function collect_rows(row_iterator, _, iterator_size::SizeUnknown, Item, ::Val{false})
row_state = iterate(row_iterator)
if row_state === nothing
eltype_error(Item)
else
row, state = row_state
grow_to!(
(@inbounds Rows(map(vect, row)...; model_column = [nothing])),
row_iterator,
state,
)
end
end
function collect_rows(
row_iterator,
_,
iterator_size::Union{HasLength, HasShape},
Item,
::Val{false},
)
shape = _similar_shape(row_iterator, iterator_size)
row_state = iterate(row_iterator)
if row_state === nothing
eltype_error(Item)
else
row, state = row_state
collect_to_with_first!(
(@inbounds Rows(
map(
let iterator_size = iterator_size, shape = shape
function (item::Item) where {Item}
_similar_for(1:0, Item, row_iterator, iterator_size, shape)
end
end,
row,
)...;
model_column = _similar_for(
1:0,
Nothing,
row_iterator,
iterator_size,
shape,
),
)),
row,
row_iterator,
state,
)
end
end
export unzip
end