@@ -114,13 +114,22 @@ end
114
114
@propagate_inbounds Base. setindex! (A:: AxisArray , v, idx:: AbstractArray{Bool} ) = (A. data[idx] = v)
115
115
116
116
# ## Fancier indexing capabilities provided only by AxisArrays ###
117
- @propagate_inbounds Base. getindex (A:: AxisArray , idxs... ) = A[to_index (A,idxs... )... ]
118
- @propagate_inbounds Base. setindex! (A:: AxisArray , v, idxs... ) = (A[to_index (A,idxs... )... ] = v)
117
+ # To avoid StackOverflowErrors on indexes that we don't know how to convert, we
118
+ # give AxisArrays once chance to convert into known format and then defer to the parent
119
+ @propagate_inbounds Base. getindex (A:: AxisArray , idxs... ) = getindex_converted (A, to_index (A,idxs... )... )
120
+ @propagate_inbounds Base. setindex! (A:: AxisArray , v, idxs... ) = setindex!_converted (A, v, to_index (A,idxs... )... )
119
121
# Deal with lots of ambiguities here
120
122
@propagate_inbounds Base. view (A:: AxisArray , idxs:: ViewIndex... ) = view (A, to_index (A,idxs... )... )
121
123
@propagate_inbounds Base. view (A:: AxisArray , idxs:: Union{ViewIndex,AbstractCartesianIndex} ...) = view (A, to_index (A,Base. IteratorsMD. flatten (idxs)... )... )
122
124
@propagate_inbounds Base. view (A:: AxisArray , idxs... ) = view (A, to_index (A,idxs... )... )
123
125
126
+ @propagate_inbounds getindex_converted (A, idxs:: Idx... ) = A[idxs... ]
127
+ @propagate_inbounds setindex!_converted (A, v, idxs:: Idx... ) = (A[idxs... ] = v)
128
+
129
+ @propagate_inbounds getindex_converted (A, idxs... ) = A. data[idxs... ]
130
+ @propagate_inbounds setindex!_converted (A, v, idxs... ) = (A. data[idxs... ] = v)
131
+
132
+
124
133
# First is indexing by named axis. We simply sort the axes and re-dispatch.
125
134
# When indexing by named axis the shapes of omitted dimensions are preserved
126
135
# TODO : should we handle multidimensional Axis indexes? It could be interpreted
@@ -179,7 +188,7 @@ axisindexes(t, ax, idx) = error("cannot index $(typeof(ax)) with $(typeof(idx));
179
188
# Maybe extend error message to all <: Numbers if Base allows it?
180
189
axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx:: Real ) =
181
190
throw (ArgumentError (" invalid index: $idx . Use `atvalue` when indexing by value." ))
182
- function axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx)
191
+ function axisindexes (:: Type{Dimensional} , ax:: AbstractVector{T} , idx:: T ) where T
183
192
idxs = searchsorted (ax, ClosedInterval (idx,idx))
184
193
length (idxs) > 1 && error (" more than one datapoint lies on axis value $idx ; use an interval to return all values" )
185
194
if length (idxs) == 1
@@ -218,6 +227,8 @@ function axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::ExactValue)
218
227
throw (BoundsError (ax, idx))
219
228
end
220
229
end
230
+ # For index types that AxisArrays doesn't know about
231
+ axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx) = idx
221
232
222
233
# Dimensional axes may be indexed by intervals to select a range
223
234
axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx:: ClosedInterval ) = searchsorted (ax, idx)
0 commit comments