@@ -8,8 +8,9 @@ Reduce the size of `img` by approximately two-fold along the dimensions listed i
8
8
9
9
The type of output array `imgr` depends on the input type:
10
10
11
- - If `img` is not an `OffsetArray`, then output array `imgr` will be a typical `Array` type.
12
11
- If `img` is an `OffsetArray`, then output array `imgr` will also be an `OffsetArray`.
12
+ - If `img` is not an `OffsetArray`, then output array `imgr` will be an `Array` type
13
+ even if it has offset indices.
13
14
14
15
The size of `imgr` is approximately `1/2` of the original size. More specifically:
15
16
@@ -122,7 +123,10 @@ function restrict(A::AbstractArray, dims::Dims)
122
123
end
123
124
124
125
function restrict (A:: AbstractArray{T,N} , dim:: Integer ) where {T,N}
125
- require_one_based_indexing (A)
126
+ if Base. has_offset_axes (A)
127
+ # For type stability, we cannot return `OffsetArray` in this method
128
+ A = OffsetArrays. no_offset_view (A)
129
+ end
126
130
127
131
indsA = axes (A)
128
132
newinds = ntuple (i-> i== dim ? restrict_indices (indsA[dim]) : indsA[i], Val (N))
133
137
function restrict (A:: OffsetArray{T,N} , dim:: Integer ) where {T,N}
134
138
indsA = axes (A)
135
139
newinds = map (UnitRange, ntuple (i-> i== dim ? restrict_indices (indsA[dim]) : indsA[i], Val (N)))
136
- # This calls OffsetArrays implementation: a type piracy
137
- # https://github.com/JuliaArrays/OffsetArrays.jl/issues/87
138
- out = similar (A, restrict_eltype (first (A)), newinds)
139
- restrict! (out, A, dim)
140
- out
140
+ # By shifting it back to normal array, the internal for loop becomes faster because
141
+ # it requires less indexing computation
142
+ OffsetArray (restrict (A. parent, dim), newinds)
141
143
end
142
144
143
145
function restrict_eltype (A:: AbstractArray )
0 commit comments