@@ -58,12 +58,12 @@ parenttype(A::OffsetArray) = parenttype(typeof(A))
5858
5959Base. parent (A:: OffsetArray ) = A. parent
6060
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)
6461Base. eachindex (:: IndexCartesian , A:: OffsetArray ) = CartesianIndices (axes (A))
6562Base. eachindex (:: IndexLinear , A:: OffsetVector ) = axes (A, 1 )
6663
64+ Base. size (A:: OffsetArray ) = size (parent (A))
65+ Base. size (A:: OffsetArray , d) = size (parent (A), d)
66+
6767# Implementations of axes and indices1. Since bounds-checking is
6868# performance-critical and relies on axes, these are usually worth
6969# optimizing thoroughly.
@@ -74,8 +74,7 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1)
7474@inline _axes (inds, offsets) =
7575 (Base. Slice (inds[1 ] .+ offsets[1 ]), _axes (tail (inds), tail (offsets))... )
7676_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
7978
8079const OffsetAxis = Union{Integer, UnitRange, Base. Slice{<: UnitRange }, Base. OneTo}
8180function Base. similar (A:: OffsetArray , :: Type{T} , dims:: Dims ) where T
@@ -111,34 +110,33 @@ Base.trues(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
111110Base. falses (inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
112111 fill! (OffsetArray (BitArray {N} (undef, map (indexlength, inds)), map (indexoffset, inds)), false )
113112
114- # Don't allow bounds-checks to be removed during Julia 0.5
115113@inline function Base. getindex (A:: OffsetArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
116- checkbounds (A, I... )
114+ @boundscheck checkbounds (A, I... )
117115 @inbounds ret = parent (A)[offset (A. offsets, I)... ]
118116 ret
119117end
120118@inline function Base. getindex (A:: OffsetVector , i:: Int )
121- checkbounds (A, i)
119+ @boundscheck checkbounds (A, i)
122120 @inbounds ret = parent (A)[offset (A. offsets, (i,))[1 ]]
123121 ret
124122end
125123@inline function Base. getindex (A:: OffsetArray , i:: Int )
126- checkbounds (A, i)
124+ @boundscheck checkbounds (A, i)
127125 @inbounds ret = parent (A)[i]
128126 ret
129127end
130128@inline function Base. setindex! (A:: OffsetArray{T,N} , val, I:: Vararg{Int,N} ) where {T,N}
131- checkbounds (A, I... )
129+ @boundscheck checkbounds (A, I... )
132130 @inbounds parent (A)[offset (A. offsets, I)... ] = val
133131 val
134132end
135133@inline function Base. setindex! (A:: OffsetVector , val, i:: Int )
136- checkbounds (A, i)
134+ @boundscheck checkbounds (A, i)
137135 @inbounds parent (A)[offset (A. offsets, (i,))[1 ]] = val
138136 val
139137end
140138@inline function Base. setindex! (A:: OffsetArray , val, i:: Int )
141- checkbounds (A, i)
139+ @boundscheck checkbounds (A, i)
142140 @inbounds parent (A)[i] = val
143141 val
144142end
@@ -163,82 +161,10 @@ offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be short
163161
164162indexoffset (r:: AbstractRange ) = first (r) - 1
165163indexoffset (i:: Integer ) = 0
166- indexlength (r:: AbstractRange ) = Base . _length (r)
164+ indexlength (r:: AbstractRange ) = length (r)
167165indexlength (i:: Integer ) = i
168166
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" ))
242168
243169function Base. showarg (io:: IO , a:: OffsetArray , toplevel)
244170 print (io, " OffsetArray(" )
0 commit comments