@@ -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
91
92
for FT in (:OffsetArray , :OffsetVector , :OffsetMatrix )
92
93
# The only route out to inner constructor
93
94
@eval function $FT (A:: AbstractArray{T} , offsets:: NTuple{N, Integer} ) where {T, N}
94
- ndims (A) == N || throw (DimensionMismatch (" Array dimensions should equal to number of offsets " ))
95
+ ndims (A) == N || throw (DimensionMismatch (" The number of offsets should equal ndims(A) = $( ndims (A)) " ))
95
96
OffsetArray {T, ndims(A), typeof(A)} (A, offsets)
96
97
end
97
98
# nested OffsetArrays
98
- @eval function $FT (A:: OffsetArray{T} , offsets:: NTuple{N, Integer} ) where {T,N}
99
- $ FT (parent (A), A. offsets .+ offsets)
100
- end
99
+ @eval $ FT (A:: OffsetArray{T} , offsets:: NTuple{N, Integer} ) where {T,N} = $ FT (parent (A), A. offsets .+ offsets)
101
100
# convert ranges to offsets
102
101
@eval function $FT (A:: AbstractArray{T} , inds:: NTuple{N,OffsetAxisKnownLength} ) where {T,N}
103
102
axparent = axes (A)
@@ -108,37 +107,40 @@ for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix)
108
107
end
109
108
# lower CartesianIndices and Colon
110
109
@eval function $FT (A:: AbstractArray{T} , inds:: NTuple{N, OffsetAxis} ) where {T, N}
111
- indsN = _uncolonindices (A, _stripCartesianIndices ( _splitCartesianIndices ( inds) ))
110
+ indsN = _uncolonindices (A, _expandCartesianIndices ( inds))
112
111
$ FT (A, indsN)
113
112
end
114
- @eval function $FT (A:: AbstractArray{T} , inds:: Vararg{OffsetAxis,N} ) where {T, N}
115
- $ FT (A, _uncolonindices (A, _stripCartesianIndices (_splitCartesianIndices (inds))))
116
- end
117
- @eval $ FT (A:: AbstractArray , inds:: CartesianIndices ) = $ FT (A, inds. indices)
113
+ @eval $ FT (A:: AbstractArray{T} , inds:: Vararg{OffsetAxis,N} ) where {T, N} = $ FT (A, inds)
114
+ @eval $ FT (A:: AbstractArray , inds:: CartesianIndices ) = $ FT (A, convert (Tuple{Vararg{AbstractUnitRange{Int}}}, inds))
118
115
end
119
116
120
117
# array initialization
121
118
OffsetArray {T,N} (init:: ArrayInitializer , inds:: NTuple{N, OffsetAxisKnownLength} ) where {T,N} =
122
119
OffsetArray (Array {T,N} (init, map (_indexlength, inds)), map (_indexoffset, inds))
123
120
OffsetArray {T,N} (init:: ArrayInitializer , inds:: Vararg{OffsetAxisKnownLength,N} ) where {T,N} = OffsetArray {T,N} (init, inds)
124
121
function OffsetArray {T, N} (init:: ArrayInitializer , inds:: NTuple{N, Union{OffsetAxisKnownLength, CartesianIndices}} ) where {T, N}
125
- OffsetArray {T, N} (init, _stripCartesianIndices ( _splitCartesianIndices ( inds) ))
122
+ OffsetArray {T, N} (init, _expandCartesianIndices ( inds))
126
123
end
127
124
function OffsetArray {T, N} (init:: ArrayInitializer , inds:: Vararg{Union{OffsetAxisKnownLength, CartesianIndices}} ) where {T, N}
128
125
OffsetArray {T, N} (init, inds)
129
126
end
130
- OffsetArray {T, N} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N} = OffsetArray {T, N} (init, inds. indices)
127
+ function OffsetArray {T, N} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N}
128
+ OffsetArray {T, N} (init, convert (Tuple{Vararg{AbstractUnitRange{Int}}}, inds))
129
+ end
131
130
132
131
OffsetArray {T} (init:: ArrayInitializer , inds:: NTuple{N, OffsetAxisKnownLength} ) where {T,N} = OffsetArray {T,N} (init, inds)
133
132
OffsetArray {T} (init:: ArrayInitializer , inds:: Vararg{OffsetAxisKnownLength,N} ) where {T,N} = OffsetArray {T,N} (init, inds)
134
133
function OffsetArray {T} (init:: ArrayInitializer , inds:: NTuple{N, Union{OffsetAxisKnownLength, CartesianIndices}} ) where {T, N}
135
- indsN = _stripCartesianIndices (_splitCartesianIndices (inds)) # CartesianIndices might contain multiple dimensions
136
- OffsetArray {T, length(indsN)} (init, _stripCartesianIndices (_splitCartesianIndices (inds)))
134
+ # N is probably not the actual dimension of the array; CartesianIndices might contain multiple dimensions
135
+ indsN = _expandCartesianIndices (inds)
136
+ OffsetArray {T, length(indsN)} (init, indsN)
137
137
end
138
138
function OffsetArray {T} (init:: ArrayInitializer , inds:: Vararg{Union{OffsetAxisKnownLength, CartesianIndices}, N} ) where {T, N}
139
139
OffsetArray {T} (init, inds)
140
140
end
141
- OffsetArray {T} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N} = OffsetArray {T, N} (init, inds. indices)
141
+ function OffsetArray {T} (init:: ArrayInitializer , inds:: CartesianIndices{N} ) where {T,N}
142
+ OffsetArray {T, N} (init, convert (Tuple{Vararg{AbstractUnitRange{Int}}}, inds))
143
+ end
142
144
143
145
Base. IndexStyle (:: Type{OA} ) where {OA<: OffsetArray } = IndexStyle (parenttype (OA))
144
146
parenttype (:: Type{OffsetArray{T,N,AA}} ) where {T,N,AA} = AA
0 commit comments