Skip to content

Commit d10fd33

Browse files
committed
copy returns Fill
1 parent 1edf1bd commit d10fd33

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/FillArrays.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module FillArrays
33
using LinearAlgebra, SparseArrays
44
import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
55
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
6-
any, all, axes, isone, iterate, unique, allunique, permutedims, inv
6+
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
7+
copy
78

89
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag
910

@@ -74,6 +75,8 @@ convert(::Type{AbstractFill}, F::AbstractFill) = F
7475
convert(::Type{AbstractFill{T}}, F::AbstractFill) where T = convert(AbstractArray{T}, F)
7576
convert(::Type{AbstractFill{T,N}}, F::AbstractFill) where {T,N} = convert(AbstractArray{T,N}, F)
7677

78+
copy(F::Fill) = Fill(copy(F.value), F.axes)
79+
7780
""" Throws an error if `arr` does not contain one and only one unique value. """
7881
function unique_value(arr::AbstractArray)
7982
if isempty(arr) error("Cannot convert empty array to Fill") end
@@ -174,6 +177,8 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
174177
convert(::Type{AbstractArray{T}}, F::$Typ) where T = AbstractArray{T}(F)
175178
convert(::Type{AbstractArray{T,N}}, F::$Typ) where {T,N} = AbstractArray{T,N}(F)
176179

180+
copy(F::$Typ) = F
181+
177182
getindex(F::$Typ{T,0}) where T = getindex_value(F)
178183
function getindex(F::$Typ{T}, kj::Vararg{AbstractVector{II},N}) where {T,II<:Integer,N}
179184
checkbounds(F, kj...)

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ import FillArrays: AbstractFill, RectDiagonal
129129
@test Zeros{Bool}(5) + x x
130130
@test -x Fill(-1,5)
131131
end
132+
133+
@testset "copy should return Fill" begin
134+
x = Fill(1.0,10)
135+
@test copy(x) x
136+
x = Zeros(10)
137+
@test copy(x) x
138+
x = Fill([1.,2.],10)
139+
@test copy(x) == x
140+
@test copy(x) !== x
141+
@test copy(x) isa Fill
142+
end
132143
end
133144

134145
@testset "RectDiagonal" begin

0 commit comments

Comments
 (0)