1
1
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
-
25
2
"""
26
3
ArrayInterface.to_indices(A, I::Tuple) -> Tuple
27
4
@@ -162,16 +139,16 @@ to_index(::LinearIndices, i::AbstractArray{Bool}) = LogicalIndex{Int}(i)
162
139
@inline to_index (x, i:: NDIndex ) = getfield (i, 1 )
163
140
@inline to_index (x, i:: AbstractArray{<:AbstractCartesianIndex} ) = i
164
141
@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))
166
143
end
167
144
@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))
169
146
end
170
147
@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)
172
149
end
173
150
@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)
175
152
end
176
153
# integer indexing
177
154
to_index (x, i:: AbstractArray{<:Integer} ) = i
@@ -232,7 +209,7 @@ indices calling [`to_axis`](@ref).
232
209
end
233
210
end
234
211
# 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))
236
213
to_axes (A, a:: Tuple , i:: Tuple{I,Vararg{Any}} ) where {I} = _to_axes (StaticInt (ndims_index (I)), A, a, i)
237
214
function _to_axes (:: StaticInt{1} , A, axs:: Tuple , inds:: Tuple )
238
215
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}
309
286
end
310
287
311
288
# 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}
313
290
if IndexStyle (A) === IndexLinear ()
314
291
is_forwarding_wrapper (A) || throw (MethodError (unsafe_getindex, (A, i)))
315
292
return unsafe_getindex (parent (a), i)
316
293
else
317
294
return unsafe_getindex (a, _to_cartesian (a, i)... )
318
295
end
319
296
end
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}
321
298
if IndexStyle (A) === IndexLinear ()
322
299
return unsafe_getindex (a, _to_linear (a, (i, ii... )))
323
300
else
@@ -329,24 +306,24 @@ end
329
306
unsafe_getindex (a, i:: Vararg{Any} ) = unsafe_get_collection (a, i)
330
307
331
308
unsafe_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 } )
334
311
unsafe_getindex (A, _to_linear (A, (i, ii... )))
335
312
end
336
313
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 } ) =
340
317
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])
342
319
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 } )
345
322
@inbounds (parent (A)[_to_linear (A, (i, ii... ))])
346
323
end
347
324
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... ])
350
327
351
328
# This is based on Base._unsafe_getindex from https://github.com/JuliaLang/julia/blob/c5ede45829bf8eb09f2145bfd6f089459d77b2b1/base/multidimensional.jl#L755.
352
329
#=
@@ -364,17 +341,17 @@ function unsafe_get_collection(A, inds)
364
341
end
365
342
return dest
366
343
end
367
- _ints2range (x:: CanonicalInt ) = x: x
344
+ _ints2range (x:: IntType ) = x: x
368
345
_ints2range (x:: AbstractRange ) = x
369
346
# apply _ints2range to front N elements
370
347
_ints2range_front (:: Val{N} , ind, inds... ) where {N} =
371
348
(_ints2range (ind), _ints2range_front (Val (N - 1 ), inds... )... )
372
349
_ints2range_front (:: Val{0} , ind, inds... ) = ()
373
350
_ints2range_front (:: Val{0} ) = ()
374
351
# get output shape with given indices
375
- _output_shape (:: CanonicalInt , inds... ) = _output_shape (inds... )
352
+ _output_shape (:: IntType , inds... ) = _output_shape (inds... )
376
353
_output_shape (ind:: AbstractRange , inds... ) = (Base. length (ind), _output_shape (inds... )... )
377
- _output_shape (:: CanonicalInt ) = ()
354
+ _output_shape (:: IntType ) = ()
378
355
_output_shape (x:: AbstractRange ) = (Base. length (x),)
379
356
@inline function unsafe_get_collection (A:: CartesianIndices{N} , inds) where {N}
380
357
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}
426
403
return unsafe_setindex! (parent (a), v)
427
404
end
428
405
# 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}
430
407
if IndexStyle (A) === IndexLinear ()
431
408
is_forwarding_wrapper (A) || throw (MethodError (unsafe_setindex!, (A, v, i)))
432
409
return unsafe_setindex! (parent (a), v, i)
433
410
else
434
411
return unsafe_setindex! (a, v, _to_cartesian (a, i)... )
435
412
end
436
413
end
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}
438
415
if IndexStyle (A) === IndexLinear ()
439
416
return unsafe_setindex! (a, v, _to_linear (a, (i, ii... )))
440
417
else
446
423
function unsafe_setindex! (A:: Array{T} , v) where {T}
447
424
Base. arrayset (false , A, convert (T, v):: T , 1 )
448
425
end
449
- function unsafe_setindex! (A:: Array{T} , v, i:: CanonicalInt ) where {T}
426
+ function unsafe_setindex! (A:: Array{T} , v, i:: IntType ) where {T}
450
427
return Base. arrayset (false , A, convert (T, v):: T , Int (i))
451
428
end
452
429
0 commit comments