Skip to content

Commit

Permalink
Convert fill!(AbstractArray, x) to Cartesian
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jan 13, 2014
1 parent c6bb39c commit da8f1dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 0 additions & 7 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,6 @@ function squeeze(A::AbstractArray, dims)
reshape(A, d)
end

function fill!(A::AbstractArray, x)
for i = 1:length(A)
A[i] = x
end
return A
end

function copy!(dest::AbstractArray, src)
i = 1
for x in src
Expand Down
8 changes: 8 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ eval(ngenerate(:N, :(setindex!{T}(s::SubArray{T,N}, v, ind::Integer)), gen_setin


### from abstractarray.jl

@ngenerate N function fill!{T,N}(A::AbstractArray{T,N}, x)
@nloops N i A begin
@inbounds (@nref N A i) = x

This comment has been minimized.

Copy link
@ivarne

ivarne Jan 14, 2014

Member

Are you including the performance benefit @inbounds in the Cartesian benchmark comparison?

This comment has been minimized.

Copy link
@timholy

timholy Jan 14, 2014

Author Member

SubArrays don't actually do any bounds-checking, so I think in this case it's irrelevant.

end
return A
end

## code generator for specializing on the number of dimensions ##

#otherbodies are the bodies that reside between loops, if its a 2 dimension array.
Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ end

# fill
@test fill!(Array(Float64,1),-0.0)[1] === -0.0
A = ones(3,3)
S = sub(A, 2, 1:3)
fill!(S, 2)
S = sub(A, 1:2, 3)
fill!(S, 3)
@test A == [1 1 3; 2 2 3; 1 1 1]

# splice!
for idx in {1, 2, 5, 9, 10, 1:0, 2:1, 1:1, 2:2, 1:2, 2:4, 9:8, 10:9, 9:9, 10:10,
Expand Down

0 comments on commit da8f1dd

Please sign in to comment.