Skip to content

Commit 41f551c

Browse files
committed
Extend similar for AsEltype and simplify code
1 parent 62c0057 commit 41f551c

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ This page describes the most important changes in `TypeUtils`. The format is bas
44
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to
55
[Semantic Versioning](https://semver.org/spec).
66

7+
## Unreleased
8+
9+
### Added
10+
11+
- Extend `similar` for the type of arrays returned by `as_eltype`.
12+
713
## Version 1.6.0 (2025-04-15)
814

915
### Added

src/TypeUtils.jl

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,43 +1040,36 @@ as_eltype(::Type{T}, A::AbstractArray) where {T} = AsEltype{T}(A)
10401040

10411041
struct 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)
10431045
end
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.
10511050
for func in (:axes, :length, :size)
10521051
@eval Base.$func(A::AsEltype) = $func(parent(A))
10531052
end
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
10731068
end
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

Comments
 (0)