@@ -4,6 +4,7 @@ module OffsetArrays
4
4
5
5
using Base: Indices, tail
6
6
using Compat
7
+ using Compat: axes, CartesianIndices
7
8
8
9
export OffsetArray, OffsetVector, @unsafe
9
10
@@ -27,7 +28,7 @@ OffsetArray(A::AbstractArray{T,N}, offsets::Vararg{Int,N}) where {T,N} =
27
28
OffsetArray (A, offsets)
28
29
29
30
OffsetArray {T,N} (inds:: Indices{N} ) where {T,N} =
30
- OffsetArray {T,N,Array{T,N}} (Array {T,N} (map (length, inds)), map (indexoffset, inds))
31
+ OffsetArray {T,N,Array{T,N}} (Array {T,N} (uninitialized, map (length, inds)), map (indexoffset, inds))
31
32
OffsetArray {T} (inds:: Indices{N} ) where {T,N} = OffsetArray {T,N} (inds)
32
33
OffsetArray {T,N} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} = OffsetArray {T,N} (inds)
33
34
OffsetArray {T} (inds:: Vararg{AbstractUnitRange,N} ) where {T,N} = OffsetArray {T,N} (inds)
@@ -44,9 +45,9 @@ OffsetVector{T}(inds::AbstractUnitRange) where {T} = OffsetArray{T}(inds)
44
45
OffsetArray (A:: AbstractArray{T,0} , inds:: Tuple{} ) where {T} = OffsetArray {T,0,typeof(A)} (A, ())
45
46
OffsetArray (A:: AbstractArray{T,N} , inds:: Tuple{} ) where {T,N} = error (" this should never be called" )
46
47
function OffsetArray (A:: AbstractArray{T,N} , inds:: NTuple{N,AbstractUnitRange} ) where {T,N}
47
- lA = map (length, indices (A))
48
+ lA = map (length, axes (A))
48
49
lI = map (length, inds)
49
- lA == lI || throw (DimensionMismatch (" supplied indices do not agree with the size of the array (got size $lA for the array and $lI for the indices" ))
50
+ lA == lI || throw (DimensionMismatch (" supplied axes do not agree with the size of the array (got size $lA for the array and $lI for the indices" ))
50
51
OffsetArray (A, map (indexoffset, inds))
51
52
end
52
53
OffsetArray (A:: AbstractArray{T,N} , inds:: Vararg{AbstractUnitRange,N} ) where {T,N} =
@@ -58,22 +59,22 @@ parenttype(A::OffsetArray) = parenttype(typeof(A))
58
59
59
60
Base. parent (A:: OffsetArray ) = A. parent
60
61
61
- errmsg (A) = error (" size not supported for arrays with indices $(indices (A)) ; see http://docs.julialang.org/en/latest/devdocs/offset-arrays/" )
62
+ errmsg (A) = error (" size not supported for arrays with axes $(axes (A)) ; see http://docs.julialang.org/en/latest/devdocs/offset-arrays/" )
62
63
Base. size (A:: OffsetArray ) = errmsg (A)
63
64
Base. size (A:: OffsetArray , d) = errmsg (A)
64
- Base. eachindex (:: IndexCartesian , A:: OffsetArray ) = CartesianRange ( indices (A))
65
- Base. eachindex (:: IndexLinear , A:: OffsetVector ) = indices (A, 1 )
65
+ Base. eachindex (:: IndexCartesian , A:: OffsetArray ) = CartesianIndices ( axes (A))
66
+ Base. eachindex (:: IndexLinear , A:: OffsetVector ) = axes (A, 1 )
66
67
67
- # Implementations of indices and indices1. Since bounds-checking is
68
- # performance-critical and relies on indices , these are usually worth
68
+ # Implementations of axes and indices1. Since bounds-checking is
69
+ # performance-critical and relies on axes , these are usually worth
69
70
# optimizing thoroughly.
70
- @inline Base . indices (A:: OffsetArray , d) =
71
- 1 <= d <= length (A. offsets) ? plus (indices (parent (A))[d], A. offsets[d]) : (1 : 1 )
72
- @inline Base . indices (A:: OffsetArray ) =
73
- _indices ( indices (parent (A)), A. offsets) # would rather use ntuple, but see #15276
74
- @inline _indices (inds, offsets) =
75
- (plus (inds[1 ], offsets[1 ]), _indices (tail (inds), tail (offsets))... )
76
- _indices (:: Tuple{} , :: Tuple{} ) = ()
71
+ @inline Compat . axes (A:: OffsetArray , d) =
72
+ 1 <= d <= length (A. offsets) ? plus (axes (parent (A))[d], A. offsets[d]) : (1 : 1 )
73
+ @inline Compat . axes (A:: OffsetArray ) =
74
+ _axes ( axes (parent (A)), A. offsets) # would rather use ntuple, but see #15276
75
+ @inline _axes (inds, offsets) =
76
+ (plus (inds[1 ], offsets[1 ]), _axes (tail (inds), tail (offsets))... )
77
+ _axes (:: Tuple{} , :: Tuple{} ) = ()
77
78
Base. indices1 (A:: OffsetArray{T,0} ) where {T} = 1 : 1 # we only need to specialize this one
78
79
79
80
function Base. similar (A:: OffsetArray , :: Type{T} , dims:: Dims ) where T
@@ -84,8 +85,14 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{UnitRange,Vararg{
84
85
OffsetArray (B, map (indexoffset, inds))
85
86
end
86
87
87
- Base. similar (f:: Union{ Function,Type} , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) =
88
+ Base. similar (f:: Function , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) =
88
89
OffsetArray (f (map (length, shape)), map (indexoffset, shape))
90
+ Base. similar (:: Type{T} , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) where {T<: OffsetArray } =
91
+ OffsetArray (T (map (length, shape)), map (indexoffset, shape))
92
+ Base. similar (:: Type{T} , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) where {T<: Array } =
93
+ OffsetArray (T (uninitialized, map (length, shape)), map (indexoffset, shape))
94
+ Base. similar (:: Type{T} , shape:: Tuple{UnitRange,Vararg{UnitRange}} ) where {T<: BitArray } =
95
+ OffsetArray (T (uninitialized, map (length, shape)), map (indexoffset, shape))
89
96
90
97
Base. reshape (A:: AbstractArray , inds:: Tuple{UnitRange,Vararg{UnitRange}} ) =
91
98
OffsetArray (reshape (A, map (length, inds)), map (indexoffset, inds))
@@ -94,7 +101,7 @@ Base.reshape(A::OffsetArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
94
101
OffsetArray (reshape (parent (A), map (length, inds)), map (indexoffset, inds))
95
102
96
103
function Base. reshape (A:: OffsetArray , inds:: Tuple{UnitRange,Vararg{Union{UnitRange,Int,Base.OneTo}}} )
97
- throw (ArgumentError (" reshape must supply UnitRange indices , got $(typeof (inds)) .\n Note that reshape(A, Val{N}) is not supported for OffsetArrays." ))
104
+ throw (ArgumentError (" reshape must supply UnitRange axes , got $(typeof (inds)) .\n Note that reshape(A, Val{N}) is not supported for OffsetArrays." ))
98
105
end
99
106
100
107
# Don't allow bounds-checks to be removed during Julia 0.5
@@ -198,20 +205,20 @@ end
198
205
ret
199
206
end
200
207
@inline _unsafe_getindex (:: IndexCartesian , a:: OffsetArray , i:: Int ) =
201
- unsafe_getindex (a, ind2sub ( indices (a), i) ... )
208
+ unsafe_getindex (a, CartesianIndices ( axes (a))[i] )
202
209
@inline function _unsafe_setindex! (:: IndexLinear , a:: OffsetArray , val, i:: Int )
203
210
@inbounds parent (a)[i] = val
204
211
val
205
212
end
206
213
@inline _unsafe_setindex! (:: IndexCartesian , a:: OffsetArray , val, i:: Int ) =
207
- unsafe_setindex! (a, val, ind2sub ( indices (a), i) ... )
214
+ unsafe_setindex! (a, val, CartesianIndices ( axes (a))[i] . .. )
208
215
209
216
@inline unsafe_getindex (a:: OffsetArray , I:: Int... ) = unsafe_getindex (parent (a), offset (a. offsets, I)... )
210
217
@inline unsafe_setindex! (a:: OffsetArray , val, I:: Int... ) = unsafe_setindex! (parent (a), val, offset (a. offsets, I)... )
211
218
@inline unsafe_getindex (a:: OffsetArray , I... ) = unsafe_getindex (a, Base. IteratorsMD. flatten (I)... )
212
219
@inline unsafe_setindex! (a:: OffsetArray , val, I... ) = unsafe_setindex! (a, val, Base. IteratorsMD. flatten (I)... )
213
220
214
- # Indexing a SubArray which has OffsetArray indices
221
+ # Indexing a SubArray which has OffsetArray axes
215
222
OffsetSubArray{T,N,P,I<: Tuple{OffsetArray,Vararg{OffsetArray}} } = SubArray{T,N,P,I,false }
216
223
@inline function unsafe_getindex (a:: OffsetSubArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
217
224
J = map (unsafe_getindex, a. indexes, I)
@@ -230,7 +237,7 @@ if VERSION >= v"0.7.0-DEV.1790"
230
237
Base. showarg (io, parent (a), false )
231
238
if ndims (a) > 0
232
239
print (io, " , " )
233
- printindices (io, indices (a)... )
240
+ printindices (io, axes (a)... )
234
241
end
235
242
print (io, ' )' )
236
243
toplevel && print (io, " with eltype " , eltype (a))
0 commit comments