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

change similar(::AbstractSet) to empty(::AbstractSet) #25224

Merged
merged 1 commit into from
Dec 24, 2017
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
10 changes: 5 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
end

function grow_to!(dest, itr)
out = grow_to!(similar(dest,Union{}), itr, start(itr))
out = grow_to!(empty(dest, Union{}), itr, start(itr))
return isempty(out) ? dest : out
end

Expand All @@ -595,12 +595,12 @@ function grow_to!(dest, itr, st)
if S === T || S <: T
push!(dest, el::T)
else
new = similar(dest, typejoin(T, S))
new = sizehint!(empty(dest, typejoin(T, S)), length(dest))
if new isa AbstractSet
# TODO: merge back these two branches when copy! is re-enabled for sets
# TODO: merge back these two branches when copy! is re-enabled for sets/vectors
union!(new, dest)
else
copyto!(new, dest)
append!(new, dest)
end
push!(new, el)
return grow_to!(new, itr, st)
Expand Down Expand Up @@ -2214,7 +2214,7 @@ function filter!(f, a::AbstractVector)
return a
end

filter(f, a::Vector) = mapfilter(f, push!, a, similar(a, 0))
filter(f, a::Vector) = mapfilter(f, push!, a, empty(a))

# set-like operators for vectors
# These are moderately efficient, preserve order, and remove dupes.
Expand Down
1 change: 0 additions & 1 deletion base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ BitSet(itr) = union!(BitSet(), itr)
@inline intoffset(s::BitSet) = s.offset << 6

eltype(::Type{BitSet}) = Int
similar(s::BitSet) = BitSet()

empty(s::BitSet, ::Type{Int}=Int) = BitSet()
emptymutable(s::BitSet, ::Type{Int}=Int) = BitSet()
Expand Down
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,10 @@ info(err::Exception; prefix="ERROR: ", kw...) =
@deprecate similar(a::AbstractDict) empty(a)
@deprecate similar(a::AbstractDict, ::Type{Pair{K,V}}) where {K, V} empty(a, K, V)

# 25224
@deprecate similar(s::AbstractSet) empty(s)
@deprecate similar(s::AbstractSet, ::Type{T}) where {T} empty(s, T)

# PR #24594
@eval LibGit2 begin
@deprecate AbstractCredentials AbstractCredential false
Expand Down
6 changes: 3 additions & 3 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ function Set(g::Generator)
return Set{T}(g)
end

similar(s::Set{T}, ::Type{U}=T) where {T,U} = Set{U}()

empty(s::Set{T}, ::Type{U}=T) where {T,U} = Set{U}()

# return an empty set with eltype T, which is mutable (can be grown)
# by default, a Set is returned
emptymutable(s::AbstractSet{T}, ::Type{U}=T) where {T,U} = Set{U}()

_similar_for(c::AbstractSet, T, itr, isz) = empty(c, T)

function show(io::IO, s::Set)
print(io, "Set(")
show_vector(io, s)
Expand Down Expand Up @@ -519,7 +519,7 @@ allunique(::Set) = true

allunique(r::AbstractRange{T}) where {T} = (step(r) != zero(T)) || (length(r) <= 1)

filter(pred, s::AbstractSet) = mapfilter(pred, push!, s, similar(s))
filter(pred, s::AbstractSet) = mapfilter(pred, push!, s, emptymutable(s))
filter!(f, s::Set) = unsafe_filter!(f, s)

# it must be safe to delete the current element while iterating over s:
Expand Down
8 changes: 4 additions & 4 deletions test/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
@test length(data_out) === length(data_in)
end

@testset "eltype, similar" begin
@testset "eltype, empty" begin
@test eltype(BitSet()) === Int
@test eltype(BitSet) === Int
@test isequal(similar(BitSet([1,2,3])), BitSet())
@test isequal(empty(BitSet([1,2,3])), BitSet())
end

@testset "show" begin
Expand Down Expand Up @@ -99,9 +99,9 @@ end
@test_throws MethodError symdiff!(BitSet([1, 2]), [[1]]) # should not return BitSet([2])
end

@testset "copy, copy!, similar" begin
@testset "copy, copy!, empty" begin
s1 = BitSet([1,2,3])
s2 = similar(s1)
s2 = empty(s1)
copy!(s2, s1)
s3 = copy(s2)
@test s3 == s2 == s1
Expand Down
8 changes: 4 additions & 4 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ end
@test !isequal(Set{Any}([1,2,3,4]), Set{Int}([1,2,3]))
@test !isequal(Set{Int}([1,2,3,4]), Set{Any}([1,2,3]))
end
@testset "eltype, similar" begin
s1 = similar(Set([1,"hello"]))
@testset "eltype, empty" begin
s1 = empty(Set([1,"hello"]))
@test isequal(s1, Set())
@test ===(eltype(s1), Any)
s2 = similar(Set{Float32}([2.0f0,3.0f0,4.0f0]))
s2 = empty(Set{Float32}([2.0f0,3.0f0,4.0f0]))
@test isequal(s2, Set())
@test ===(eltype(s2), Float32)
s3 = similar(Set([1,"hello"]),Float32)
s3 = empty(Set([1,"hello"]),Float32)
@test isequal(s3, Set())
@test ===(eltype(s3), Float32)
end
Expand Down