1
1
2
2
using MappedArrays: mappedarray, ReadonlyMappedArray
3
3
4
- function ensure_all_linear_indexed (vecs:: T ) where {T<: Tuple }
5
- linear_indexed = ntuple (
6
- n -> Base. IndexStyle (fieldtype (T,n)) === IndexLinear (),
7
- Base. _counttuple (T)
8
- )
9
- all (linear_indexed) || throw (ArgumentError (
10
- " $(vecs[findfirst (x-> ! x, linear_indexed)]) cannot be linearly accessed. All inputs need to implement `Base.getindex(::T, ::Int)`"
11
- ))
12
- end
13
-
14
- struct ProductArray
15
- arrays
16
- end
17
-
18
- """
19
- lazy_product(vectors...)
20
-
21
- The output is a lazy form of
22
- ```julia
23
- collect(Iterators.product(vectors...))
24
- ```
25
- i.e. it is an AbstractArray in contrast to `Iterators.product(vectors...)`. So
26
- is accessible with `getindex` and gets default Array implementations for free.
27
- In particular it can be passed to `Base.PermutedDimsArray`` for lazy permutation
28
- and `vec()` to obtain a lazy `Base.ReshapedArray`.
29
- """
30
- function lazy_product (vectors... )
31
- ensure_all_linear_indexed (vectors)
32
- indices = CartesianIndices (ntuple (n -> axes (vec (vectors[n]), 1 ), length (vectors)))
33
- return mappedarray (indices) do idx
34
- return ntuple (n -> vec (vectors[n])[idx[n]], length (vectors))
35
- end
36
- end
4
+ using ProductArrays: productArray
37
5
38
6
"""
39
7
lazy_flatten(vectors...)
@@ -49,19 +17,19 @@ and `vec()` to obtain a lazy `Base.ReshapedArray`.
49
17
"""
50
18
function lazy_flatten (vectors... )
51
19
ensure_all_linear_indexed (vectors)
52
- lengths = cumsum (length .(vectors))
20
+ lengths = cumsum (length .(vectors))
53
21
return mappedarray (1 : lengths[end ]) do idx
54
22
# this is not efficient for iteration - maybe go with LazyArrays.jl -> Vcat instead.
55
23
v_idx = searchsortedfirst (lengths, idx)
56
- return vectors[v_idx][idx - get (lengths, v_idx- 1 , 0 )]
24
+ return vectors[v_idx][idx- get (lengths, v_idx - 1 , 0 )]
57
25
end
58
26
end
59
27
60
28
61
29
function multi_out_byFeatures (positions, out_dims)
62
- return vec (PermutedDimsArray (lazy_product (positions, 1 : out_dims), (2 , 1 )))
30
+ return vec (PermutedDimsArray (productArray (positions, 1 : out_dims), (2 , 1 )))
63
31
end
64
32
function multi_out_byOutput (positions, out_dims)
65
- return vec (lazy_product (positions, 1 : out_dims))
33
+ return vec (productArray (positions, 1 : out_dims))
66
34
end
67
35
0 commit comments