@@ -2,9 +2,9 @@ __precompile__()
2
2
3
3
module OffsetArrays
4
4
5
- using Base: Indices, tail
5
+ using Base: Indices, tail, @propagate_inbounds
6
6
7
- export OffsetArray, OffsetVector, @unsafe
7
+ export OffsetArray, OffsetVector
8
8
9
9
struct OffsetArray{T,N,AA<: AbstractArray } <: AbstractArray{T,N}
10
10
parent:: AA
@@ -58,12 +58,12 @@ parenttype(A::OffsetArray) = parenttype(typeof(A))
58
58
59
59
Base. parent (A:: OffsetArray ) = A. parent
60
60
61
- errmsg (A) = error (" size not supported for arrays with axes $(axes (A)) ; see http://docs.julialang.org/en/latest/devdocs/offset-arrays/" )
62
- Base. size (A:: OffsetArray ) = errmsg (A)
63
- Base. size (A:: OffsetArray , d) = errmsg (A)
64
61
Base. eachindex (:: IndexCartesian , A:: OffsetArray ) = CartesianIndices (axes (A))
65
62
Base. eachindex (:: IndexLinear , A:: OffsetVector ) = axes (A, 1 )
66
63
64
+ Base. size (A:: OffsetArray ) = size (parent (A))
65
+ Base. size (A:: OffsetArray , d) = size (parent (A), d)
66
+
67
67
# Implementations of axes and indices1. Since bounds-checking is
68
68
# performance-critical and relies on axes, these are usually worth
69
69
# optimizing thoroughly.
@@ -74,8 +74,7 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1)
74
74
@inline _axes (inds, offsets) =
75
75
(Base. Slice (inds[1 ] .+ offsets[1 ]), _axes (tail (inds), tail (offsets))... )
76
76
_axes (:: Tuple{} , :: Tuple{} ) = ()
77
- Base. indices1 (A:: OffsetArray{T,0} ) where {T} = 1 : 1 # we only need to specialize this one
78
-
77
+ Base. axes1 (A:: OffsetArray{T,0} ) where {T} = 1 : 1 # we only need to specialize this one
79
78
80
79
const OffsetAxis = Union{Integer, UnitRange, Base. Slice{<: UnitRange }, Base. OneTo}
81
80
function Base. similar (A:: OffsetArray , :: Type{T} , dims:: Dims ) where T
@@ -111,34 +110,33 @@ Base.trues(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
111
110
Base. falses (inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
112
111
fill! (OffsetArray (BitArray {N} (undef, map (indexlength, inds)), map (indexoffset, inds)), false )
113
112
114
- # Don't allow bounds-checks to be removed during Julia 0.5
115
- @inline function Base. getindex (A:: OffsetArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
116
- checkbounds (A, I... )
113
+ @inline @propagate_inbounds function Base. getindex (A:: OffsetArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
114
+ @boundscheck checkbounds (A, I... )
117
115
@inbounds ret = parent (A)[offset (A. offsets, I)... ]
118
116
ret
119
117
end
120
- @inline function Base. getindex (A:: OffsetVector , i:: Int )
121
- checkbounds (A, i)
118
+ @inline @propagate_inbounds function Base. getindex (A:: OffsetVector , i:: Int )
119
+ @boundscheck checkbounds (A, i)
122
120
@inbounds ret = parent (A)[offset (A. offsets, (i,))[1 ]]
123
121
ret
124
122
end
125
- @inline function Base. getindex (A:: OffsetArray , i:: Int )
126
- checkbounds (A, i)
123
+ @inline @propagate_inbounds function Base. getindex (A:: OffsetArray , i:: Int )
124
+ @boundscheck checkbounds (A, i)
127
125
@inbounds ret = parent (A)[i]
128
126
ret
129
127
end
130
- @inline function Base. setindex! (A:: OffsetArray{T,N} , val, I:: Vararg{Int,N} ) where {T,N}
131
- checkbounds (A, I... )
128
+ @inline @propagate_inbounds function Base. setindex! (A:: OffsetArray{T,N} , val, I:: Vararg{Int,N} ) where {T,N}
129
+ @boundscheck checkbounds (A, I... )
132
130
@inbounds parent (A)[offset (A. offsets, I)... ] = val
133
131
val
134
132
end
135
- @inline function Base. setindex! (A:: OffsetVector , val, i:: Int )
136
- checkbounds (A, i)
133
+ @inline @propagate_inbounds function Base. setindex! (A:: OffsetVector , val, i:: Int )
134
+ @boundscheck checkbounds (A, i)
137
135
@inbounds parent (A)[offset (A. offsets, (i,))[1 ]] = val
138
136
val
139
137
end
140
- @inline function Base. setindex! (A:: OffsetArray , val, i:: Int )
141
- checkbounds (A, i)
138
+ @inline @propagate_inbounds function Base. setindex! (A:: OffsetArray , val, i:: Int )
139
+ @boundscheck checkbounds (A, i)
142
140
@inbounds parent (A)[i] = val
143
141
val
144
142
end
@@ -163,82 +161,10 @@ offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be short
163
161
164
162
indexoffset (r:: AbstractRange ) = first (r) - 1
165
163
indexoffset (i:: Integer ) = 0
166
- indexlength (r:: AbstractRange ) = Base . _length (r)
164
+ indexlength (r:: AbstractRange ) = length (r)
167
165
indexlength (i:: Integer ) = i
168
166
169
- macro unsafe (ex)
170
- esc (unsafe (ex))
171
- end
172
- unsafe (ex) = ex
173
- function unsafe (ex:: Expr )
174
- if ex. head ∈ (:+= , :-= , :*= , :/= )
175
- ex = Expr (:(= ), ex. args[1 ], Expr (:call , Symbol (string (ex. head)[1 ]), ex. args... ))
176
- end
177
- if ex. head == :(= )
178
- a = ex. args[1 ]
179
- if isa (a, Expr) && (a:: Expr ). head == :ref
180
- # setindex!
181
- newargs = map (unsafe, ex. args[2 : end ])
182
- @assert length (newargs) == 1
183
- return Expr (:call , :(OffsetArrays. unsafe_setindex!), (a:: Expr ). args[1 ], newargs[1 ], (a:: Expr ). args[2 : end ]. .. )
184
- end
185
- end
186
- newargs = map (unsafe, ex. args)
187
- if ex. head == :ref
188
- # getindex
189
- return Expr (:call , :(OffsetArrays. unsafe_getindex), newargs... )
190
- end
191
- Expr (ex. head, newargs... )
192
- end
193
-
194
- @inline unsafe_getindex (a:: AbstractArray , I... ) = (@inbounds ret = a[I... ]; ret)
195
- @inline unsafe_setindex! (a:: AbstractArray , val, I... ) = (@inbounds a[I... ] = val; val)
196
-
197
- # Linear indexing
198
- @inline unsafe_getindex (a:: OffsetArray , i:: Int ) = _unsafe_getindex (IndexStyle (a), a, i)
199
- @inline unsafe_setindex! (a:: OffsetArray , val, i:: Int ) = _unsafe_setindex! (IndexStyle (a), a, val, i)
200
- for T in (IndexLinear, IndexCartesian) # ambiguity-resolution requires specificity for both
201
- @eval begin
202
- @inline function _unsafe_getindex (:: $T , a:: OffsetVector , i:: Int )
203
- @inbounds ret = parent (a)[offset (a. offsets, (i,))[1 ]]
204
- ret
205
- end
206
- @inline function _unsafe_setindex! (:: $T , a:: OffsetVector , val, i:: Int )
207
- @inbounds parent (a)[offset (a. offsets, (i,))[1 ]] = val
208
- val
209
- end
210
- end
211
- end
212
- @inline function _unsafe_getindex (:: IndexLinear , a:: OffsetArray , i:: Int )
213
- @inbounds ret = parent (a)[i]
214
- ret
215
- end
216
- @inline _unsafe_getindex (:: IndexCartesian , a:: OffsetArray , i:: Int ) =
217
- unsafe_getindex (a, CartesianIndices (axes (a))[i])
218
- @inline function _unsafe_setindex! (:: IndexLinear , a:: OffsetArray , val, i:: Int )
219
- @inbounds parent (a)[i] = val
220
- val
221
- end
222
- @inline _unsafe_setindex! (:: IndexCartesian , a:: OffsetArray , val, i:: Int ) =
223
- unsafe_setindex! (a, val, CartesianIndices (axes (a))[i]. .. )
224
-
225
- @inline unsafe_getindex (a:: OffsetArray , I:: Int... ) = unsafe_getindex (parent (a), offset (a. offsets, I)... )
226
- @inline unsafe_setindex! (a:: OffsetArray , val, I:: Int... ) = unsafe_setindex! (parent (a), val, offset (a. offsets, I)... )
227
- @inline unsafe_getindex (a:: OffsetArray , I... ) = unsafe_getindex (a, Base. IteratorsMD. flatten (I)... )
228
- @inline unsafe_setindex! (a:: OffsetArray , val, I... ) = unsafe_setindex! (a, val, Base. IteratorsMD. flatten (I)... )
229
-
230
- # Indexing a SubArray which has OffsetArray axes
231
- OffsetSubArray{T,N,P,I<: Tuple{OffsetArray,Vararg{OffsetArray}} } = SubArray{T,N,P,I,false }
232
- @inline function unsafe_getindex (a:: OffsetSubArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
233
- J = map (unsafe_getindex, a. indexes, I)
234
- unsafe_getindex (parent (a), J... )
235
- end
236
- @inline function unsafe_setindex! (a:: OffsetSubArray{T,N} , val, I:: Vararg{Int,N} ) where {T,N}
237
- J = map (unsafe_getindex, a. indexes, I)
238
- unsafe_setindex! (parent (a), val, J... )
239
- end
240
- @inline unsafe_getindex (a:: OffsetSubArray , I:: Union{Integer,CartesianIndex} ...) = unsafe_getindex (a, Base. IteratorsMD. flatten (I)... )
241
- @inline unsafe_setindex! (a:: OffsetSubArray , val, I:: Union{Integer,CartesianIndex} ...) = unsafe_setindex! (a, val, Base. IteratorsMD. flatten (I)... )
167
+ @eval @deprecate $ (Symbol (" @unsafe" )) $ (Symbol (" @inbounds" ))
242
168
243
169
function Base. showarg (io:: IO , a:: OffsetArray , toplevel)
244
170
print (io, " OffsetArray(" )
0 commit comments