|
1 | 1 | const Idx = Union{Real,Colon,AbstractArray{Int}}
|
2 | 2 |
|
3 |
| -using Base: ViewIndex |
| 3 | +using Base: ViewIndex, @propagate_inbounds |
4 | 4 |
|
5 | 5 | # Defer IndexStyle to the wrapped array
|
6 | 6 | @compat Base.IndexStyle{T,N,D,Ax}(::Type{AxisArray{T,N,D,Ax}}) = IndexStyle(D)
|
7 | 7 |
|
8 | 8 | # Simple scalar indexing where we just set or return scalars
|
9 |
| -@inline Base.getindex(A::AxisArray, idxs::Int...) = A.data[idxs...] |
10 |
| -@inline Base.setindex!(A::AxisArray, v, idxs::Int...) = (A.data[idxs...] = v) |
| 9 | +@propagate_inbounds Base.getindex(A::AxisArray, idxs::Int...) = A.data[idxs...] |
| 10 | +@propagate_inbounds Base.setindex!(A::AxisArray, v, idxs::Int...) = (A.data[idxs...] = v) |
11 | 11 |
|
12 | 12 | # Cartesian iteration
|
13 | 13 | Base.eachindex(A::AxisArray) = eachindex(A.data)
|
14 |
| -Base.getindex(A::AxisArray, idx::Base.IteratorsMD.CartesianIndex) = A.data[idx] |
15 |
| -Base.setindex!(A::AxisArray, v, idx::Base.IteratorsMD.CartesianIndex) = (A.data[idx] = v) |
16 | 14 |
|
17 | 15 | @generated function reaxis(A::AxisArray, I::Idx...)
|
18 | 16 | N = length(I)
|
@@ -56,48 +54,48 @@ Base.setindex!(A::AxisArray, v, idx::Base.IteratorsMD.CartesianIndex) = (A.data[
|
56 | 54 | end
|
57 | 55 | end
|
58 | 56 |
|
59 |
| -@inline function Base.getindex(A::AxisArray, idxs::Idx...) |
| 57 | +@propagate_inbounds function Base.getindex(A::AxisArray, idxs::Idx...) |
60 | 58 | AxisArray(A.data[idxs...], reaxis(A, idxs...))
|
61 | 59 | end
|
62 | 60 |
|
63 | 61 | # To resolve ambiguities, we need several definitions
|
64 | 62 | if VERSION >= v"0.6.0-dev.672"
|
65 | 63 | using Base.AbstractCartesianIndex
|
66 |
| - Base.view(A::AxisArray, idxs::Idx...) = AxisArray(view(A.data, idxs...), reaxis(A, idxs...)) |
| 64 | + @propagate_inbounds Base.view(A::AxisArray, idxs::Idx...) = AxisArray(view(A.data, idxs...), reaxis(A, idxs...)) |
67 | 65 | else
|
68 |
| - @inline function Base.view{T,N}(A::AxisArray{T,N}, idxs::Vararg{Idx,N}) |
| 66 | + @propagate_inbounds function Base.view{T,N}(A::AxisArray{T,N}, idxs::Vararg{Idx,N}) |
69 | 67 | AxisArray(view(A.data, idxs...), reaxis(A, idxs...))
|
70 | 68 | end
|
71 |
| - function Base.view(A::AxisArray, idx::Idx) |
| 69 | + @propagate_inbounds function Base.view(A::AxisArray, idx::Idx) |
72 | 70 | AxisArray(view(A.data, idx), reaxis(A, idx))
|
73 | 71 | end
|
74 |
| - @inline function Base.view{N}(A::AxisArray, idxs::Vararg{Idx,N}) |
| 72 | + @propagate_inbounds function Base.view{N}(A::AxisArray, idxs::Vararg{Idx,N}) |
75 | 73 | # this should eventually be deleted, see julia #14770
|
76 | 74 | AxisArray(view(A.data, idxs...), reaxis(A, idxs...))
|
77 | 75 | end
|
78 | 76 | end
|
79 | 77 |
|
80 | 78 | # Setindex is so much simpler. Just assign it to the data:
|
81 |
| -@inline Base.setindex!(A::AxisArray, v, idxs::Idx...) = (A.data[idxs...] = v) |
| 79 | +@propagate_inbounds Base.setindex!(A::AxisArray, v, idxs::Idx...) = (A.data[idxs...] = v) |
82 | 80 |
|
83 | 81 | ### Fancier indexing capabilities provided only by AxisArrays ###
|
84 |
| -@inline Base.getindex(A::AxisArray, idxs...) = A[to_index(A,idxs...)...] |
85 |
| -@inline Base.setindex!(A::AxisArray, v, idxs...) = (A[to_index(A,idxs...)...] = v) |
| 82 | +@propagate_inbounds Base.getindex(A::AxisArray, idxs...) = A[to_index(A,idxs...)...] |
| 83 | +@propagate_inbounds Base.setindex!(A::AxisArray, v, idxs...) = (A[to_index(A,idxs...)...] = v) |
86 | 84 | # Deal with lots of ambiguities here
|
87 | 85 | if VERSION >= v"0.6.0-dev.672"
|
88 |
| - Base.view(A::AxisArray, idxs::ViewIndex...) = view(A, to_index(A,idxs...)...) |
89 |
| - Base.view(A::AxisArray, idxs::Union{ViewIndex,AbstractCartesianIndex}...) = view(A, to_index(A,Base.IteratorsMD.flatten(idxs)...)...) |
90 |
| - Base.view(A::AxisArray, idxs...) = view(A, to_index(A,idxs...)...) |
| 86 | + @propagate_inbounds Base.view(A::AxisArray, idxs::ViewIndex...) = view(A, to_index(A,idxs...)...) |
| 87 | + @propagate_inbounds Base.view(A::AxisArray, idxs::Union{ViewIndex,AbstractCartesianIndex}...) = view(A, to_index(A,Base.IteratorsMD.flatten(idxs)...)...) |
| 88 | + @propagate_inbounds Base.view(A::AxisArray, idxs...) = view(A, to_index(A,idxs...)...) |
91 | 89 | else
|
92 | 90 | for T in (:ViewIndex, :Any)
|
93 | 91 | @eval begin
|
94 |
| - @inline function Base.view{T,N}(A::AxisArray{T,N}, idxs::Vararg{$T,N}) |
| 92 | + @propagate_inbounds function Base.view{T,N}(A::AxisArray{T,N}, idxs::Vararg{$T,N}) |
95 | 93 | view(A, to_index(A,idxs...)...)
|
96 | 94 | end
|
97 |
| - function Base.view(A::AxisArray, idx::$T) |
| 95 | + @propagate_inbounds function Base.view(A::AxisArray, idx::$T) |
98 | 96 | view(A, to_index(A,idx)...)
|
99 | 97 | end
|
100 |
| - @inline function Base.view{N}(A::AxisArray, idsx::Vararg{$T,N}) |
| 98 | + @propagate_inbounds function Base.view{N}(A::AxisArray, idsx::Vararg{$T,N}) |
101 | 99 | # this should eventually be deleted, see julia #14770
|
102 | 100 | view(A, to_index(A,idxs...)...)
|
103 | 101 | end
|
|
229 | 227 | @inline getaxis{Ax<:Axis}(::Type{Ax}, ax::Ax, axs...) = ax
|
230 | 228 | @inline getaxis{Ax<:Axis}(::Type{Ax}, ax::Axis, axs...) = getaxis(Ax, axs...)
|
231 | 229 | @noinline getaxis{Ax<:Axis}(::Type{Ax}) = throw(ArgumentError("no axis of type $Ax was found"))
|
| 230 | + |
| 231 | +# Boundschecking specialization: defer to the data array. |
| 232 | +# Note that we could unwrap AxisArrays when they are used as indices into other |
| 233 | +# arrays within Base's to_index/to_indices methods, but that requires a bigger |
| 234 | +# refactor to merge our to_index method with Base's. |
| 235 | +@inline Base.checkindex(::Type{Bool}, inds::AbstractUnitRange, A::AxisArray) = Base.checkindex(Bool, inds, A.data) |
0 commit comments