Skip to content

Commit

Permalink
faster copy! between arrays of different types (ref JuliaLang#11004)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Apr 26, 2015
1 parent 252bb1b commit 102e840
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ end

squeeze(A::AbstractArray, dim::Integer) = squeeze(A, (Int(dim),))

## from general iterable to any array

function copy!(dest::AbstractArray, src)
i = 1
for x in src
Expand All @@ -220,7 +222,6 @@ function copy!(dest::AbstractArray, src)
return dest
end

# copy with minimal requirements on src
# if src is not an AbstractArray, moving to the offset might be O(n)
function copy!(dest::AbstractArray, doffs::Integer, src)
doffs < 1 && throw(BoundsError())
Expand All @@ -247,7 +248,7 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer)
dn = done(src, st)
dn && throw(BoundsError())
i, dmax = doffs, length(dest)
@inbounds while !dn
@inbounds while !dn
i > dmax && throw(BoundsError())
val, st = next(src, st)
dest[i] = val
Expand Down Expand Up @@ -280,7 +281,20 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Inte
return dest
end

# if src is an AbstractArray and a source offset is passed, use indexing
## copy between abstract arrays - generally more efficient
## since a single index variable can be used.

function copy!(dest::AbstractArray, src::AbstractArray)
n = length(src)
if n > length(dest)
throw(BoundsError())
end
@inbounds for i = 1:n
dest[i] = src[i]
end
return dest
end

function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray)
copy!(dest, doffs, src, 1, length(src))
end
Expand Down

0 comments on commit 102e840

Please sign in to comment.