Skip to content

Commit

Permalink
combine _copy_oftype methods (#250)
Browse files Browse the repository at this point in the history
* combine _copy_oftype methods

* tests for copy
  • Loading branch information
jishnub authored May 16, 2023
1 parent 549e949 commit 61a1cf9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/fillbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,8 @@ function broadcasted(::DefaultArrayStyle{1}, ::typeof(*), a::AbstractRange{V}, b
return _range_convert(AbstractVector{promote_type(T,V)}, a)
end

_copy_oftype(A::AbstractArray{T,N}, ::Type{T}) where {T,N} = copy(A)
_copy_oftype(A::AbstractArray{T,N}, ::Type{S}) where {T,N,S} = convert(AbstractArray{S,N}, A)
_copy_oftype(A::AbstractRange{T}, ::Type{T}) where T = copy(A)
_copy_oftype(A::AbstractRange{T}, ::Type{S}) where {T,S} = map(S, A)
_copy_oftype(A::AbstractArray, ::Type{S}) where {S} = eltype(A) == S ? copy(A) : AbstractArray{S}(A)
_copy_oftype(A::AbstractRange, ::Type{S}) where {S} = map(S, A)

for op in (:+, :-)
@eval begin
Expand Down Expand Up @@ -254,4 +252,4 @@ broadcasted(::DefaultArrayStyle{N}, ::typeof(Base.literal_pow), ::Base.RefValue{
# supports structured broadcast
if isdefined(LinearAlgebra, :fzero)
LinearAlgebra.fzero(x::Zeros) = zero(eltype(x))
end
end
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,16 @@ end

@test copy(m) m
@test copy(D) D
@test FillArrays._copy_oftype(m, eltype(m)) m
@test FillArrays._copy_oftype(m, Int) Eye{Int}(10)
@test FillArrays._copy_oftype(D, eltype(D)) D
@test FillArrays._copy_oftype(D, Float64) Diagonal(Fill(2.0,10))

# test that _copy_oftype does, in fact, copy the array
D2 = Diagonal([1,1])
@test FillArrays._copy_oftype(D2, Float64) isa Diagonal{Float64}
@test FillArrays._copy_oftype(D2, eltype(D2)) == D2
@test FillArrays._copy_oftype(D2, eltype(D2)) !== D2
end

@testset "Issue #31" begin
Expand Down

2 comments on commit 61a1cf9

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/83737

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.1 -m "<description of version>" 61a1cf9932c7c30ce8d7ffffa111bedb6472566d
git push origin v1.0.1

Please sign in to comment.