@@ -3,22 +3,9 @@ __precompile__()
33module OffsetArrays
44
55using Base: Indices, tail
6- using Compat
7- using Compat: axes, CartesianIndices
86
97export OffsetArray, OffsetVector, @unsafe
108
11- # # Major change in 0.7: OffsetArray now uses Offset axes
12- const AxisType = VERSION < v " 0.7.0-DEV.5242" ? identity : Base. Slice
13-
14- # TODO : just use .+
15- # See https://github.com/JuliaLang/julia/pull/22932#issuecomment-330711997
16- if VERSION < v " 0.7.0-DEV.1759"
17- plus (r:: AbstractUnitRange , i:: Integer ) = r + i
18- else
19- plus (r:: AbstractUnitRange , i:: Integer ) = broadcast (+ , r, i)
20- end
21-
229struct OffsetArray{T,N,AA<: AbstractArray } <: AbstractArray{T,N}
2310 parent:: AA
2411 offsets:: NTuple{N,Int}
@@ -31,7 +18,7 @@ OffsetArray(A::AbstractArray{T,N}, offsets::Vararg{Int,N}) where {T,N} =
3118 OffsetArray (A, offsets)
3219
3320OffsetArray {T,N} (:: UndefInitializer , inds:: Indices{N} ) where {T,N} =
34- OffsetArray {T,N,Array{T,N}} (Array {T,N} (undef, map (length , inds)), map (indexoffset, inds))
21+ OffsetArray {T,N,Array{T,N}} (Array {T,N} (undef, map (indexlength , inds)), map (indexoffset, inds))
3522OffsetArray {T} (:: UndefInitializer , inds:: Indices{N} ) where {T,N} = OffsetArray {T,N} (undef, inds)
3623OffsetArray {T,N} (:: UndefInitializer , inds:: Vararg{AbstractUnitRange,N} ) where {T,N} = OffsetArray {T,N} (undef, inds)
3724OffsetArray {T} (:: UndefInitializer , inds:: Vararg{AbstractUnitRange,N} ) where {T,N} = OffsetArray {T,N} (undef, inds)
@@ -44,33 +31,13 @@ OffsetVector{T}(::UndefInitializer, inds::AbstractUnitRange) where {T} = OffsetA
4431# deprecated constructors
4532using Base: @deprecate
4633
47- # https://github.com/JuliaLang/julia/pull/19989
48- @static if isdefined (Base, :UndefInitializer )
49- @deprecate OffsetArray (:: Type{T} , inds:: Vararg{UnitRange{Int},N} ) where {T,N} OffsetArray {T} (undef, inds)
50- @deprecate OffsetVector (:: Type{T} , inds:: AbstractUnitRange ) where {T} OffsetVector {T} (undef, inds)
51- else
52- OffsetArray (:: Type{T} , inds:: Vararg{UnitRange{Int},N} ) where {T,N} = OffsetArray {T} (inds)
53- OffsetVector (:: Type{T} , inds:: AbstractUnitRange ) where {T} = OffsetVector {T} (inds)
54- end
55-
56- # https://github.com/JuliaLang/julia/pull/24652
57- # Only activate deprecation if `undef` is available from Base;
58- # should not rely on the user having `undef` available from Compat
59- # and OffsetArrays.jl should probably not re-export Compat.undef
60- @static if isdefined (Base, :UndefInitializer )
61- @deprecate OffsetArray {T,N} (inds:: Indices{N} ) where {T,N} OffsetArray {T,N} (undef, inds)
62- @deprecate OffsetArray {T} (inds:: Indices{N} ) where {T,N} OffsetArray {T} (undef, inds)
63- @deprecate OffsetArray {T,N} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} OffsetArray {T,N} (undef, inds)
64- @deprecate OffsetArray {T} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} OffsetArray {T} (undef, inds)
65- @deprecate OffsetVector {T} (inds:: AbstractUnitRange ) where {T} OffsetVector {T} (undef, inds)
66- else
67- OffsetArray {T,N} (inds:: Indices{N} ) where {T,N} = OffsetArray {T,N} (undef, inds)
68- OffsetArray {T} (inds:: Indices{N} ) where {T,N} = OffsetArray {T} (undef, inds)
69- OffsetArray {T,N} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} = OffsetArray {T,N} (undef, inds)
70- OffsetArray {T} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} = OffsetArray {T} (undef, inds)
71- OffsetVector {T} (inds:: AbstractUnitRange ) where {T} = OffsetVector {T} (undef, inds)
72- end
73-
34+ @deprecate OffsetArray (:: Type{T} , inds:: Vararg{UnitRange{Int},N} ) where {T,N} OffsetArray {T} (undef, inds)
35+ @deprecate OffsetVector (:: Type{T} , inds:: AbstractUnitRange ) where {T} OffsetVector {T} (undef, inds)
36+ @deprecate OffsetArray {T,N} (inds:: Indices{N} ) where {T,N} OffsetArray {T,N} (undef, inds)
37+ @deprecate OffsetArray {T} (inds:: Indices{N} ) where {T,N} OffsetArray {T} (undef, inds)
38+ @deprecate OffsetArray {T,N} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} OffsetArray {T,N} (undef, inds)
39+ @deprecate OffsetArray {T} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} OffsetArray {T} (undef, inds)
40+ @deprecate OffsetVector {T} (inds:: AbstractUnitRange ) where {T} OffsetVector {T} (undef, inds)
7441
7542# The next two are necessary for ambiguity resolution. Really, the
7643# second method should not be necessary.
@@ -100,12 +67,12 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1)
10067# Implementations of axes and indices1. Since bounds-checking is
10168# performance-critical and relies on axes, these are usually worth
10269# optimizing thoroughly.
103- @inline Compat . axes (A:: OffsetArray , d) =
104- 1 <= d <= length (A. offsets) ? AxisType ( plus ( axes (parent (A))[d], A. offsets[d]) ) : (1 : 1 )
105- @inline Compat . axes (A:: OffsetArray ) =
70+ @inline Base . axes (A:: OffsetArray , d) =
71+ 1 <= d <= length (A. offsets) ? Base . Slice ( axes (parent (A))[d] .+ A. offsets[d]) : (1 : 1 )
72+ @inline Base . axes (A:: OffsetArray ) =
10673 _axes (axes (parent (A)), A. offsets) # would rather use ntuple, but see #15276
10774@inline _axes (inds, offsets) =
108- (AxisType ( plus ( inds[1 ], offsets[1 ]) ), _axes (tail (inds), tail (offsets))... )
75+ (Base . Slice ( inds[1 ] .+ offsets[1 ]), _axes (tail (inds), tail (offsets))... )
10976_axes (:: Tuple{} , :: Tuple{} ) = ()
11077Base. indices1 (A:: OffsetArray{T,0} ) where {T} = 1 : 1 # we only need to specialize this one
11178
@@ -119,16 +86,6 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{OffsetAxis,Vararg
11986 OffsetArray (B, map (indexoffset, inds))
12087end
12188
122- if VERSION < v " 0.7.0-DEV.5242"
123- # Reshape's methods in Base changed, so using the new definitions leads to ambiguities
124- Base. reshape (A:: AbstractArray , inds:: Tuple{UnitRange,Vararg{UnitRange}} ) =
125- OffsetArray (reshape (A, map (length, inds)), map (indexoffset, inds))
126- Base. reshape (A:: OffsetArray , inds:: Tuple{UnitRange,Vararg{UnitRange}} ) =
127- OffsetArray (reshape (parent (A), map (length, inds)), map (indexoffset, inds))
128- function Base. reshape (A:: OffsetArray , inds:: Tuple{UnitRange,Vararg{Union{UnitRange,Int,Base.OneTo}}} )
129- throw (ArgumentError (" reshape must supply UnitRange axes, got $(typeof (inds)) .\n Note that reshape(A, Val{N}) is not supported for OffsetArrays." ))
130- end
131- else
13289Base. reshape (A:: AbstractArray , inds:: Tuple{OffsetAxis,Vararg{OffsetAxis}} ) =
13390 OffsetArray (reshape (A, map (indexlength, inds)), map (indexoffset, inds))
13491
@@ -139,30 +96,20 @@ Base.reshape(A::OffsetArray, inds::Tuple{OffsetAxis,Vararg{OffsetAxis}}) =
13996# And for non-offset axes, we can just return a reshape of the parent directly
14097Base. reshape (A:: OffsetArray , inds:: Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}} ) = reshape (parent (A), inds)
14198Base. reshape (A:: OffsetArray , inds:: Dims ) = reshape (parent (A), inds)
142- end
14399
144- if VERSION < v " 0.7.0-DEV.4873"
145- # Julia PR #26733 removed similar(f, ...) in favor of just using method extension directly
146- # https://github.com/JuliaLang/julia/pull/26733
147- Base. similar (:: Type{T} , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) where {T<: AbstractArray } =
148- OffsetArray (T (undef, map (indexlength, shape)), map (indexoffset, shape))
149- Base. similar (f:: Function , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) =
150- OffsetArray (f (map (indexlength, shape)), map (indexoffset, shape))
151- else
152- Base. similar (:: Type{T} , shape:: Tuple{OffsetAxis,Vararg{OffsetAxis}} ) where {T<: AbstractArray } =
153- OffsetArray (T (undef, map (indexlength, shape)), map (indexoffset, shape))
154-
155- Base. fill (v, inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
156- fill! (OffsetArray (Array {typeof(v), N} (undef, map (indexlength, inds)), map (indexoffset, inds)), v)
157- Base. zeros (:: Type{T} , inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {T, N} =
158- fill! (OffsetArray (Array {T, N} (undef, map (indexlength, inds)), map (indexoffset, inds)), zero (T))
159- Base. ones (:: Type{T} , inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {T, N} =
160- fill! (OffsetArray (Array {T, N} (undef, map (indexlength, inds)), map (indexoffset, inds)), one (T))
161- Base. trues (inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
162- fill! (OffsetArray (BitArray {N} (undef, map (indexlength, inds)), map (indexoffset, inds)), true )
163- Base. falses (inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
164- fill! (OffsetArray (BitArray {N} (undef, map (indexlength, inds)), map (indexoffset, inds)), false )
165- end
100+ Base. similar (:: Type{T} , shape:: Tuple{OffsetAxis,Vararg{OffsetAxis}} ) where {T<: AbstractArray } =
101+ OffsetArray (T (undef, map (indexlength, shape)), map (indexoffset, shape))
102+
103+ Base. fill (v, inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
104+ fill! (OffsetArray (Array {typeof(v), N} (undef, map (indexlength, inds)), map (indexoffset, inds)), v)
105+ Base. zeros (:: Type{T} , inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {T, N} =
106+ fill! (OffsetArray (Array {T, N} (undef, map (indexlength, inds)), map (indexoffset, inds)), zero (T))
107+ Base. ones (:: Type{T} , inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {T, N} =
108+ fill! (OffsetArray (Array {T, N} (undef, map (indexlength, inds)), map (indexoffset, inds)), one (T))
109+ Base. trues (inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
110+ fill! (OffsetArray (BitArray {N} (undef, map (indexlength, inds)), map (indexoffset, inds)), true )
111+ Base. falses (inds:: NTuple{N, Union{Integer, AbstractUnitRange}} ) where {N} =
112+ fill! (OffsetArray (BitArray {N} (undef, map (indexlength, inds)), map (indexoffset, inds)), false )
166113
167114# Don't allow bounds-checks to be removed during Julia 0.5
168115@inline function Base. getindex (A:: OffsetArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
@@ -293,22 +240,20 @@ end
293240@inline unsafe_getindex (a:: OffsetSubArray , I:: Union{Integer,CartesianIndex} ...) = unsafe_getindex (a, Base. IteratorsMD. flatten (I)... )
294241@inline unsafe_setindex! (a:: OffsetSubArray , val, I:: Union{Integer,CartesianIndex} ...) = unsafe_setindex! (a, val, Base. IteratorsMD. flatten (I)... )
295242
296- if VERSION >= v " 0.7.0-DEV.1790"
297- function Base. showarg (io:: IO , a:: OffsetArray , toplevel)
298- print (io, " OffsetArray(" )
299- Base. showarg (io, parent (a), false )
300- if ndims (a) > 0
301- print (io, " , " )
302- printindices (io, axes (a)... )
303- end
304- print (io, ' )' )
305- toplevel && print (io, " with eltype " , eltype (a))
243+ function Base. showarg (io:: IO , a:: OffsetArray , toplevel)
244+ print (io, " OffsetArray(" )
245+ Base. showarg (io, parent (a), false )
246+ if ndims (a) > 0
247+ print (io, " , " )
248+ printindices (io, axes (a)... )
306249 end
307- printindices (io:: IO , ind1, inds... ) =
308- (print (io, _unslice (ind1), " , " ); printindices (io, inds... ))
309- printindices (io:: IO , ind1) = print (io, _unslice (ind1))
310- _unslice (x) = x
311- _unslice (x:: Base.Slice ) = x. indices
250+ print (io, ' )' )
251+ toplevel && print (io, " with eltype " , eltype (a))
312252end
253+ printindices (io:: IO , ind1, inds... ) =
254+ (print (io, _unslice (ind1), " , " ); printindices (io, inds... ))
255+ printindices (io:: IO , ind1) = print (io, _unslice (ind1))
256+ _unslice (x) = x
257+ _unslice (x:: Base.Slice ) = x. indices
313258
314259end # module
0 commit comments