Skip to content

Commit ebc2493

Browse files
authored
change similar(::AbstractSet) to empty(::AbstractSet) (#25224)
1 parent fa2ae8c commit ebc2493

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

base/array.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
583583
end
584584

585585
function grow_to!(dest, itr)
586-
out = grow_to!(similar(dest,Union{}), itr, start(itr))
586+
out = grow_to!(empty(dest, Union{}), itr, start(itr))
587587
return isempty(out) ? dest : out
588588
end
589589

@@ -595,12 +595,12 @@ function grow_to!(dest, itr, st)
595595
if S === T || S <: T
596596
push!(dest, el::T)
597597
else
598-
new = similar(dest, typejoin(T, S))
598+
new = sizehint!(empty(dest, typejoin(T, S)), length(dest))
599599
if new isa AbstractSet
600-
# TODO: merge back these two branches when copy! is re-enabled for sets
600+
# TODO: merge back these two branches when copy! is re-enabled for sets/vectors
601601
union!(new, dest)
602602
else
603-
copyto!(new, dest)
603+
append!(new, dest)
604604
end
605605
push!(new, el)
606606
return grow_to!(new, itr, st)
@@ -2214,7 +2214,7 @@ function filter!(f, a::AbstractVector)
22142214
return a
22152215
end
22162216

2217-
filter(f, a::Vector) = mapfilter(f, push!, a, similar(a, 0))
2217+
filter(f, a::Vector) = mapfilter(f, push!, a, empty(a))
22182218

22192219
# set-like operators for vectors
22202220
# These are moderately efficient, preserve order, and remove dupes.

base/bitset.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ BitSet(itr) = union!(BitSet(), itr)
3131
@inline intoffset(s::BitSet) = s.offset << 6
3232

3333
eltype(::Type{BitSet}) = Int
34-
similar(s::BitSet) = BitSet()
3534

3635
empty(s::BitSet, ::Type{Int}=Int) = BitSet()
3736
emptymutable(s::BitSet, ::Type{Int}=Int) = BitSet()

base/deprecated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,10 @@ info(err::Exception; prefix="ERROR: ", kw...) =
33733373
@deprecate similar(a::AbstractDict) empty(a)
33743374
@deprecate similar(a::AbstractDict, ::Type{Pair{K,V}}) where {K, V} empty(a, K, V)
33753375

3376+
# 25224
3377+
@deprecate similar(s::AbstractSet) empty(s)
3378+
@deprecate similar(s::AbstractSet, ::Type{T}) where {T} empty(s, T)
3379+
33763380
# PR #24594
33773381
@eval LibGit2 begin
33783382
@deprecate AbstractCredentials AbstractCredential false

base/set.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ function Set(g::Generator)
2727
return Set{T}(g)
2828
end
2929

30-
similar(s::Set{T}, ::Type{U}=T) where {T,U} = Set{U}()
31-
3230
empty(s::Set{T}, ::Type{U}=T) where {T,U} = Set{U}()
3331

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

36+
_similar_for(c::AbstractSet, T, itr, isz) = empty(c, T)
37+
3838
function show(io::IO, s::Set)
3939
print(io, "Set(")
4040
show_vector(io, s)
@@ -519,7 +519,7 @@ allunique(::Set) = true
519519

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

522-
filter(pred, s::AbstractSet) = mapfilter(pred, push!, s, similar(s))
522+
filter(pred, s::AbstractSet) = mapfilter(pred, push!, s, emptymutable(s))
523523
filter!(f, s::Set) = unsafe_filter!(f, s)
524524

525525
# it must be safe to delete the current element while iterating over s:

test/bitset.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
@test length(data_out) === length(data_in)
1111
end
1212

13-
@testset "eltype, similar" begin
13+
@testset "eltype, empty" begin
1414
@test eltype(BitSet()) === Int
1515
@test eltype(BitSet) === Int
16-
@test isequal(similar(BitSet([1,2,3])), BitSet())
16+
@test isequal(empty(BitSet([1,2,3])), BitSet())
1717
end
1818

1919
@testset "show" begin
@@ -99,9 +99,9 @@ end
9999
@test_throws MethodError symdiff!(BitSet([1, 2]), [[1]]) # should not return BitSet([2])
100100
end
101101

102-
@testset "copy, copy!, similar" begin
102+
@testset "copy, copy!, empty" begin
103103
s1 = BitSet([1,2,3])
104-
s2 = similar(s1)
104+
s2 = empty(s1)
105105
copy!(s2, s1)
106106
s3 = copy(s2)
107107
@test s3 == s2 == s1

test/sets.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ end
6161
@test !isequal(Set{Any}([1,2,3,4]), Set{Int}([1,2,3]))
6262
@test !isequal(Set{Int}([1,2,3,4]), Set{Any}([1,2,3]))
6363
end
64-
@testset "eltype, similar" begin
65-
s1 = similar(Set([1,"hello"]))
64+
@testset "eltype, empty" begin
65+
s1 = empty(Set([1,"hello"]))
6666
@test isequal(s1, Set())
6767
@test ===(eltype(s1), Any)
68-
s2 = similar(Set{Float32}([2.0f0,3.0f0,4.0f0]))
68+
s2 = empty(Set{Float32}([2.0f0,3.0f0,4.0f0]))
6969
@test isequal(s2, Set())
7070
@test ===(eltype(s2), Float32)
71-
s3 = similar(Set([1,"hello"]),Float32)
71+
s3 = empty(Set([1,"hello"]),Float32)
7272
@test isequal(s3, Set())
7373
@test ===(eltype(s3), Float32)
7474
end

0 commit comments

Comments
 (0)