Skip to content

Commit c63ee65

Browse files
rfourquetJeffBezanson
authored andcommitted
move Future.copy! to Base (fix #29173) (#29178)
1 parent ba054ea commit c63ee65

File tree

9 files changed

+66
-64
lines changed

9 files changed

+66
-64
lines changed

base/abstractarray.jl

+18
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,24 @@ empty(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}()
638638
emptymutable(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}()
639639
emptymutable(itr, ::Type{U}) where {U} = Vector{U}()
640640

641+
"""
642+
copy!(dst, src) -> dst
643+
644+
In-place [`copy`](@ref) of `src` into `dst`, discarding any pre-existing
645+
elements in `dst`.
646+
If `dst` and `src` are of the same type, `dst == src` should hold after
647+
the call. If `dst` and `src` are multidimensional arrays, they must have
648+
equal [`axes`](@ref).
649+
See also [`copyto!`](@ref).
650+
"""
651+
copy!(dst::AbstractVector, src::AbstractVector) = append!(empty!(dst), src)
652+
653+
function copy!(dst::AbstractArray, src::AbstractArray)
654+
axes(dst) == axes(src) || throw(ArgumentError(
655+
"arrays must have the same axes for copy! (consider using `copyto!`)"))
656+
copyto!(dst, src)
657+
end
658+
641659
## from general iterable to any array
642660

643661
function copyto!(dest::AbstractArray, src)

base/abstractdict.jl

+2-7
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,8 @@ The default is to return an empty `Dict`.
148148
empty(a::AbstractDict) = empty(a, keytype(a), valtype(a))
149149
empty(a::AbstractDict, ::Type{V}) where {V} = empty(a, keytype(a), V) # Note: this is the form which makes sense for `Vector`.
150150

151-
function copy(a::AbstractDict)
152-
b = empty(a)
153-
for (k,v) in a
154-
b[k] = v
155-
end
156-
return b
157-
end
151+
copy(a::AbstractDict) = merge!(empty(a), a)
152+
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
158153

159154
"""
160155
merge!(d::AbstractDict, others::AbstractDict...)

base/abstractset.jl

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
eltype(::Type{<:AbstractSet{T}}) where {T} = @isdefined(T) ? T : Any
44
sizehint!(s::AbstractSet, n) = nothing
55

6+
copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
7+
68
"""
79
union(s, itrs...)
810
∪(s, itrs...)

base/bitset.jl

-8
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ emptymutable(s::BitSet, ::Type{Int}=Int) = BitSet()
4646
copy(s1::BitSet) = copy!(BitSet(), s1)
4747
copymutable(s::BitSet) = copy(s)
4848

49-
"""
50-
copy!(dst, src)
51-
52-
In-place [`copy`](@ref) of `src` into `dst`. After the call to `copy!`,
53-
`dst` must be left equal to `src`, otherwise an error is thrown; this
54-
function appropriately resizes `dst` if necessary.
55-
See also [`copyto!`](@ref).
56-
"""
5749
function copy!(dest::BitSet, src::BitSet)
5850
resize!(dest.bits, length(src.bits))
5951
copyto!(dest.bits, src.bits)

stdlib/Future/src/Future.jl

+7-12
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@ using Random
88

99
## copy!
1010

11+
# This has now been moved to Base (#29178), and should be deprecated in the
12+
# next "deprecation phase".
13+
1114
"""
1215
Future.copy!(dst, src) -> dst
1316
1417
Copy `src` into `dst`.
15-
For collections of the same type, copy the elements of `src` into `dst`,
16-
discarding any pre-existing elements in `dst`.
17-
Usually, `dst == src` holds after the call.
18+
This function has now been moved into `Base`, consider using `copy!(dst, src)` instead.
1819
"""
19-
copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
20-
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
21-
copy!(dst::AbstractVector, src::AbstractVector) = append!(empty!(dst), src)
22-
23-
function copy!(dst::AbstractArray, src::AbstractArray)
24-
axes(dst) == axes(src) || throw(ArgumentError(
25-
"arrays must have the same axes for copy! (consider using `copyto!`)"))
26-
copyto!(dst, src)
27-
end
20+
copy!(dst::AbstractSet, src::AbstractSet) = Base.copy!(dst, src)
21+
copy!(dst::AbstractDict, src::AbstractDict) = Base.copy!(dst, src)
22+
copy!(dst::AbstractArray, src::AbstractArray) = Base.copy!(dst, src)
2823

2924

3025
## randjump

stdlib/Future/test/runtests.jl

-37
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,3 @@
22

33
using Test
44
using Future
5-
using SparseArrays
6-
7-
@testset "Future.copy! for AbstractSet" begin
8-
for S = (Set, BitSet)
9-
s = S([1, 2])
10-
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
11-
@test s === Future.copy!(s, Set(a)) == S(a)
12-
@test s === Future.copy!(s, BitSet(a)) == S(a)
13-
end
14-
end
15-
end
16-
17-
18-
@testset "Future.copy! for AbstractDict" begin
19-
s = Dict(1=>2, 2=>3)
20-
for a = ([3=>4], [0x3=>0x4], [3=>4, 5=>6, 7=>8], Pair{UInt,UInt}[3=>4, 5=>6, 7=>8])
21-
@test s === Future.copy!(s, Dict(a)) == Dict(a)
22-
if length(a) == 1 # current limitation of Base.ImmutableDict
23-
@test s === Future.copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
24-
end
25-
end
26-
end
27-
28-
@testset "Future.copy! for AbstractVector" begin
29-
s = Vector([1, 2])
30-
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
31-
@test s === Future.copy!(s, Vector(a)) == Vector(a)
32-
@test s === Future.copy!(s, SparseVector(a)) == Vector(a)
33-
end
34-
end
35-
36-
@testset "Future.copy! for AbstractArray" begin
37-
@test_throws ArgumentError Future.copy!(zeros(2, 3), zeros(3, 2))
38-
s = zeros(2, 2)
39-
@test s === Future.copy!(s, fill(1, 2, 2)) == fill(1, 2, 2)
40-
@test s === Future.copy!(s, fill(1.0, 2, 2)) == fill(1.0, 2, 2)
41-
end

test/abstractarray.jl

+16
Original file line numberDiff line numberDiff line change
@@ -916,3 +916,19 @@ end
916916
@test hcat(1:2, fill(1, (2,1))) == hcat([1:2;], fill(1, (2,1))) == reshape([1,2,1,1],2,2)
917917
@test [(1:3) (4:6); fill(1, (3,2))] == reshape([1,2,3,1,1,1,4,5,6,1,1,1], 6,2)
918918
end
919+
920+
@testset "copy!" begin
921+
@testset "AbstractVector" begin
922+
s = Vector([1, 2])
923+
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
924+
@test s === copy!(s, Vector(a)) == Vector(a)
925+
@test s === copy!(s, SparseVector(a)) == Vector(a)
926+
end
927+
end
928+
@testset "AbstractArray" begin
929+
@test_throws ArgumentError copy!(zeros(2, 3), zeros(3, 2))
930+
s = zeros(2, 2)
931+
@test s === copy!(s, fill(1, 2, 2)) == fill(1, 2, 2)
932+
@test s === copy!(s, fill(1.0, 2, 2)) == fill(1.0, 2, 2)
933+
end
934+
end

test/dict.jl

+10
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,13 @@ end
990990
@test String(take!(buf)) ==
991991
"Base.KeySet for a Base.ImmutableDict{$Int,$Int} with 3 entries. Keys:\n 5\n"
992992
end
993+
994+
@testset "copy!" begin
995+
s = Dict(1=>2, 2=>3)
996+
for a = ([3=>4], [0x3=>0x4], [3=>4, 5=>6, 7=>8], Pair{UInt,UInt}[3=>4, 5=>6, 7=>8])
997+
@test s === copy!(s, Dict(a)) == Dict(a)
998+
if length(a) == 1 # current limitation of Base.ImmutableDict
999+
@test s === copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
1000+
end
1001+
end
1002+
end

test/sets.jl

+11
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ end
136136
@test !in(100,c)
137137
@test !in(200,s)
138138
end
139+
140+
@testset "copy!" begin
141+
for S = (Set, BitSet)
142+
s = S([1, 2])
143+
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
144+
@test s === copy!(s, Set(a)) == S(a)
145+
@test s === copy!(s, BitSet(a)) == S(a)
146+
end
147+
end
148+
end
149+
139150
@testset "sizehint, empty" begin
140151
s = Set([1])
141152
@test isequal(sizehint!(s, 10), Set([1]))

0 commit comments

Comments
 (0)