Skip to content

Commit

Permalink
improve constructor of Set from Set (#24346)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored and stevengj committed Oct 30, 2017
1 parent 03045ca commit 4aa4652
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mutable struct Set{T} <: AbstractSet{T}
dict::Dict{T,Void}

Set{T}() where {T} = new(Dict{T,Void}())
Set{T}(s::Set{T}) where {T} = new(Dict{T,Void}(s.dict))
Set{T}(itr) where {T} = union!(new(Dict{T,Void}()), itr)
end
Set() = Set{Any}()
Expand Down Expand Up @@ -54,7 +55,7 @@ end

delete!(s::Set, x) = (delete!(s.dict, x); s)

copy(s::Set) = union!(similar(s), s)
copy(s::Set{T}) where T = Set{T}(s)

sizehint!(s::Set, newsz) = (sizehint!(s.dict, newsz); s)
empty!(s::Set) = (empty!(s.dict); s)
Expand Down
13 changes: 13 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ using Main.TestHelpers.OAs
@test isa(Set(f17741(x) for x = 1:3), Set{Int})
@test isa(Set(f17741(x) for x = -1:1), Set{Integer})
end
let s1 = Set(["foo", "bar"]), s2 = Set(s1)
@test s1 == s2
x = pop!(s1)
@test s1 != s2
@test !(x in s1)
@test x in s2
push!(s1, "baz")
push!(s2, "baz2")
@test "baz" in s1
@test !("baz" in s2)
@test !("baz2" in s1)
@test "baz2" in s2
end
end

@testset "hash" begin
Expand Down

0 comments on commit 4aa4652

Please sign in to comment.