-
Notifications
You must be signed in to change notification settings - Fork 15
/
group.jl
402 lines (322 loc) · 11.4 KB
/
group.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
group(iter)
group(by::Union{Function, Type}, iter)
group(by::Union{Function, Type}, f::Union{Function, Type}, iter...)
Return a dictionary of groups of elements `x` of the iterable `iter` into groups labeled by
`by(x)`, transforming each element to `f(x)`, where `by` and `f` default to the `identity`
function. If multiple collections (of the same length) are provided, the transformations
`by` and `f` occur elementwise.
# Example
```jldoctest
julia> group(iseven, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Dictionary{Bool,Array{Int64,1}} with 2 entries:
false │ [1, 3, 5, 7, 9]
true │ [2, 4, 6, 8, 10]
julia> group(iseven, x -> x ÷ 2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Dictionary{Bool,Array{Int64,1}} with 2 entries:
false │ [0, 1, 2, 3, 4]
true │ [1, 2, 3, 4, 5]
```
"""
group(iter) = group(iter, iter)
group(by::Callable, iter) = group(mapview(by, iter), iter)
group(by::Callable, f::Callable, iter) = group(mapview(by, iter), mapview(f, iter))
# TODO multi-input mapview
#group(by::Callable, f::Callable, iter1, iter2) = group(mapview(by, iter1, iter2), mapview(f, iter1, iter2))
#group(by::Callable, f::Callable, iter1, iter2, iters...) = group(mapview(by, iter1, iter2, iters...), mapview(f, iter1, iter2, iters...))
group(by::Callable, f::Callable, iter1, iter2, iters...) = group((x -> by(x...)), (x -> f(x...)), zip(iter1, iter2, iters...))
"""
group(groups, values)
Return a dictionary of the elements of `values` grouped by the label inidcated by by the
matching element from `groups`.
# Example
```julia
julia> group([true,false,true,false,true], [1,2,3,4,5])
2-element Dictionary{Bool,Array{Int64,1}}
true │ [1, 3, 5]
false │ [2, 4]
```
"""
function group(groups, values)
I = eltype(groups)
T = eltype(values)
out = Dictionary{I, Vector{T}}()
@inbounds for (group, value) in zip(groups, values)
push!(get!(Vector{T}, out, group), value)
end
return out
end
function group(groups::AbstractArray, values::AbstractArray)
I = eltype(groups) # TODO EltypeUnknown
T = eltype(values) # TODO EltypeUnknown
if keys(groups) != keys(values)
throw(DimensionMismatch("dimensions must match"))
end
out = Dictionary{I, Vector{T}}()
@inbounds for i in keys(groups)
group = groups[i]
value = values[i]
push!(get!(Vector{T}, out, group), value)
end
return out
end
function group(groups::AbstractDictionary, values::AbstractDictionary)
I = eltype(groups)
T = eltype(values)
out = Dictionary{I, Vector{T}}()
if sharetokens(groups, values)
@inbounds for token in tokens(groups)
group = gettokenvalue(groups, token)
value = gettokenvalue(values, token)
push!(get!(Vector{T}, out, group), value)
end
else
if length(groups) != length(values)
throw(KeyError("Indices must match"))
end
for (i, group) in pairs(groups)
value = values[i]
push!(get!(Vector{T}, out, group), value)
end
end
return out
end
"""
groupreduce(by, f, op, iter...; [init])
Applies a `mapreduce`-like operation on the groupings labeled by passing the elements of
`iter` through `by`. Mostly equivalent to `map(g -> reduce(op, g), group(by, f, iter))`,
but designed to be more efficient. If multiple collections (of the same length) are
provided, the transformations `by` and `f` occur elementwise.
"""
groupreduce(by::Callable, op::Callable, iter) = groupreduce(by, identity, op, iter)
groupreduce(by::Callable, f::Callable, op::Callable, iter1, iter2, iters...; kw...) = groupreduce((x -> by(x...)), (x -> f(x...)), op, zip(iter1, iter2, iters...); kw...)
function groupreduce(by::Callable, f::Callable, op::Callable, iter; kw...)
groupreduce(op, mapview(by, iter), mapview(f, iter); kw...)
end
function groupreduce(op::Callable, groups, values; kw...)
I = eltype(groups)
nt = kw.data
if nt isa NamedTuple{()}
T = eltype(values)
elseif nt isa NamedTuple{(:init,)}
T = Core.Compiler.return_type(op, Tuple{typeof(nt.init), eltype(values)})
else
throw(ArgumentError("groupreduce doesn't support these keyword arguments: $(setdiff(keys(nt), (:init,)))"))
end
out = Dictionary{I, T}()
@inbounds for (group, value) in zip(groups, values)
(hadtoken, token) = gettoken!(out, group)
if hadtoken
settokenvalue!(out, token, op(gettokenvalue(out, token), value))
else
if nt isa NamedTuple{()}
settokenvalue!(out, token, value)
else
settokenvalue!(out, token, op(nt.init, value))
end
end
end
return out
end
"""
groupsum(by, [f], iter)
Sum the elements of `iter` belonging to different groups, optionally mapping by `f`.
"""
groupsum(iter) = groupsum(identity, iter)
groupsum(by::Callable, iter) = groupsum(by, identity, iter)
groupsum(by::Callable, f::Callable, iter) = groupreduce(by, f, +, iter)
"""
groupprod(by, [f], iter)
Multiply the elements of `iter` belonging to different groups, optionally mapping by `f`.
"""
groupprod(iter) = groupprod(identity, iter)
groupprod(by::Callable, iter) = groupprod(by, identity, iter)
groupprod(by::Callable, f::Callable, iter) = groupreduce(by, f, *, iter)
"""
groupcount([by], iter)
Determine the number of elements of `iter` belonging to each group.
"""
groupcount(by::Callable, iter) = groupreduce(by, x->1, +, iter)
groupcount(iter) = groupcount(identity, iter) # A handy extension of `unique`
"""
groupfirst([by], [f], iter)
Find the first element of each grouping.
"""
groupfirst(iter) = groupreduce(identity, identity, former, iter)
groupfirst(by::Callable, iter) = groupreduce(by, identity, former, iter)
groupfirst(by::Callable, f::Callable, iter) = groupreduce(by, f, former, iter)
groupfirst(groups, values) = groupreduce(former, groups, values)
"""
grouplast([by], [f], iter)
Find the last element of each grouping.
"""
grouplast(iter) = groupreduce(identity, identity, latter, iter)
grouplast(by::Callable, iter) = groupreduce(by, identity, latter, iter)
grouplast(by::Callable, f::Callable, iter) = groupreduce(by, f, latter, iter)
grouplast(groups, values) = groupreduce(latter, groups, values)
"""
grouponly([by], [f], iter)
Return a dictionary mapping the unique elements `x` of iter grouped by `by(x)` with value
`f(x)`, where `by` and `f` default to the identity function.
This is an optimized equivalent of `only.(group(by, f, iter))` and is similar to
`Dictionary(by.(iter), f.(iter))`
"""
grouponly(iter) = grouponly(identity, iter)
grouponly(by::Callable, iter) = grouponly(by, identity, iter)
grouponly(by::Callable, f::Callable, iter) = grouponly(mapview(by, iter), mapview(f, iter))
"""
grouponly(groups, values)
This is an optimized equivalent of `only.(group(groups, values))`.
"""
grouponly(groups, values) = Dictionary(groups, values)
# should make this more generic... if `groups` is an `AbstractIndices` we can use `similar`
# to generate the output...
"""
groupunique([by], [f], iter)
Return a dictionary mapping the sets of elements `x` of iter grouped by `by(x)` with values
`f(x)`, where `by` and `f` default to the identity function.
This is similar to `unique.(group(by, f, iter))` but each subgroup is an `AbstractIndices`
of distinct values (rather than an `AbstractArray` with possibly repeated values).
# Example
```julia
julia> groupunique(iseven, [1,2,1,2,3])
2-element Dictionary{Bool,Indices{Int64}}
false │ {2}
true │ {1, 3}
```
"""
groupunique(iter) = groupunique(identity, iter)
groupunique(by::Callable, iter) = groupunique(by, identity, iter)
groupunique(by::Callable, f::Callable, iter) = groupunique(mapview(by, iter), mapview(f, iter))
"""
groupunique(groups, values)
Collect the unique elements of `values` by the corresponding element of `groups`, returning
a dictinary of sets.
# Example
```julia
julia> groupunique([true,false,true,false,true], [1,2,1,2,3])
2-element Dictionary{Bool,Indices{Int64}}
true │ {1, 3}
false │ {2}
```
"""
function groupunique(groups, values)
I = eltype(groups)
T = eltype(values)
out = Dictionary{I, Indices{T}}()
for (group, value) in zip(groups, values)
grouping = get!(Indices{T}, out, group)
set!(grouping, value)
end
return out
end
@deprecate groupinds groupfind
"""
groupfind([by], container)
Seperate the indices `i` of `container` into groups labeled by `by(container[i])`, where the
default value of `by` is `identity`.
# Example
```jldoctest
julia> groupfind(iseven, [3,4,2,6,5,8])
2-element Dictionary{Bool,Array{Int64,1}}
false │ [1, 5]
true │ [2, 3, 4, 6]
```
"""
groupfind(by::Callable, container) = groupfind(mapview(by, container))
function groupfind(container)
I = eltype(container)
T = keytype(container)
out = Dictionary{I, Vector{T}}()
@inbounds for i in keys(container)
tmp = get!(Vector{T}, out, container[i])
push!(tmp, i)
end
return out
end
function groupfind(inds::AbstractIndices)
I = eltype(inds)
T = keytype(inds)
out = Dictionary{I, Vector{T}}()
for i in inds
tmp = get!(Vector{T}, out, i)
push!(tmp, i)
end
return out
end
function groupfind(dict::AbstractDictionary)
I = eltype(dict)
T = keytype(dict)
inds = keys(dict)
out = Dictionary{I, Vector{T}}()
@inbounds if istokenizable(dict)
for t in tokens(dict)
tmp = get!(Vector{T}, out, gettokenvalue(dict, t))
push!(tmp, gettokenvalue(inds, t))
end
else
for i in inds
tmp = get!(Vector{T}, out, dict[i])
push!(tmp, i)
end
end
return out
end
# Semi-lazy grouping container (TODO docstring)
struct GroupDictionary{I, T, Parent, Inds <: AbstractDictionary{I}} <: AbstractDictionary{I, T}
parent::Parent
inds::Inds
end
Base.parent(g::GroupDictionary) = getfield(g, :parent)
_inds(g::GroupDictionary) = getfield(g, :inds)
Base.keys(g::GroupDictionary) = keys(_inds(g))
Base.isassigned(g::GroupDictionary{I}, k::I) where {I} = isassigned(_inds(g), k)
Base.@propagate_inbounds function Base.getindex(g::GroupDictionary{I}, i::I) where {I}
inds = _inds(g)[i]
return view(parent(g), inds)
end
Dictionaries.tokentype(g::GroupDictionary) = tokentype(_inds(g))
@propagate_inbounds Dictionaries.gettoken(g::GroupDictionary{I}, i::I) where {I} = gettoken(_inds(g), i)
Dictionaries.istokenassigned(g::GroupDictionary, t) = istokenassigned(_inds(g), t)
@propagate_inbounds function Dictionaries.gettokenvalue(g::GroupDictionary, t)
inds = gettokenvalue(_inds(g), t)
return view(parent(g), inds)
end
"""
groupview([by], container)
Like `group`, but each grouping is a `view` of the indexable input `container`.
# Example
```jldoctest
julia> v = [3,4,2,6,5,8]
6-element Array{Int64,1}:
3
4
2
6
5
8
julia> groups = groupview(iseven, v)
2-element GroupDictionary{Bool,SubArray{Int64,1,Array{Int64,1},Tuple{Array{Int64,1}},false},Array{Int64,1},Dictionary{Bool,Array{Int64,1}}}
false │ [3, 5]
true │ [4, 2, 6, 8]
julia> groups[false] .= 99 # set all the odd values to 99
2-element view(::Array{Int64,1}, [1, 5]) with eltype Int64:
99
99
julia> v
6-element Array{Int64,1}:
99
4
2
6
99
8
```
"""
groupview(container) = groupview(identity, container)
function groupview(by::Callable, container)
inds = groupfind(by, container)
T = typeof(container)
V = Core.Compiler.return_type(view, Tuple{T, eltype(inds)})
return GroupDictionary{keytype(inds), V, T, typeof(inds)}(container, inds)
end