-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
copy!
between arrays of different types (ref #11004)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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()) | ||
|
@@ -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 | ||
|
@@ -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 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JeffBezanson
Author
Member
|
||
dest[i] = src[i] | ||
end | ||
return dest | ||
end | ||
|
||
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray) | ||
copy!(dest, doffs, src, 1, length(src)) | ||
end | ||
|
Ahem. Get with the times, dude 😄. #10858
(You need to check that the sizes are the same first, however, and can use this as a fallback if they don't match. If they do, use the
eachindex(dest,src)
version.)