@@ -12,7 +12,8 @@ export OffsetArray, OffsetMatrix, OffsetVector
12
12
include (" axes.jl" )
13
13
include (" utils.jl" )
14
14
15
- # Techniquely we know the length of CartesianIndices
15
+ # Technically we know the length of CartesianIndices but we need to convert it first, so here we
16
+ # don't put it in OffsetAxisKnownLength.
16
17
const OffsetAxisKnownLength = Union{Integer, UnitRange, Base. OneTo, IdentityUnitRange, IdOffsetRange}
17
18
const OffsetAxis = Union{OffsetAxisKnownLength, CartesianIndices, Colon}
18
19
const ArrayInitializer = Union{UndefInitializer, Missing, Nothing}
@@ -72,7 +73,7 @@ struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
72
73
parent:: AA
73
74
offsets:: NTuple{N,Int}
74
75
function OffsetArray {T, N, AA} (parent:: AA , offsets:: NTuple{N, Int} ) where {T, N, AA<: AbstractArray }
75
- overflow_check .(axes (parent), offsets)
76
+ @boundscheck overflow_check .(axes (parent), offsets)
76
77
new {T, N, AA} (parent, offsets)
77
78
end
78
79
end
95
96
for FT in (:OffsetArray , :OffsetVector , :OffsetMatrix )
96
97
# The only route out to inner constructor
97
98
@eval function $FT (A:: AbstractArray{T} , offsets:: NTuple{N, Integer} ) where {T, N}
98
- ndims (A) == N || throw (DimensionMismatch (" Array dimensions should equal to number of offsets " ))
99
+ ndims (A) == N || throw (DimensionMismatch (" The number of offsets should equal ndims(A) = $( ndims (A)) " ))
99
100
OffsetArray {T, ndims(A), typeof(A)} (A, offsets)
100
101
end
101
102
# nested OffsetArrays
102
- @eval function $FT (A:: OffsetArray{T} , offsets:: NTuple{N, Integer} ) where {T,N}
103
- $ FT (parent (A), A. offsets .+ offsets)
104
- end
103
+ @eval $ FT (A:: OffsetArray{T} , offsets:: NTuple{N, Integer} ) where {T,N} = $ FT (parent (A), A. offsets .+ offsets)
105
104
# convert ranges to offsets
106
105
@eval function $FT (A:: AbstractArray{T} , inds:: NTuple{N,OffsetAxisKnownLength} ) where {T,N}
107
106
axparent = axes (A)
@@ -112,37 +111,40 @@ for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix)
112
111
end
113
112
# lower CartesianIndices and Colon
114
113
@eval function $FT (A:: AbstractArray{T} , inds:: NTuple{N, OffsetAxis} ) where {T, N}
115
- indsN = _uncolonindices (A, _stripCartesianIndices ( _splitCartesianIndices ( inds) ))
114
+ indsN = _uncolonindices (A, _expandCartesianIndices ( inds))
116
115
$ FT (A, indsN)
117
116
end
118
- @eval function $FT (A:: AbstractArray{T} , inds:: Vararg{OffsetAxis,N} ) where {T, N}
119
- $ FT (A, _uncolonindices (A, _stripCartesianIndices (_splitCartesianIndices (inds))))
120
- end
121
- @eval $ FT (A:: AbstractArray , inds:: CartesianIndices ) = $ FT (A, inds. indices)
117
+ @eval $ FT (A:: AbstractArray{T} , inds:: Vararg{OffsetAxis,N} ) where {T, N} = $ FT (A, inds)
118
+ @eval $ FT (A:: AbstractArray , inds:: CartesianIndices ) = $ FT (A, convert (Tuple{Vararg{AbstractUnitRange{Int}}}, inds))
122
119
end
123
120
124
121
# array initialization
125
122
OffsetArray {T,N} (init:: ArrayInitializer , inds:: NTuple{N, OffsetAxisKnownLength} ) where {T,N} =
126
123
OffsetArray (Array {T,N} (init, map (_indexlength, inds)), map (_indexoffset, inds))
127
124
OffsetArray {T,N} (init:: ArrayInitializer , inds:: Vararg{OffsetAxisKnownLength,N} ) where {T,N} = OffsetArray {T,N} (init, inds)
128
125
function OffsetArray {T, N} (init:: ArrayInitializer , inds:: NTuple{N, Union{OffsetAxisKnownLength, CartesianIndices}} ) where {T, N}
129
- OffsetArray {T, N} (init, _stripCartesianIndices ( _splitCartesianIndices ( inds) ))
126
+ OffsetArray {T, N} (init, _expandCartesianIndices ( inds))
130
127
end
131
128
function OffsetArray {T, N} (init:: ArrayInitializer , inds:: Vararg{Union{OffsetAxisKnownLength, CartesianIndices}} ) where {T, N}
132
129
OffsetArray {T, N} (init, inds)
133
130
end
134
- OffsetArray {T, N} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N} = OffsetArray {T, N} (init, inds. indices)
131
+ function OffsetArray {T, N} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N}
132
+ OffsetArray {T, N} (init, convert (Tuple{Vararg{AbstractUnitRange{Int}}}, inds))
133
+ end
135
134
136
135
OffsetArray {T} (init:: ArrayInitializer , inds:: NTuple{N, OffsetAxisKnownLength} ) where {T,N} = OffsetArray {T,N} (init, inds)
137
136
OffsetArray {T} (init:: ArrayInitializer , inds:: Vararg{OffsetAxisKnownLength,N} ) where {T,N} = OffsetArray {T,N} (init, inds)
138
137
function OffsetArray {T} (init:: ArrayInitializer , inds:: NTuple{N, Union{OffsetAxisKnownLength, CartesianIndices}} ) where {T, N}
139
- indsN = _stripCartesianIndices (_splitCartesianIndices (inds)) # CartesianIndices might contain multiple dimensions
140
- OffsetArray {T, length(indsN)} (init, _stripCartesianIndices (_splitCartesianIndices (inds)))
138
+ # N is probably not the actual dimension of the array; CartesianIndices might contain multiple dimensions
139
+ indsN = _expandCartesianIndices (inds)
140
+ OffsetArray {T, length(indsN)} (init, indsN)
141
141
end
142
142
function OffsetArray {T} (init:: ArrayInitializer , inds:: Vararg{Union{OffsetAxisKnownLength, CartesianIndices}, N} ) where {T, N}
143
143
OffsetArray {T} (init, inds)
144
144
end
145
- OffsetArray {T} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N} = OffsetArray {T, N} (init, inds. indices)
145
+ function OffsetArray {T} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N}
146
+ OffsetArray {T, N} (init, convert (Tuple{Vararg{AbstractUnitRange{Int}}}, inds))
147
+ end
146
148
147
149
Base. IndexStyle (:: Type{OA} ) where {OA<: OffsetArray } = IndexStyle (parenttype (OA))
148
150
parenttype (:: Type{OffsetArray{T,N,AA}} ) where {T,N,AA} = AA
0 commit comments