@@ -3,22 +3,9 @@ __precompile__()
3
3
module OffsetArrays
4
4
5
5
using Base: Indices, tail
6
- using Compat
7
- using Compat: axes, CartesianIndices
8
6
9
7
export OffsetArray, OffsetVector, @unsafe
10
8
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
-
22
9
struct OffsetArray{T,N,AA<: AbstractArray } <: AbstractArray{T,N}
23
10
parent:: AA
24
11
offsets:: NTuple{N,Int}
@@ -44,33 +31,13 @@ OffsetVector{T}(::UndefInitializer, inds::AbstractUnitRange) where {T} = OffsetA
44
31
# deprecated constructors
45
32
using Base: @deprecate
46
33
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)
74
41
75
42
# The next two are necessary for ambiguity resolution. Really, the
76
43
# second method should not be necessary.
@@ -100,12 +67,12 @@ Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1)
100
67
# Implementations of axes and indices1. Since bounds-checking is
101
68
# performance-critical and relies on axes, these are usually worth
102
69
# 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 ) =
106
73
_axes (axes (parent (A)), A. offsets) # would rather use ntuple, but see #15276
107
74
@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))... )
109
76
_axes (:: Tuple{} , :: Tuple{} ) = ()
110
77
Base. indices1 (A:: OffsetArray{T,0} ) where {T} = 1 : 1 # we only need to specialize this one
111
78
@@ -119,16 +86,6 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{OffsetAxis,Vararg
119
86
OffsetArray (B, map (indexoffset, inds))
120
87
end
121
88
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
132
89
Base. reshape (A:: AbstractArray , inds:: Tuple{OffsetAxis,Vararg{OffsetAxis}} ) =
133
90
OffsetArray (reshape (A, map (indexlength, inds)), map (indexoffset, inds))
134
91
@@ -139,30 +96,20 @@ Base.reshape(A::OffsetArray, inds::Tuple{OffsetAxis,Vararg{OffsetAxis}}) =
139
96
# And for non-offset axes, we can just return a reshape of the parent directly
140
97
Base. reshape (A:: OffsetArray , inds:: Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}} ) = reshape (parent (A), inds)
141
98
Base. reshape (A:: OffsetArray , inds:: Dims ) = reshape (parent (A), inds)
142
- end
143
99
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 )
166
113
167
114
# Don't allow bounds-checks to be removed during Julia 0.5
168
115
@inline function Base. getindex (A:: OffsetArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
@@ -293,22 +240,20 @@ end
293
240
@inline unsafe_getindex (a:: OffsetSubArray , I:: Union{Integer,CartesianIndex} ...) = unsafe_getindex (a, Base. IteratorsMD. flatten (I)... )
294
241
@inline unsafe_setindex! (a:: OffsetSubArray , val, I:: Union{Integer,CartesianIndex} ...) = unsafe_setindex! (a, val, Base. IteratorsMD. flatten (I)... )
295
242
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)... )
306
249
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))
312
252
end
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
313
258
314
259
end # module
0 commit comments