We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents a5556f1 + 8fe78ec commit 2f81a7aCopy full SHA for 2f81a7a
src/core.jl
@@ -274,13 +274,10 @@ Given an AxisArray and an Axis, return the integer dimension of
274
the Axis within the array.
275
"""
276
axisdim(A::AxisArray, ax::Axis) = axisdim(A, typeof(ax))
277
-@generated function axisdim(A::AxisArray, ax::Type{Ax}) where Ax<:Axis
278
- dim = axisdim(A, Ax)
279
- :($dim)
280
-end
+axisdim(A::AxisArray, ax::Type{Ax}) where Ax<:Axis = axisdim(typeof(A), Ax)
281
# The actual computation is done in the type domain, which is a little tricky
282
# due to type invariance.
283
-function axisdim(::Type{AxisArray{T,N,D,Ax}}, ::Type{<:Axis{name,S} where S}) where {T,N,D,Ax,name}
+@generated function axisdim(::Type{AxisArray{T,N,D,Ax}}, ::Type{<:Axis{name,S} where S}) where {T,N,D,Ax,name}
284
isa(name, Int) && return name <= N ? name : error("axis $name greater than array dimensionality $N")
285
names = axisnames(Ax)
286
idx = findfirst(isequal(name), names)
src/indexing.jl
@@ -303,15 +303,28 @@ function axisindexes(::Type{Categorical}, ax::AbstractVector, idx::AbstractVecto
303
res
304
end
305
306
+# Creates *instances* of axis traits for a set of axes.
307
+# TODO: Transition axistrait() to return trait instances in line with common
308
+# practice in Base and other packages.
309
+#
310
+# This function is a utility tool to ensure that `axistrait` is only called
311
+# from outside the generated function below. (If not, we can get world age
312
+# errors.)
313
+_axistraits(ax1, rest...) = (axistrait(ax1)(), _axistraits(rest...)...)
314
+_axistraits() = ()
315
+
316
# This catch-all method attempts to convert any axis-specific non-standard
317
# indexing types to their integer or integer range equivalents using axisindexes
318
# It is separate from the `Base.getindex` function to allow reuse between
319
# set- and get- index.
-@generated function to_index(A::AxisArray{T,N,D,Ax}, I...) where {T,N,D,Ax}
320
+to_index(A::AxisArray, I...) = _to_index(A, _axistraits(I...), I...)
321
322
+@generated function _to_index(A::AxisArray{T,N,D,Ax}, axtraits, I...) where {T,N,D,Ax}
323
ex = Expr(:tuple)
324
n = 0
325
+ axtrait_types = axtraits.parameters
326
for i=1:length(I)
- if axistrait(I[i]) <: Categorical && i <= length(Ax.parameters)
327
+ if axtrait_types[i] <: Categorical && i <= length(Ax.parameters)
328
if I[i] <: Axis
329
push!(ex.args, :(axisindexes(A.axes[$i], I[$i].val)))
330
else
test/indexing.jl
@@ -70,11 +70,11 @@ B = AxisArray(reshape(1:15, 5,3), .1:.1:0.5, [:a, :b, :c])
70
@test @view(B[ClosedInterval(0.2, 0.6), :]) == @view(B[ClosedInterval(0.2, 0.6)]) == B[2:end,:]
71
72
# Test Categorical indexing
73
-@test B[:, :a] == @view(B[:, :a]) == B[:,1]
74
-@test B[:, :c] == @view(B[:, :c]) == B[:,3]
75
-@test B[:, [:a]] == @view(B[:, [:a]]) == B[:,[1]]
76
-@test B[:, [:c]] == @view(B[:, [:c]]) == B[:,[3]]
77
-@test B[:, [:a,:c]] == @view(B[:, [:a,:c]]) == B[:,[1,3]]
+@test @inferred(B[:, :a]) == @view(B[:, :a]) == B[:,1]
+@test @inferred(B[:, :c]) == @view(B[:, :c]) == B[:,3]
+@test @inferred(B[:, [:a]]) == @view(B[:, [:a]]) == B[:,[1]]
+@test @inferred(B[:, [:c]]) == @view(B[:, [:c]]) == B[:,[3]]
+@test @inferred(B[:, [:a,:c]]) == @view(B[:, [:a,:c]]) == B[:,[1,3]]
78
79
@test B[Axis{:row}(ClosedInterval(0.15, 0.3))] == @view(B[Axis{:row}(ClosedInterval(0.15, 0.3))]) == B[2:3,:]
80
0 commit comments