@@ -320,47 +320,46 @@ end
320
320
321
321
# But SubArrays with fast linear indexing pre-compute a stride and offset
322
322
FastSubArray{T,N,P,I} = SubArray{T,N,P,I,true }
323
+ # We define a convenience functions to compute the shifted parent index
324
+ # This differs from reindex as this accepts the view directly, instead of its indices
325
+ @inline _reindexlinear (V:: FastSubArray , i:: Int ) = V. offset1 + V. stride1* i
326
+ @inline _reindexlinear (V:: FastSubArray , i:: AbstractUnitRange{Int} ) = V. offset1 .+ V. stride1 .* i
327
+
323
328
function getindex (V:: FastSubArray , i:: Int )
324
329
@inline
325
330
@boundscheck checkbounds (V, i)
326
- @inbounds r = V. parent[V . offset1 + V . stride1 * i ]
331
+ @inbounds r = V. parent[_reindexlinear (V, i) ]
327
332
r
328
333
end
329
- # We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange,
330
- # or if all the indices are scalars, i.e. the view is for a single value only
331
- FastContiguousSubArray{T,N,P,I<: Union {Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}},
332
- Tuple{Vararg{ScalarIndex}}}} = SubArray{T,N,P,I,true }
333
- function getindex (V:: FastContiguousSubArray , i:: Int )
334
+
335
+ # For vector views with linear indexing, we disambiguate to favor the stride/offset
336
+ # computation as that'll generally be faster than (or just as fast as) re-indexing into a range.
337
+ function getindex (V:: FastSubArray{<:Any, 1} , i:: Int )
334
338
@inline
335
339
@boundscheck checkbounds (V, i)
336
- @inbounds r = V. parent[V . offset1 + i ]
340
+ @inbounds r = V. parent[_reindexlinear (V, i) ]
337
341
r
338
342
end
343
+
344
+ # We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange,
345
+ # or if all the indices are scalars, i.e. the view is for a single value only
346
+ FastContiguousSubArray{T,N,P,I<: Union {Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}},
347
+ Tuple{Vararg{ScalarIndex}}}} = SubArray{T,N,P,I,true }
348
+
349
+ @inline _reindexlinear (V:: FastContiguousSubArray , i:: Int ) = V. offset1 + i
350
+ @inline _reindexlinear (V:: FastContiguousSubArray , i:: AbstractUnitRange{Int} ) = V. offset1 .+ i
351
+
339
352
# parents of FastContiguousSubArrays may support fast indexing with AbstractUnitRanges,
340
353
# so we may just forward the indexing to the parent
341
354
# This may only be done for non-offset ranges, as the result would otherwise have offset axes
342
355
const OneBasedRanges = Union{OneTo{Int}, UnitRange{Int}, Slice{OneTo{Int}}, IdentityUnitRange{OneTo{Int}}}
343
356
function getindex (V:: FastContiguousSubArray , i:: OneBasedRanges )
344
357
@inline
345
358
@boundscheck checkbounds (V, i)
346
- @inbounds r = V. parent[V . offset1 .+ i ]
359
+ @inbounds r = V. parent[_reindexlinear (V, i) ]
347
360
r
348
361
end
349
362
350
- # For vector views with linear indexing, we disambiguate to favor the stride/offset
351
- # computation as that'll generally be faster than (or just as fast as) re-indexing into a range.
352
- function getindex (V:: FastSubArray{<:Any, 1} , i:: Int )
353
- @inline
354
- @boundscheck checkbounds (V, i)
355
- @inbounds r = V. parent[V. offset1 + V. stride1* i]
356
- r
357
- end
358
- function getindex (V:: FastContiguousSubArray{<:Any, 1} , i:: Int )
359
- @inline
360
- @boundscheck checkbounds (V, i)
361
- @inbounds r = V. parent[V. offset1 + i]
362
- r
363
- end
364
363
@inline getindex (V:: FastContiguousSubArray , i:: Colon ) = getindex (V, to_indices (V, (:,))... )
365
364
366
365
# Indexed assignment follows the same pattern as `getindex` above
@@ -373,40 +372,23 @@ end
373
372
function setindex! (V:: FastSubArray , x, i:: Int )
374
373
@inline
375
374
@boundscheck checkbounds (V, i)
376
- @inbounds V. parent[V . offset1 + V . stride1 * i ] = x
375
+ @inbounds V. parent[_reindexlinear (V, i) ] = x
377
376
V
378
377
end
379
- function setindex! (V:: FastContiguousSubArray , x, i:: Int )
378
+ function setindex! (V:: FastSubArray{<:Any, 1} , x, i:: Int )
380
379
@inline
381
380
@boundscheck checkbounds (V, i)
382
- @inbounds V. parent[V . offset1 + i ] = x
381
+ @inbounds V. parent[_reindexlinear (V, i) ] = x
383
382
V
384
383
end
384
+
385
385
function setindex! (V:: FastSubArray , x, i:: AbstractUnitRange{Int} )
386
386
@inline
387
387
@boundscheck checkbounds (V, i)
388
- @inbounds V. parent[V. offset1 .+ V. stride1 .* i] = x
389
- V
390
- end
391
- function setindex! (V:: FastContiguousSubArray , x, i:: AbstractUnitRange{Int} )
392
- @inline
393
- @boundscheck checkbounds (V, i)
394
- @inbounds V. parent[V. offset1 .+ i] = x
388
+ @inbounds V. parent[_reindexlinear (V, i)] = x
395
389
V
396
390
end
397
391
398
- function setindex! (V:: FastSubArray{<:Any, 1} , x, i:: Int )
399
- @inline
400
- @boundscheck checkbounds (V, i)
401
- @inbounds V. parent[V. offset1 + V. stride1* i] = x
402
- V
403
- end
404
- function setindex! (V:: FastContiguousSubArray{<:Any, 1} , x, i:: Int )
405
- @inline
406
- @boundscheck checkbounds (V, i)
407
- @inbounds V. parent[V. offset1 + i] = x
408
- V
409
- end
410
392
@inline setindex! (V:: FastSubArray , x, i:: Colon ) = setindex! (V, x, to_indices (V, (i,))... )
411
393
412
394
function isassigned (V:: SubArray{T,N} , I:: Vararg{Int,N} ) where {T,N}
@@ -418,25 +400,13 @@ end
418
400
function isassigned (V:: FastSubArray , i:: Int )
419
401
@inline
420
402
@boundscheck checkbounds (Bool, V, i) || return false
421
- @inbounds r = isassigned (V. parent, V. offset1 + V. stride1* i)
422
- r
423
- end
424
- function isassigned (V:: FastContiguousSubArray , i:: Int )
425
- @inline
426
- @boundscheck checkbounds (Bool, V, i) || return false
427
- @inbounds r = isassigned (V. parent, V. offset1 + i)
403
+ @inbounds r = isassigned (V. parent, _reindexlinear (V, i))
428
404
r
429
405
end
430
406
function isassigned (V:: FastSubArray{<:Any, 1} , i:: Int )
431
407
@inline
432
408
@boundscheck checkbounds (Bool, V, i) || return false
433
- @inbounds r = isassigned (V. parent, V. offset1 + V. stride1* i)
434
- r
435
- end
436
- function isassigned (V:: FastContiguousSubArray{<:Any, 1} , i:: Int )
437
- @inline
438
- @boundscheck checkbounds (Bool, V, i) || return false
439
- @inbounds r = isassigned (V. parent, V. offset1 + i)
409
+ @inbounds r = isassigned (V. parent, _reindexlinear (V, i))
440
410
r
441
411
end
442
412
0 commit comments