@@ -10,6 +10,10 @@ Should be equal to `collect(Iterators.flatten(iter))`, but faster.
10
10
Acting on a vector of vectors, this is equal to `vcat(iter...)` or `reduce(vcat, iter)`,
11
11
and to `vec(stack(iter))` when this is allowed, i.e. when `allequal(length.(iter))`.
12
12
13
+ !!! Warning
14
+ `flatten` is an experiment which got committed here by accident, more or less.
15
+ I'm not sure it's part of the long-term future of LazyStack.
16
+
13
17
# Examples
14
18
```
15
19
julia> v = flatten((1:2, 8:9))
96
100
97
101
# _typed_flatten(::Type{T}, ::Union{HasShape, HasLength}, A) where {T} = _flatten(collect(A))
98
102
103
+ # Non-array iterators, whose elements are arrays or tuples, should probably be collected & sent to the above path.
104
+
99
105
function _typed_flatten (:: Type{T} , :: IteratorSize , A) where {T}
100
106
xit = iterate (A)
101
107
nothing === xit && return T[]
@@ -297,4 +303,41 @@ vectors of varying eltype
297
303
min 2.713 μs, mean 9.107 μs (2 allocations, 78.17 KiB)
298
304
min 68.750 μs, mean 77.771 μs (9 allocations, 326.55 KiB)
299
305
306
+ =#
307
+
308
+
309
+ #=
310
+
311
+ julia> f(x, y) = reduce(vcat, map(current -> searchsorted(x, current), y)); # collected
312
+
313
+ julia> g(x, y) = mapreduce(current -> searchsorted(x, current), vcat, y); # pairwise
314
+
315
+ julia> h(x, y) = flatten(current -> searchsorted(x, current), y); # append!
316
+
317
+ julia> let
318
+ Random.seed!(1)
319
+ list = sort(rand(1:99, 10^6))
320
+ needed = unique(sort(rand(1:99, 10)))
321
+ @btime f($list, $needed) # reduce(vcat, map(...
322
+ # @btime g($list, $needed) # mapreduce(...
323
+ @btime h($list, $needed) # flatten(...
324
+ @btime flatten([searchsorted($list, current) for current in $needed])
325
+ end;
326
+ min 17.958 μs, mean 74.229 μs (4 allocations, 789.84 KiB)
327
+ min 19.917 μs, mean 57.096 μs (2 allocations, 807.81 KiB) # here the guess is good
328
+ min 19.542 μs, mean 72.105 μs (3 allocations, 789.70 KiB)
329
+
330
+ julia> let
331
+ Random.seed!(42)
332
+ list = sort(rand(1:99, 10^6))
333
+ needed = unique(sort(rand(1:99, 10)))
334
+ @btime f($list, $needed) # reduce(vcat, map(...
335
+ # @btime g($list, $needed) # mapreduce(...
336
+ @btime h($list, $needed) # flatten(...
337
+ @btime flatten([searchsorted($list, current) for current in $needed])
338
+ end;
339
+ min 17.791 μs, mean 67.903 μs (4 allocations, 790.41 KiB)
340
+ min 37.375 μs, mean 128.942 μs (3 allocations, 2.13 MiB) # here the guess is not so good
341
+ min 19.417 μs, mean 77.225 μs (3 allocations, 790.27 KiB)
342
+
300
343
=#
0 commit comments