Skip to content

Commit

Permalink
Add copy(::KeySet) method and specialized Set{T}(::KeySet) method (
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock authored Sep 1, 2021
1 parent dd8d3c7 commit 27a2266
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function iterate(v::Union{KeySet,ValueIterator}, state...)
return (y[1][isa(v, KeySet) ? 1 : 2], y[2])
end

copy(v::KeySet) = copymutable(v)

in(k, v::KeySet) = get(v.dict, k, secret_table_token) !== secret_table_token

"""
Expand Down
12 changes: 10 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
struct Set{T} <: AbstractSet{T}
dict::Dict{T,Nothing}

Set{T}() where {T} = new(Dict{T,Nothing}())
Set{T}(s::Set{T}) where {T} = new(Dict{T,Nothing}(s.dict))
global _Set(dict::Dict{T,Nothing}) where {T} = new{T}(dict)
end

Set{T}() where {T} = _Set(Dict{T,Nothing}())
Set{T}(s::Set{T}) where {T} = _Set(Dict{T,Nothing}(s.dict))
Set{T}(itr) where {T} = union!(Set{T}(), itr)
Set() = Set{Any}()

function Set{T}(s::KeySet{T, <:Dict{T}}) where {T}
d = s.dict
slots = copy(d.slots)
keys = copy(d.keys)
vals = similar(d.vals, Nothing)
_Set(Dict{T,Nothing}(slots, keys, vals, d.ndel, d.count, d.age, d.idxfloor, d.maxprobe))
end

"""
Set([itr])
Expand Down
4 changes: 4 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ end
@test !in(200,s)
end

@testset "copy(::KeySet) (issue #41537)" begin
@test union(keys(Dict(1=>2, 3=>4))) == copy(keys(Dict(1=>2, 3=>4))) == Set([1,3])
end

@testset "copy!" begin
for S = (Set, BitSet)
s = S([1, 2])
Expand Down

0 comments on commit 27a2266

Please sign in to comment.