@@ -43,7 +43,7 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
4343 if N != 0 && sizeof (S) != sizeof (T)
4444 ax1 = axes (a)[1 ]
4545 dim = length (ax1)
46- if Base . issingletontype (T)
46+ if issingletontype (T)
4747 dim == 0 || throwsingleton (S, T, " a non-empty" )
4848 else
4949 rem (dim* sizeof (S),sizeof (T)) == 0 || thrownonint (S, T, dim)
@@ -75,15 +75,15 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T
7575 if sizeof (S) == sizeof (T)
7676 N = ndims (a)
7777 elseif sizeof (S) > sizeof (T)
78- Base . issingletontype (T) && throwsingleton (S, T, " with reshape a" )
78+ issingletontype (T) && throwsingleton (S, T, " with reshape a" )
7979 rem (sizeof (S), sizeof (T)) == 0 || throwintmult (S, T)
8080 N = ndims (a) + 1
8181 else
82- Base . issingletontype (S) && throwfromsingleton (S, T)
82+ issingletontype (S) && throwfromsingleton (S, T)
8383 rem (sizeof (T), sizeof (S)) == 0 || throwintmult (S, T)
8484 N = ndims (a) - 1
8585 N > - 1 || throwsize0 (S, T, " larger" )
86- axes (a, 1 ) == Base . OneTo (sizeof (T) ÷ sizeof (S)) || throwsize1 (a, T)
86+ axes (a, 1 ) == OneTo (sizeof (T) ÷ sizeof (S)) || throwsize1 (a, T)
8787 end
8888 readable = array_subpadding (T, S)
8989 writable = array_subpadding (S, T)
@@ -148,33 +148,39 @@ StridedVector{T} = StridedArray{T,1}
148148StridedMatrix{T} = StridedArray{T,2 }
149149StridedVecOrMat{T} = Union{StridedVector{T}, StridedMatrix{T}}
150150
151- # the definition of strides for Array{T,N} is tuple() if N = 0, otherwise it is
152- # a tuple containing 1 and a cumulative product of the first N-1 sizes
153- # this definition is also used for StridedReshapedArray and StridedReinterpretedArray
154- # which have the same memory storage as Array
155- stride (a:: Union{DenseArray,StridedReshapedArray,StridedReinterpretArray} , i:: Int ) = _stride (a, i)
156-
157- function stride (a:: ReinterpretArray , i:: Int )
158- a. parent isa StridedArray || throw (ArgumentError (" Parent must be strided." ))
159- return _stride (a, i)
160- end
151+ strides (a:: Union{DenseArray,StridedReshapedArray,StridedReinterpretArray} ) = size_to_strides (1 , size (a)... )
161152
162- function _stride (a, i)
163- if i > ndims (a)
164- return length (a)
153+ function strides (a:: ReshapedReinterpretArray )
154+ ap = parent (a)
155+ els, elp = elsize (a), elsize (ap)
156+ stp = strides (ap)
157+ els == elp && return stp
158+ els < elp && return (1 , _checked_strides (stp, els, elp)... )
159+ stp[1 ] == 1 || throw (ArgumentError (" Parent must be contiguous in the 1st dimension!" ))
160+ return _checked_strides (tail (stp), els, elp)
161+ end
162+
163+ function strides (a:: NonReshapedReinterpretArray )
164+ ap = parent (a)
165+ els, elp = elsize (a), elsize (ap)
166+ stp = strides (ap)
167+ els == elp && return stp
168+ stp[1 ] == 1 || throw (ArgumentError (" Parent must be contiguous in the 1st dimension!" ))
169+ return (1 , _checked_strides (tail (stp), els, elp)... )
170+ end
171+
172+ @inline function _checked_strides (stp:: Tuple , els:: Integer , elp:: Integer )
173+ if elp > els && rem (elp, els) == 0
174+ N = div (elp, els)
175+ return map (i -> N * i, stp)
165176 end
166- s = 1
167- for n = 1 : (i- 1 )
168- s *= size (a, n)
169- end
170- return s
177+ drs = map (i -> divrem (elp * i, els), stp)
178+ all (i-> iszero (i[2 ]), drs) ||
179+ throw (ArgumentError (" Parent's strides could not be exactly divided!" ))
180+ map (first, drs)
171181end
172182
173- function strides (a:: ReinterpretArray )
174- a. parent isa StridedArray || throw (ArgumentError (" Parent must be strided." ))
175- size_to_strides (1 , size (a)... )
176- end
177- strides (a:: Union{DenseArray,StridedReshapedArray,StridedReinterpretArray} ) = size_to_strides (1 , size (a)... )
183+ _checkcontiguous (:: Type{Bool} , A:: ReinterpretArray ) = _checkcontiguous (Bool, parent (A))
178184
179185similar (a:: ReinterpretArray , T:: Type , d:: Dims ) = similar (a. parent, T, d)
180186
@@ -227,12 +233,12 @@ SCartesianIndices2{K}(indices2::AbstractUnitRange{Int}) where {K} = (@assert K::
227233eachindex (:: IndexSCartesian2{K} , A:: ReshapedReinterpretArray ) where {K} = SCartesianIndices2 {K} (eachindex (IndexLinear (), parent (A)))
228234@inline function eachindex (style:: IndexSCartesian2{K} , A:: AbstractArray , B:: AbstractArray... ) where {K}
229235 iter = eachindex (style, A)
230- Base . _all_match_first (C-> eachindex (style, C), iter, B... ) || Base . throw_eachindex_mismatch_indices (IndexSCartesian2 {K} (), axes (A), axes .(B)... )
236+ _all_match_first (C-> eachindex (style, C), iter, B... ) || throw_eachindex_mismatch_indices (IndexSCartesian2 {K} (), axes (A), axes .(B)... )
231237 return iter
232238end
233239
234240size (iter:: SCartesianIndices2{K} ) where K = (K, length (iter. indices2))
235- axes (iter:: SCartesianIndices2{K} ) where K = (Base . OneTo (K), iter. indices2)
241+ axes (iter:: SCartesianIndices2{K} ) where K = (OneTo (K), iter. indices2)
236242
237243first (iter:: SCartesianIndices2{K} ) where {K} = SCartesianIndex2 {K} (1 , first (iter. indices2))
238244last (iter:: SCartesianIndices2{K} ) where {K} = SCartesianIndex2 {K} (K, last (iter. indices2))
@@ -300,27 +306,27 @@ unaliascopy(a::ReshapedReinterpretArray{T}) where {T} = reinterpret(reshape, T,
300306
301307function size (a:: NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
302308 psize = size (a. parent)
303- size1 = Base . issingletontype (T) ? psize[1 ] : div (psize[1 ]* sizeof (S), sizeof (T))
309+ size1 = issingletontype (T) ? psize[1 ] : div (psize[1 ]* sizeof (S), sizeof (T))
304310 tuple (size1, tail (psize)... )
305311end
306312function size (a:: ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
307313 psize = size (a. parent)
308314 sizeof (S) > sizeof (T) && return (div (sizeof (S), sizeof (T)), psize... )
309- sizeof (S) < sizeof (T) && return Base . tail (psize)
315+ sizeof (S) < sizeof (T) && return tail (psize)
310316 return psize
311317end
312318size (a:: NonReshapedReinterpretArray{T,0} ) where {T} = ()
313319
314320function axes (a:: NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
315321 paxs = axes (a. parent)
316322 f, l = first (paxs[1 ]), length (paxs[1 ])
317- size1 = Base . issingletontype (T) ? l : div (l* sizeof (S), sizeof (T))
323+ size1 = issingletontype (T) ? l : div (l* sizeof (S), sizeof (T))
318324 tuple (oftype (paxs[1 ], f: f+ size1- 1 ), tail (paxs)... )
319325end
320326function axes (a:: ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
321327 paxs = axes (a. parent)
322- sizeof (S) > sizeof (T) && return (Base . OneTo (div (sizeof (S), sizeof (T))), paxs... )
323- sizeof (S) < sizeof (T) && return Base . tail (paxs)
328+ sizeof (S) > sizeof (T) && return (OneTo (div (sizeof (S), sizeof (T))), paxs... )
329+ sizeof (S) < sizeof (T) && return tail (paxs)
324330 return paxs
325331end
326332axes (a:: NonReshapedReinterpretArray{T,0} ) where {T} = ()
372378@inline @propagate_inbounds function _getindex_ra (a:: NonReshapedReinterpretArray{T,N,S} , i1:: Int , tailinds:: TT ) where {T,N,S,TT}
373379 # Make sure to match the scalar reinterpret if that is applicable
374380 if sizeof (T) == sizeof (S) && (fieldcount (T) + fieldcount (S)) == 0
375- if Base . issingletontype (T) # singleton types
381+ if issingletontype (T) # singleton types
376382 @boundscheck checkbounds (a, i1, tailinds... )
377383 return T. instance
378384 end
420426@inline @propagate_inbounds function _getindex_ra (a:: ReshapedReinterpretArray{T,N,S} , i1:: Int , tailinds:: TT ) where {T,N,S,TT}
421427 # Make sure to match the scalar reinterpret if that is applicable
422428 if sizeof (T) == sizeof (S) && (fieldcount (T) + fieldcount (S)) == 0
423- if Base . issingletontype (T) # singleton types
429+ if issingletontype (T) # singleton types
424430 @boundscheck checkbounds (a, i1, tailinds... )
425431 return T. instance
426432 end
511517 v = convert (T, v):: T
512518 # Make sure to match the scalar reinterpret if that is applicable
513519 if sizeof (T) == sizeof (S) && (fieldcount (T) + fieldcount (S)) == 0
514- if Base . issingletontype (T) # singleton types
520+ if issingletontype (T) # singleton types
515521 @boundscheck checkbounds (a, i1, tailinds... )
516522 # setindex! is a noop except for the index check
517523 else
577583 v = convert (T, v):: T
578584 # Make sure to match the scalar reinterpret if that is applicable
579585 if sizeof (T) == sizeof (S) && (fieldcount (T) + fieldcount (S)) == 0
580- if Base . issingletontype (T) # singleton types
586+ if issingletontype (T) # singleton types
581587 @boundscheck checkbounds (a, i1, tailinds... )
582588 # setindex! is a noop except for the index check
583589 else
0 commit comments