@@ -1040,43 +1040,36 @@ as_eltype(::Type{T}, A::AbstractArray) where {T} = AsEltype{T}(A)
10401040
10411041struct AsEltype{T,N,L,A<: AbstractArray } <: AbstractArray{T,N}
10421042 parent:: A
1043+ AsEltype {T} (arr:: A ) where {T,N,A<: AbstractArray{<:Any,N} } =
1044+ new {T,N,IndexStyle(A)===IndexLinear(),A} (arr)
10431045end
10441046
1045- AsEltype {T} (arr:: A ) where {T,N,A<: AbstractArray{<:Any,N} } =
1046- AsEltype {T,N,IndexStyle(A)===IndexLinear(),A} (arr)
1047-
1048- Base. parent (A:: AsEltype ) = A. parent
1047+ Base. parent (A:: AsEltype ) = getfield (A, :parent )
10491048
10501049# Implement abstract array API for `AsEltype` objects.
10511050for func in (:axes , :length , :size )
10521051 @eval Base.$ func (A:: AsEltype ) = $ func (parent (A))
10531052end
1054- Base. IndexStyle (:: Type{<:AsEltype{<:Any,<:Any,true}} ) = IndexLinear ()
1055- Base. IndexStyle (:: Type{<:AsEltype{<:Any,<:Any,false}} ) = IndexCartesian ()
1056-
1057- @inline function Base. getindex (A:: AsEltype{T,N,true} , i:: Int ) where {T,N}
1058- @boundscheck checkbounds (A, i)
1059- @inbounds r = getindex (parent (A), i)
1060- return as (T, r)
1061- end
1062-
1063- @inline function Base. getindex (A:: AsEltype{T,N,false} , I:: Vararg{Int,N} ) where {T,N}
1064- @boundscheck checkbounds (A, I... )
1065- @inbounds r = getindex (parent (A), I... )
1066- return as (T, r)
1067- end
1068-
1069- @inline function Base. setindex! (A:: AsEltype{T,N,true} , x, i:: Int ) where {T,N}
1070- @boundscheck checkbounds (A, i)
1071- @inbounds setindex! (parent (A), x, i)
1072- return A
1053+ for (L, S, Idecl, Icall) in ((false , :IndexCartesian , :(I:: Vararg{Int,N} ), :(I... )),
1054+ (true , :IndexLinear , :(i:: Int ), :(i)))
1055+ @eval begin
1056+ Base. IndexStyle (:: Type{<:AsEltype{T,N,$L}} ) where {T,N} = $ S ()
1057+ @inline function Base. getindex (A:: AsEltype{T,N,$L} , $ Idecl) where {T,N}
1058+ @boundscheck checkbounds (A, $ Icall)
1059+ r = @inbounds getindex (parent (A), $ Icall)
1060+ return as (T, r)
1061+ end
1062+ @inline function Base. setindex! (A:: AsEltype{T,N,$L} , x, $ Idecl) where {T,N}
1063+ @boundscheck checkbounds (A, $ Icall)
1064+ @inbounds setindex! (parent (A), x, $ Icall)
1065+ return A
1066+ end
1067+ end
10731068end
10741069
1075- @inline function Base. setindex! (A:: AsEltype{T,N,false} , x, I:: Vararg{Int,N} ) where {T,N}
1076- @boundscheck checkbounds (A, I... )
1077- @inbounds setindex! (parent (A), x, I... )
1078- return A
1079- end
1070+ Base. similar (A:: AsEltype , :: Type{T} ) where {T} = similar (parent (A), T)
1071+ Base. similar (A:: AsEltype , :: Type{T} , shape:: Union{Dims,ArrayAxes} ) where {T} =
1072+ similar (parent (A), T, shape)
10801073
10811074"""
10821075 destructure(obj) -> vals::Tuple
0 commit comments