11
2- function known_lastindex (:: Type{T} ) where {T}
3- if known_offset1 (T) === nothing || known_length (T) === nothing
4- return nothing
5- else
6- return known_length (T) - known_offset1 (T) + 1
7- end
8- end
9- known_lastindex (@nospecialize x) = known_lastindex (typeof (x))
10-
11- @inline static_lastindex (x) = Static. maybe_static (known_lastindex, lastindex, x)
12-
13- function Base. first (x:: AbstractVector , n:: StaticInt )
14- @boundscheck n < 0 && throw (ArgumentError (" Number of elements must be nonnegative" ))
15- start = offset1 (x)
16- @inbounds x[start: min ((start - one (start)) + n, static_lastindex (x))]
17- end
18-
19- function Base. last (x:: AbstractVector , n:: StaticInt )
20- @boundscheck n < 0 && throw (ArgumentError (" Number of elements must be nonnegative" ))
21- stop = static_lastindex (x)
22- @inbounds x[max (offset1 (x), (stop + one (stop)) - n): stop]
23- end
24-
252"""
263 ArrayInterface.to_indices(A, I::Tuple) -> Tuple
274
@@ -162,16 +139,16 @@ to_index(::LinearIndices, i::AbstractArray{Bool}) = LogicalIndex{Int}(i)
162139@inline to_index (x, i:: NDIndex ) = getfield (i, 1 )
163140@inline to_index (x, i:: AbstractArray{<:AbstractCartesianIndex} ) = i
164141@inline function to_index (x, i:: Base.Fix2{<:Union{typeof(<),typeof(isless)},<:Union{Base.BitInteger,StaticInt}} )
165- static_first (x): min (_sub1 (canonicalize (i. x)), static_last (x))
142+ static_first (x): min (_sub1 (IntType (i. x)), static_last (x))
166143end
167144@inline function to_index (x, i:: Base.Fix2{typeof(<=),<:Union{Base.BitInteger,StaticInt}} )
168- static_first (x): min (canonicalize (i. x), static_last (x))
145+ static_first (x): min (IntType (i. x), static_last (x))
169146end
170147@inline function to_index (x, i:: Base.Fix2{typeof(>=),<:Union{Base.BitInteger,StaticInt}} )
171- max (canonicalize (i. x), static_first (x)): static_last (x)
148+ max (IntType (i. x), static_first (x)): static_last (x)
172149end
173150@inline function to_index (x, i:: Base.Fix2{typeof(>),<:Union{Base.BitInteger,StaticInt}} )
174- max (_add1 (canonicalize (i. x)), static_first (x)): static_last (x)
151+ max (_add1 (IntType (i. x)), static_first (x)): static_last (x)
175152end
176153# integer indexing
177154to_index (x, i:: AbstractArray{<:Integer} ) = i
@@ -232,7 +209,7 @@ indices calling [`to_axis`](@ref).
232209 end
233210end
234211# drop this dimension
235- to_axes (A, a:: Tuple , i:: Tuple{<:CanonicalInt ,Vararg{Any}} ) = to_axes (A, _maybe_tail (a), tail (i))
212+ to_axes (A, a:: Tuple , i:: Tuple{<:IntType ,Vararg{Any}} ) = to_axes (A, _maybe_tail (a), tail (i))
236213to_axes (A, a:: Tuple , i:: Tuple{I,Vararg{Any}} ) where {I} = _to_axes (StaticInt (ndims_index (I)), A, a, i)
237214function _to_axes (:: StaticInt{1} , A, axs:: Tuple , inds:: Tuple )
238215 return (to_axis (_maybe_first (axs), first (inds)), to_axes (A, _maybe_tail (axs), tail (inds))... )
@@ -309,15 +286,15 @@ function unsafe_getindex(a::A) where {A}
309286end
310287
311288# TODO Need to manage index transformations between nested layers of arrays
312- function unsafe_getindex (a:: A , i:: CanonicalInt ) where {A}
289+ function unsafe_getindex (a:: A , i:: IntType ) where {A}
313290 if IndexStyle (A) === IndexLinear ()
314291 is_forwarding_wrapper (A) || throw (MethodError (unsafe_getindex, (A, i)))
315292 return unsafe_getindex (parent (a), i)
316293 else
317294 return unsafe_getindex (a, _to_cartesian (a, i)... )
318295 end
319296end
320- function unsafe_getindex (a:: A , i:: CanonicalInt , ii:: Vararg{CanonicalInt } ) where {A}
297+ function unsafe_getindex (a:: A , i:: IntType , ii:: Vararg{IntType } ) where {A}
321298 if IndexStyle (A) === IndexLinear ()
322299 return unsafe_getindex (a, _to_linear (a, (i, ii... )))
323300 else
@@ -329,24 +306,24 @@ end
329306unsafe_getindex (a, i:: Vararg{Any} ) = unsafe_get_collection (a, i)
330307
331308unsafe_getindex (A:: Array ) = Base. arrayref (false , A, 1 )
332- unsafe_getindex (A:: Array , i:: CanonicalInt ) = Base. arrayref (false , A, Int (i))
333- @inline function unsafe_getindex (A:: Array , i:: CanonicalInt , ii:: Vararg{CanonicalInt } )
309+ unsafe_getindex (A:: Array , i:: IntType ) = Base. arrayref (false , A, Int (i))
310+ @inline function unsafe_getindex (A:: Array , i:: IntType , ii:: Vararg{IntType } )
334311 unsafe_getindex (A, _to_linear (A, (i, ii... )))
335312end
336313
337- unsafe_getindex (A:: LinearIndices , i:: CanonicalInt ) = Int (i)
338- unsafe_getindex (A:: CartesianIndices{N} , ii:: Vararg{CanonicalInt ,N} ) where {N} = CartesianIndex (ii... )
339- unsafe_getindex (A:: CartesianIndices , ii:: Vararg{CanonicalInt } ) =
314+ unsafe_getindex (A:: LinearIndices , i:: IntType ) = Int (i)
315+ unsafe_getindex (A:: CartesianIndices{N} , ii:: Vararg{IntType ,N} ) where {N} = CartesianIndex (ii... )
316+ unsafe_getindex (A:: CartesianIndices , ii:: Vararg{IntType } ) =
340317 unsafe_getindex (A, Base. front (ii)... )
341- unsafe_getindex (A:: CartesianIndices , i:: CanonicalInt ) = @inbounds (A[i])
318+ unsafe_getindex (A:: CartesianIndices , i:: IntType ) = @inbounds (A[i])
342319
343- unsafe_getindex (A:: ReshapedArray , i:: CanonicalInt ) = @inbounds (parent (A)[i])
344- function unsafe_getindex (A:: ReshapedArray , i:: CanonicalInt , ii:: Vararg{CanonicalInt } )
320+ unsafe_getindex (A:: ReshapedArray , i:: IntType ) = @inbounds (parent (A)[i])
321+ function unsafe_getindex (A:: ReshapedArray , i:: IntType , ii:: Vararg{IntType } )
345322 @inbounds (parent (A)[_to_linear (A, (i, ii... ))])
346323end
347324
348- unsafe_getindex (A:: SubArray , i:: CanonicalInt ) = @inbounds (A[i])
349- unsafe_getindex (A:: SubArray , i:: CanonicalInt , ii:: Vararg{CanonicalInt } ) = @inbounds (A[i, ii... ])
325+ unsafe_getindex (A:: SubArray , i:: IntType ) = @inbounds (A[i])
326+ unsafe_getindex (A:: SubArray , i:: IntType , ii:: Vararg{IntType } ) = @inbounds (A[i, ii... ])
350327
351328# This is based on Base._unsafe_getindex from https://github.com/JuliaLang/julia/blob/c5ede45829bf8eb09f2145bfd6f089459d77b2b1/base/multidimensional.jl#L755.
352329#=
@@ -364,17 +341,17 @@ function unsafe_get_collection(A, inds)
364341 end
365342 return dest
366343end
367- _ints2range (x:: CanonicalInt ) = x: x
344+ _ints2range (x:: IntType ) = x: x
368345_ints2range (x:: AbstractRange ) = x
369346# apply _ints2range to front N elements
370347_ints2range_front (:: Val{N} , ind, inds... ) where {N} =
371348 (_ints2range (ind), _ints2range_front (Val (N - 1 ), inds... )... )
372349_ints2range_front (:: Val{0} , ind, inds... ) = ()
373350_ints2range_front (:: Val{0} ) = ()
374351# get output shape with given indices
375- _output_shape (:: CanonicalInt , inds... ) = _output_shape (inds... )
352+ _output_shape (:: IntType , inds... ) = _output_shape (inds... )
376353_output_shape (ind:: AbstractRange , inds... ) = (Base. length (ind), _output_shape (inds... )... )
377- _output_shape (:: CanonicalInt ) = ()
354+ _output_shape (:: IntType ) = ()
378355_output_shape (x:: AbstractRange ) = (Base. length (x),)
379356@inline function unsafe_get_collection (A:: CartesianIndices{N} , inds) where {N}
380357 if (Base. length (inds) === 1 && N > 1 ) || stride_preserving_index (typeof (inds)) === False ()
@@ -426,15 +403,15 @@ function unsafe_setindex!(a::A, v) where {A}
426403 return unsafe_setindex! (parent (a), v)
427404end
428405# TODO Need to manage index transformations between nested layers of arrays
429- function unsafe_setindex! (a:: A , v, i:: CanonicalInt ) where {A}
406+ function unsafe_setindex! (a:: A , v, i:: IntType ) where {A}
430407 if IndexStyle (A) === IndexLinear ()
431408 is_forwarding_wrapper (A) || throw (MethodError (unsafe_setindex!, (A, v, i)))
432409 return unsafe_setindex! (parent (a), v, i)
433410 else
434411 return unsafe_setindex! (a, v, _to_cartesian (a, i)... )
435412 end
436413end
437- function unsafe_setindex! (a:: A , v, i:: CanonicalInt , ii:: Vararg{CanonicalInt } ) where {A}
414+ function unsafe_setindex! (a:: A , v, i:: IntType , ii:: Vararg{IntType } ) where {A}
438415 if IndexStyle (A) === IndexLinear ()
439416 return unsafe_setindex! (a, v, _to_linear (a, (i, ii... )))
440417 else
446423function unsafe_setindex! (A:: Array{T} , v) where {T}
447424 Base. arrayset (false , A, convert (T, v):: T , 1 )
448425end
449- function unsafe_setindex! (A:: Array{T} , v, i:: CanonicalInt ) where {T}
426+ function unsafe_setindex! (A:: Array{T} , v, i:: IntType ) where {T}
450427 return Base. arrayset (false , A, convert (T, v):: T , Int (i))
451428end
452429
0 commit comments