|
8 | 8 | using Base: @pure
|
9 | 9 | end
|
10 | 10 |
|
| 11 | +typealias Symbols Tuple{Symbol,Vararg{Symbol}} |
| 12 | + |
11 | 13 | @doc """
|
12 | 14 | Type-stable axis-specific indexing and identification with a
|
13 | 15 | parametric type.
|
@@ -263,6 +265,48 @@ Base.similar{T}(A::AxisArray{T}, S::Type, axs::Axis...) = similar(A, S, axs)
|
263 | 265 | AxisArray(d, $ax)
|
264 | 266 | end
|
265 | 267 | end
|
| 268 | + |
| 269 | +function Base.permutedims(A::AxisArray, perm) |
| 270 | + p = permutation(perm, axisnames(A)) |
| 271 | + AxisArray(permutedims(A.data, p), axes(A)[[p...]]) |
| 272 | +end |
| 273 | +permutation(to::Union{AbstractVector{Int},Tuple{Int,Vararg{Int}}}, from::Symbols) = to |
| 274 | + |
| 275 | +""" |
| 276 | + permutation(to, from) -> p |
| 277 | +
|
| 278 | +Calculate the permutation of labels in `from` to produce the order in |
| 279 | +`to`. Any entries in `to` that are missing in `from` will receive an |
| 280 | +index of 0. Any entries in `from` that are missing in `to` will have |
| 281 | +their indices appended to the end of the permutation. Consequently, |
| 282 | +the length of `p` is equal to the longer of `to` and `from`. |
| 283 | +""" |
| 284 | +function permutation(to::Symbols, from::Symbols) |
| 285 | + n = length(to) |
| 286 | + nf = length(from) |
| 287 | + li = linearindices(from) |
| 288 | + d = Dict(from[i]=>i for i in li) |
| 289 | + covered = similar(dims->falses(length(li)), li) |
| 290 | + ind = Array(Int, max(n, nf)) |
| 291 | + for (i,toi) in enumerate(to) |
| 292 | + j = get(d, toi, 0) |
| 293 | + ind[i] = j |
| 294 | + if j != 0 |
| 295 | + covered[j] = true |
| 296 | + end |
| 297 | + end |
| 298 | + k = n |
| 299 | + for i in li |
| 300 | + if !covered[i] |
| 301 | + d[from[i]] != i && throw(ArgumentError("$(from[i]) is a duplicated argument")) |
| 302 | + k += 1 |
| 303 | + k > nf && throw(ArgumentError("no incomplete containment allowed in $to and $from")) |
| 304 | + ind[k] = i |
| 305 | + end |
| 306 | + end |
| 307 | + ind |
| 308 | +end |
| 309 | + |
266 | 310 | # A simple display method to include axis information. It might be nice to
|
267 | 311 | # eventually display the axis labels alongside the data array, but that is
|
268 | 312 | # much more difficult.
|
|
0 commit comments