|
1 | 1 | # Mostly copied from https://github.com/JuliaLang/julia/blob/master/base/permuteddimsarray.jl
|
2 | 2 | # Like `PermutedDimsArrays` but singly nested, similar to `Adjoint` and `Transpose`
|
3 | 3 | # (though those are fully recursive).
|
| 4 | +#= |
| 5 | +TODO: Investigate replacing this with a `PermutedDimsArray` wrapped around a `MappedArrays.MappedArray`. |
| 6 | +There are a few issues with that: |
| 7 | +1. Just using a type alias leads to type piracy, for example the constructor is type piracy. |
| 8 | +2. `setindex!(::NestedPermutedDimsArray, I...)` fails because no conversion is defined between `Array` |
| 9 | +and `PermutedDimsArray`. |
| 10 | +3. The type alias is tricky to define, ideally it would have similar type parameters to the current |
| 11 | +`NestedPermutedDimsArrays.NestedPermutedDimsArray` definition which matches the type parameters |
| 12 | +of `PermutedDimsArrays.PermutedDimsArray` but that seems to be difficult to achieve. |
| 13 | +```julia |
| 14 | +module NestedPermutedDimsArrays |
| 15 | +
|
| 16 | +using MappedArrays: MultiMappedArray, mappedarray |
| 17 | +export NestedPermutedDimsArray |
| 18 | +
|
| 19 | +const NestedPermutedDimsArray{TT,T<:AbstractArray{TT},N,perm,iperm,AA<:AbstractArray{T}} = PermutedDimsArray{ |
| 20 | + PermutedDimsArray{TT,N,perm,iperm,T}, |
| 21 | + N, |
| 22 | + perm, |
| 23 | + iperm, |
| 24 | + MultiMappedArray{ |
| 25 | + PermutedDimsArray{TT,N,perm,iperm,T}, |
| 26 | + N, |
| 27 | + Tuple{AA}, |
| 28 | + Type{PermutedDimsArray{TT,N,perm,iperm,T}}, |
| 29 | + Type{PermutedDimsArray{TT,N,iperm,perm,T}}, |
| 30 | + }, |
| 31 | +} |
| 32 | +
|
| 33 | +function NestedPermutedDimsArray(a::AbstractArray, perm) |
| 34 | + iperm = invperm(perm) |
| 35 | + f = PermutedDimsArray{eltype(eltype(a)),ndims(a),perm,iperm,eltype(a)} |
| 36 | + finv = PermutedDimsArray{eltype(eltype(a)),ndims(a),iperm,perm,eltype(a)} |
| 37 | + return PermutedDimsArray(mappedarray(f, finv, a), perm) |
| 38 | +end |
| 39 | +
|
| 40 | +end |
| 41 | +``` |
| 42 | +=# |
4 | 43 | module NestedPermutedDimsArrays
|
5 | 44 |
|
6 | 45 | import Base: permutedims, permutedims!
|
|
107 | 146 | A::NestedPermutedDimsArray{T,N,perm,iperm}, val, I::Vararg{Int,N}
|
108 | 147 | ) where {T,N,perm,iperm}
|
109 | 148 | @boundscheck checkbounds(A, I...)
|
110 |
| - @inbounds setindex!(A.parent, PermutedDimsArray(val, perm), genperm(I, iperm)...) |
| 149 | + @inbounds setindex!(A.parent, PermutedDimsArray(val, iperm), genperm(I, iperm)...) |
111 | 150 | return val
|
112 | 151 | end
|
113 | 152 |
|
|
0 commit comments