Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move Future.copy! to Base (fix #29173) #29178

Merged
merged 3 commits into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 7 additions & 12 deletions stdlib/Future/src/Future.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ using Random

## copy!

# This has now been moved to Base (#29178), and should be deprecated in the
# next "deprecation phase".

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant these just call the Base methods now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a very good idea!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before pushing again and triggering CI, how about this new docstring:

"""
    Future.copy!(dst, src) -> dst

Copy `src` into `dst`.
This function has now been moved into `Base`, consider using `copy!(dst, src)` instead.
"""

"""
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.
This function has now been moved into `Base`, consider using `copy!(dst, src)` instead.
"""
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
copy!(dst::AbstractSet, src::AbstractSet) = Base.copy!(dst, src)
copy!(dst::AbstractDict, src::AbstractDict) = Base.copy!(dst, src)
copy!(dst::AbstractArray, src::AbstractArray) = Base.copy!(dst, src)


## 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