Skip to content

Commit

Permalink
move Future.copy! to Base (fix #29173)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Sep 14, 2018
1 parent 26b6a58 commit fbca002
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 73 deletions.
18 changes: 18 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,24 @@ empty(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}()
emptymutable(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}()
emptymutable(itr, ::Type{U}) where {U} = Vector{U}()

"""
copy!(dst, src) -> dst
In-place [`copy`](@ref) of `src` into `dst`, discarding any pre-existing
elements in `dst`.
If `dst` and `src` are of the same type, `dst == src` should hold after
the call. If `dst` and `src` are multidimensional arrays, they must have
equal [`axes`](@ref).
See also [`copyto!`](@ref).
"""
copy!(dst::AbstractVector, src::AbstractVector) = append!(empty!(dst), src)

function copy!(dst::AbstractArray, src::AbstractArray)
axes(dst) == axes(src) || throw(ArgumentError(
"arrays must have the same axes for copy! (consider using `copyto!`)"))
copyto!(dst, src)
end

## from general iterable to any array

function copyto!(dest::AbstractArray, src)
Expand Down
9 changes: 2 additions & 7 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,8 @@ The default is to return an empty `Dict`.
empty(a::AbstractDict) = empty(a, keytype(a), valtype(a))
empty(a::AbstractDict, ::Type{V}) where {V} = empty(a, keytype(a), V) # Note: this is the form which makes sense for `Vector`.

function copy(a::AbstractDict)
b = empty(a)
for (k,v) in a
b[k] = v
end
return b
end
copy(a::AbstractDict) = merge!(empty(a), a)
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)

"""
merge!(d::AbstractDict, others::AbstractDict...)
Expand Down
2 changes: 2 additions & 0 deletions base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
eltype(::Type{<:AbstractSet{T}}) where {T} = @isdefined(T) ? T : Any
sizehint!(s::AbstractSet, n) = nothing

copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)

"""
union(s, itrs...)
∪(s, itrs...)
Expand Down
8 changes: 0 additions & 8 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ emptymutable(s::BitSet, ::Type{Int}=Int) = BitSet()
copy(s1::BitSet) = copy!(BitSet(), s1)
copymutable(s::BitSet) = copy(s)

"""
copy!(dst, src)
In-place [`copy`](@ref) of `src` into `dst`. After the call to `copy!`,
`dst` must be left equal to `src`, otherwise an error is thrown; this
function appropriately resizes `dst` if necessary.
See also [`copyto!`](@ref).
"""
function copy!(dest::BitSet, src::BitSet)
resize!(dest.bits, length(src.bits))
copyto!(dest.bits, src.bits)
Expand Down
21 changes: 0 additions & 21 deletions stdlib/Future/src/Future.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ module Future

using Random

## copy!

"""
Future.copy!(dst, src) -> dst
Copy `src` into `dst`.
For collections of the same type, copy the elements of `src` into `dst`,
discarding any pre-existing elements in `dst`.
Usually, `dst == src` holds after the call.
"""
copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
copy!(dst::AbstractVector, src::AbstractVector) = append!(empty!(dst), src)

function copy!(dst::AbstractArray, src::AbstractArray)
axes(dst) == axes(src) || throw(ArgumentError(
"arrays must have the same axes for copy! (consider using `copyto!`)"))
copyto!(dst, src)
end


## randjump

"""
Expand Down
37 changes: 0 additions & 37 deletions stdlib/Future/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,3 @@

using Test
using Future
using SparseArrays

@testset "Future.copy! for AbstractSet" begin
for S = (Set, BitSet)
s = S([1, 2])
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
@test s === Future.copy!(s, Set(a)) == S(a)
@test s === Future.copy!(s, BitSet(a)) == S(a)
end
end
end


@testset "Future.copy! for AbstractDict" begin
s = Dict(1=>2, 2=>3)
for a = ([3=>4], [0x3=>0x4], [3=>4, 5=>6, 7=>8], Pair{UInt,UInt}[3=>4, 5=>6, 7=>8])
@test s === Future.copy!(s, Dict(a)) == Dict(a)
if length(a) == 1 # current limitation of Base.ImmutableDict
@test s === Future.copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
end
end
end

@testset "Future.copy! for AbstractVector" begin
s = Vector([1, 2])
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
@test s === Future.copy!(s, Vector(a)) == Vector(a)
@test s === Future.copy!(s, SparseVector(a)) == Vector(a)
end
end

@testset "Future.copy! for AbstractArray" begin
@test_throws ArgumentError Future.copy!(zeros(2, 3), zeros(3, 2))
s = zeros(2, 2)
@test s === Future.copy!(s, fill(1, 2, 2)) == fill(1, 2, 2)
@test s === Future.copy!(s, fill(1.0, 2, 2)) == fill(1.0, 2, 2)
end
16 changes: 16 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,19 @@ end
@test hcat(1:2, fill(1, (2,1))) == hcat([1:2;], fill(1, (2,1))) == reshape([1,2,1,1],2,2)
@test [(1:3) (4:6); fill(1, (3,2))] == reshape([1,2,3,1,1,1,4,5,6,1,1,1], 6,2)
end

@testset "copy!" begin
@testset "AbstractVector" begin
s = Vector([1, 2])
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
@test s === copy!(s, Vector(a)) == Vector(a)
@test s === copy!(s, SparseVector(a)) == Vector(a)
end
end
@testset "AbstractArray" begin
@test_throws ArgumentError copy!(zeros(2, 3), zeros(3, 2))
s = zeros(2, 2)
@test s === copy!(s, fill(1, 2, 2)) == fill(1, 2, 2)
@test s === copy!(s, fill(1.0, 2, 2)) == fill(1.0, 2, 2)
end
end
10 changes: 10 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,13 @@ end
@test String(take!(buf)) ==
"Base.KeySet for a Base.ImmutableDict{$Int,$Int} with 3 entries. Keys:\n 5\n"
end

@testset "copy!" begin
s = Dict(1=>2, 2=>3)
for a = ([3=>4], [0x3=>0x4], [3=>4, 5=>6, 7=>8], Pair{UInt,UInt}[3=>4, 5=>6, 7=>8])
@test s === copy!(s, Dict(a)) == Dict(a)
if length(a) == 1 # current limitation of Base.ImmutableDict
@test s === copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
end
end
end
11 changes: 11 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ end
@test !in(100,c)
@test !in(200,s)
end

@testset "copy!" begin
for S = (Set, BitSet)
s = S([1, 2])
for a = ([1], UInt[1], [3, 4, 5], UInt[3, 4, 5])
@test s === copy!(s, Set(a)) == S(a)
@test s === copy!(s, BitSet(a)) == S(a)
end
end
end

@testset "sizehint, empty" begin
s = Set([1])
@test isequal(sizehint!(s, 10), Set([1]))
Expand Down

0 comments on commit fbca002

Please sign in to comment.