Skip to content

Commit 27a2266

Browse files
authored
Add copy(::KeySet) method and specialized Set{T}(::KeySet) method (#41836)
1 parent dd8d3c7 commit 27a2266

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

base/abstractdict.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ function iterate(v::Union{KeySet,ValueIterator}, state...)
6666
return (y[1][isa(v, KeySet) ? 1 : 2], y[2])
6767
end
6868

69+
copy(v::KeySet) = copymutable(v)
70+
6971
in(k, v::KeySet) = get(v.dict, k, secret_table_token) !== secret_table_token
7072

7173
"""

base/set.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
struct Set{T} <: AbstractSet{T}
44
dict::Dict{T,Nothing}
55

6-
Set{T}() where {T} = new(Dict{T,Nothing}())
7-
Set{T}(s::Set{T}) where {T} = new(Dict{T,Nothing}(s.dict))
6+
global _Set(dict::Dict{T,Nothing}) where {T} = new{T}(dict)
87
end
98

9+
Set{T}() where {T} = _Set(Dict{T,Nothing}())
10+
Set{T}(s::Set{T}) where {T} = _Set(Dict{T,Nothing}(s.dict))
1011
Set{T}(itr) where {T} = union!(Set{T}(), itr)
1112
Set() = Set{Any}()
1213

14+
function Set{T}(s::KeySet{T, <:Dict{T}}) where {T}
15+
d = s.dict
16+
slots = copy(d.slots)
17+
keys = copy(d.keys)
18+
vals = similar(d.vals, Nothing)
19+
_Set(Dict{T,Nothing}(slots, keys, vals, d.ndel, d.count, d.age, d.idxfloor, d.maxprobe))
20+
end
1321

1422
"""
1523
Set([itr])

test/sets.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ end
139139
@test !in(200,s)
140140
end
141141

142+
@testset "copy(::KeySet) (issue #41537)" begin
143+
@test union(keys(Dict(1=>2, 3=>4))) == copy(keys(Dict(1=>2, 3=>4))) == Set([1,3])
144+
end
145+
142146
@testset "copy!" begin
143147
for S = (Set, BitSet)
144148
s = S([1, 2])

0 commit comments

Comments
 (0)