Skip to content

Commit 9b24a39

Browse files
committed
Add HasAxes trait
The intended use of this trait is to facilitate extensions of the AxisArrays interface to other AbstractArrays.
1 parent 60d2a11 commit 9b24a39

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/core.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ AxisArray(A::AbstractArray) = AxisArray(A, ()) # Disambiguation
193193
AxisArray(A::AbstractArray, names::Symbol...) = AxisArray(A, map((name,ind)->Axis{name}(ind), names, indices(A)))
194194
AxisArray(A::AbstractArray, vects::AbstractVector...) = AxisArray(A, ntuple(i->Axis{_defaultdimname(i)}(vects[i]), length(vects)))
195195

196+
# Traits
197+
immutable HasAxes{B} end
198+
HasAxes{A<:AxisArray}(::Type{A}) = HasAxes{true}()
199+
HasAxes{A<:AbstractArray}(::Type{A}) = HasAxes{false}()
200+
HasAxes(A::AbstractArray) = HasAxes(typeof(A))
201+
196202
# Axis definitions
197203
@doc """
198204
axisdim(::AxisArray, ::Axis) -> Int

test/core.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ A = AxisArray([0]', :x, :y)
118118
@test axisnames(@inferred(squeeze(A, Axis{:y}))) == (:x,)
119119
@test axisnames(@inferred(squeeze(squeeze(A, Axis{:x}), Axis{:y}))) == ()
120120

121+
@test AxisArrays.HasAxes(A) == AxisArrays.HasAxes{true}()
122+
@test AxisArrays.HasAxes([1]) == AxisArrays.HasAxes{false}()
123+
121124
# Test axisdim
122125
@test_throws ArgumentError AxisArray(reshape(1:24, 2,3,4),
123126
Axis{1}(.1:.1:.2),

0 commit comments

Comments
 (0)