Skip to content

Commit

Permalink
some fixes to fill! (#30879)
Browse files Browse the repository at this point in the history
- method for UInt8/Int8 did not check conversions
- there was a redundant method
- make some `fill!` callers easier to infer
  • Loading branch information
JeffBezanson committed Jan 29, 2019
1 parent 1d31fb9 commit 610880b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
29 changes: 14 additions & 15 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ copyto!(dest::Array{T}, src::Array{T}) where {T} = copyto!(dest, 1, src, 1, leng
function fill!(dest::Array{T}, x) where T
@_noinline_meta
xT = convert(T, x)
for i in 1:length(dest)
for i in eachindex(dest)
@inbounds dest[i] = xT
end
return dest
Expand Down Expand Up @@ -363,16 +363,7 @@ end
getindex(::Type{Any}) = Vector{Any}()

function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer)
ccall(:memset, Ptr{Cvoid}, (Ptr{Cvoid}, Cint, Csize_t), a, x, length(a))
return a
end

function fill!(a::Array{T}, x) where T<:Union{Integer,AbstractFloat}
@_noinline_meta
xT = convert(T, x)
for i in eachindex(a)
@inbounds a[i] = xT
end
ccall(:memset, Ptr{Cvoid}, (Ptr{Cvoid}, Cint, Csize_t), a, convert(eltype(a), x), length(a))
return a
end

Expand Down Expand Up @@ -401,8 +392,8 @@ dims)` will return an array filled with the result of evaluating `Foo()` once.
"""
fill(v, dims::DimOrInd...) = fill(v, dims)
fill(v, dims::NTuple{N, Union{Integer, OneTo}}) where {N} = fill(v, map(to_dim, dims))
fill(v, dims::NTuple{N, Integer}) where {N} = fill!(Array{typeof(v),N}(undef, dims), v)
fill(v, dims::Tuple{}) = fill!(Array{typeof(v),0}(undef, dims), v)
fill(v, dims::NTuple{N, Integer}) where {N} = (a=Array{typeof(v),N}(undef, dims); fill!(a, v); a)
fill(v, dims::Tuple{}) = (a=Array{typeof(v),0}(undef, dims); fill!(a, v); a)

"""
zeros([T=Float64,] dims...)
Expand Down Expand Up @@ -450,8 +441,16 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one))
$fname(::Type{T}, dims::DimOrInd...) where {T} = $fname(T, dims)
$fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims)
$fname(::Type{T}, dims::NTuple{N, Union{Integer, OneTo}}) where {T,N} = $fname(T, map(to_dim, dims))
$fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N} = fill!(Array{T,N}(undef, map(to_dim, dims)), $felt(T))
$fname(::Type{T}, dims::Tuple{}) where {T} = fill!(Array{T}(undef), $felt(T))
function $fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N}
a = Array{T,N}(undef, dims)
fill!(a, $felt(T))
return a
end
function $fname(::Type{T}, dims::Tuple{}) where {T}
a = Array{T}(undef)
fill!(a, $felt(T))
return a
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,12 @@ end
fill!(A, [1, 2])
@test A[1] == [1, 2]
@test A[1] === A[2]
# byte arrays
@test_throws InexactError fill!(UInt8[0], -1)
@test_throws InexactError fill!(UInt8[0], 300)
@test_throws InexactError fill!(Int8[0], 200)
@test fill!(UInt8[0,0], 200) == [200,200]
@test fill!(Int8[0,0], -2) == [-2,-2]
end

@testset "splice!" begin
Expand Down

0 comments on commit 610880b

Please sign in to comment.