Skip to content

Commit 4a4ef48

Browse files
authored
Merge pull request #43 from JuliaArrays/teh/getaxis
Extract a specific axis via ax = A[Axis{:t}]
2 parents 05ca4ae + ae081a2 commit 4a4ef48

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/indexing.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,9 @@ end
202202
meta = Expr(:meta, :inline)
203203
return :($meta; $ex)
204204
end
205+
206+
## Extracting the full axis (name + values) from the Axis{:name} type
207+
@inline Base.getindex{Ax<:Axis}(A::AxisArray, ::Type{Ax}) = getaxis(Ax, axes(A)...)
208+
@inline getaxis{Ax<:Axis}(::Type{Ax}, ax::Ax, axs...) = ax
209+
@inline getaxis{Ax<:Axis}(::Type{Ax}, ax::Axis, axs...) = getaxis(Ax, axs...)
210+
@noinline getaxis{Ax<:Axis}(::Type{Ax}) = throw(ArgumentError("no axis of type $Ax was found"))

test/indexing.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,12 @@ A = AxisArray(reshape(1:15, 3, 5), :x, :y)
9595
@test A[2,2,CartesianIndex(())] == 5
9696
@test A[2,CartesianIndex(()),2] == 5
9797
@test A[CartesianIndex(()),2,2] == 5
98+
99+
# Extracting the full axis
100+
axx = @inferred(A[Axis{:x}])
101+
@test isa(axx, Axis{:x})
102+
@test axx.val == 1:3
103+
axy = @inferred(A[Axis{:y}])
104+
@test isa(axy, Axis{:y})
105+
@test axy.val == 1:5
106+
@test_throws ArgumentError A[Axis{:z}]

0 commit comments

Comments
 (0)