From 7f403fe83d966acb93bf66649671058ecd23d8bd Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Thu, 21 Dec 2017 16:58:01 +0100 Subject: [PATCH] change similar(::AbstractSet) to empty(::AbstractSet) --- base/array.jl | 10 +++++----- base/bitset.jl | 1 - base/deprecated.jl | 4 ++++ base/set.jl | 6 +++--- test/bitset.jl | 8 ++++---- test/sets.jl | 8 ++++---- 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/base/array.jl b/base/array.jl index 7cef7f61c21ee..9ba859b55396b 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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 @@ -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) @@ -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. diff --git a/base/bitset.jl b/base/bitset.jl index 8f271b6f6d09c..e78ccd5129285 100644 --- a/base/bitset.jl +++ b/base/bitset.jl @@ -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() diff --git a/base/deprecated.jl b/base/deprecated.jl index 811a091966221..35cc4c82d0d0b 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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 diff --git a/base/set.jl b/base/set.jl index 8c1b03c893d7e..675d1d5946803 100644 --- a/base/set.jl +++ b/base/set.jl @@ -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) @@ -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: diff --git a/test/bitset.jl b/test/bitset.jl index 3c7eeffc60997..2ca0da9b74214 100644 --- a/test/bitset.jl +++ b/test/bitset.jl @@ -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 @@ -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 diff --git a/test/sets.jl b/test/sets.jl index 66dcd34664c8a..aa50bb073d733 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -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