Skip to content

Commit

Permalink
Merge pull request #19 from putianyi889/version-0.0.4.7
Browse files Browse the repository at this point in the history
support CartesianIndices
  • Loading branch information
putianyi889 authored Mar 16, 2024
2 parents 4de713c + 96f3460 commit ed73c99
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/EltypeExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
"""
elconvert(T, A)
Similar to `convert(T, A)`, but `T` refers to the eltype.
Similar to `convert(T, A)`, but `T` refers to the eltype. See also [`_to_eltype`](@ref).
# Examples
```jldoctest; setup = :(using EltypeExtensions: elconvert)
Expand All @@ -40,17 +40,26 @@ elconvert(::Type{T}, A::Tuple) where T = convert.(T, A)
"""
_to_eltype(T, S)
Convert type `S` to have the `eltype` of `T`.
Convert type `S` to have the `eltype` of `T`. See also [`elconvert`](@ref).
"""
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : MethodError(_to_eltype, T, S)
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : eltype(S) == T ? S : MethodError(_to_eltype, T, S)
_to_eltype(::Type{T}, ::Type{Array{S,N}}) where {T,S,N} = Array{T,N}
_to_eltype(::Type{T}, ::Type{<:Set}) where T = Set{T}
for TYP in (Adjoint, Diagonal, Hermitian, Symmetric, SymTridiagonal, Transpose)
@eval _to_eltype(::Type{T}, ::Type{$TYP}) where T = $TYP{T}
@eval _to_eltype(::Type{T}, ::Type{$TYP{S}}) where {T,S} = $TYP{T}
@eval _to_eltype(::Type{T}, ::Type{$TYP{S,M}}) where {T,S,M} = $TYP{T,_to_eltype(T,M)}
@eval elconvert(::Type{T}, A::S) where {T,S<:$TYP} = convert(_to_eltype(T, S), A)
end
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Integer = UnitRange{T}

@static if VERSION >= v"1.6"
_to_eltype(::Type{CartesianIndex{N}}, ::Type{CartesianIndices{N,R}}) where {N, R<:Tuple{Vararg{OrdinalRange{Int64, Int64}, N}}} = CartesianIndices{N,R}
else
_to_eltype(::Type{CartesianIndex{N}}, ::Type{CartesianIndices{N,R}}) where {N, R<:Tuple{Vararg{AbstractUnitRange{Int64},N}}} = CartesianIndices{N,R}
end
_to_eltype(::Type{T}, ::Type{<:CartesianIndices}) where T = Array{T}

@static if VERSION >= v"1.7"
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Real = StepRangeLen{T,Base.TwicePrecision{T},Base.TwicePrecision{T},Int}
else
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ end
r = 1:5
testelconvert(Int8, r)
testelconvert(Float64, r)

inds = CartesianIndex(1,1):CartesianIndex(3,3)
testelconvert(CartesianIndex{2}, inds)
testelconvert(Tuple, inds)
end

@testset "bugs" begin
Expand Down

0 comments on commit ed73c99

Please sign in to comment.