@@ -306,6 +306,14 @@ if VERSION < v"1.6"
306
306
end
307
307
end
308
308
309
+ # Utils to translate a function to the parent while preserving offsets
310
+ unwrap (x) = x, identity
311
+ unwrap (x:: OffsetArray ) = parent (x), data -> OffsetArray (data, x. offsets)
312
+ function parent_call (f, x)
313
+ parent, wrap_offset = unwrap (x)
314
+ wrap_offset (f (parent))
315
+ end
316
+
309
317
Base. similar (A:: OffsetArray , :: Type{T} , dims:: Dims ) where T =
310
318
similar (parent (A), T, dims)
311
319
function Base. similar (A:: AbstractArray , :: Type{T} , shape:: Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}} ) where T
@@ -385,7 +393,7 @@ parentindex(r::IdOffsetRange, i) = i - r.offset
385
393
end
386
394
387
395
@propagate_inbounds Base. getindex (A:: OffsetArray{<:Any,N} , c:: Vararg{Colon,N} ) where N =
388
- OffsetArray (A . parent[ c... ] , A. offsets )
396
+ parent_call (x -> getindex (x, c... ) , A)
389
397
390
398
# With one Colon we use linear indexing.
391
399
# In this case we may forward the index to the parent, as the information about the axes is lost
417
425
end
418
426
419
427
Base. in (x, A:: OffsetArray ) = in (x, parent (A))
420
- Base. copy (A:: OffsetArray ) = OffsetArray (copy (A . parent) , A. offsets )
428
+ Base. copy (A:: OffsetArray ) = parent_call (copy, A)
421
429
422
430
Base. strides (A:: OffsetArray ) = strides (parent (A))
423
431
Base. elsize (:: Type{OffsetArray{T,N,A}} ) where {T,N,A} = Base. elsize (A)
@@ -512,6 +520,10 @@ for OR in [:IIUR, :IdOffsetRange]
512
520
end
513
521
end
514
522
523
+ # eltype conversion
524
+ # This may use specialized map methods for the parent
525
+ Base. map (:: Type{T} , O:: OffsetArray ) where {T} = parent_call (x -> map (T, x), O)
526
+
515
527
# mapreduce is faster with an IdOffsetRange than with an OffsetUnitRange
516
528
# We therefore convert OffsetUnitRanges to IdOffsetRanges with the same values and axes
517
529
function Base. mapreduce (f, op, As:: OffsetUnitRange{<:Integer} ...; kw... )
692
704
# Adapt allows for automatic conversion of CPU OffsetArrays to GPU OffsetArrays
693
705
# #
694
706
import Adapt
695
- Adapt. adapt_structure (to, x :: OffsetArray ) = OffsetArray ( Adapt. adapt (to, parent (x)), x . offsets )
707
+ Adapt. adapt_structure (to, O :: OffsetArray ) = parent_call (x -> Adapt. adapt (to, x), O )
696
708
697
709
if Base. VERSION >= v " 1.4.2"
698
710
include (" precompile.jl" )
0 commit comments