|
6 | 6 | WeakKeyDict([itr]) |
7 | 7 |
|
8 | 8 | `WeakKeyDict()` constructs a hash table where the keys are weak |
9 | | -references to objects, and thus may be garbage collected even when |
| 9 | +references to objects which may be garbage collected even when |
10 | 10 | referenced in a hash table. |
11 | 11 |
|
12 | 12 | See [`Dict`](@ref) for further help. Note, unlike [`Dict`](@ref), |
@@ -112,24 +112,12 @@ getindex(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> getindex(wkh.ht, key), |
112 | 112 | isempty(wkh::WeakKeyDict) = isempty(wkh.ht) |
113 | 113 | length(t::WeakKeyDict) = length(t.ht) |
114 | 114 |
|
115 | | -function iterate(t::WeakKeyDict{K,V}) where V where K |
116 | | - gc_token = Ref{Bool}(false) # no keys will be deleted via finalizers until this token is gc'd |
117 | | - finalizer(gc_token) do r |
118 | | - if r[] |
119 | | - r[] = false |
120 | | - unlock(t.lock) |
121 | | - end |
122 | | - end |
123 | | - s = lock(t.lock) |
124 | | - iterate(t, (gc_token,)) |
125 | | -end |
126 | | -function iterate(t::WeakKeyDict{K,V}, state) where V where K |
127 | | - gc_token = first(state) |
128 | | - y = iterate(t.ht, tail(state)...) |
| 115 | +function iterate(t::WeakKeyDict{K,V}, state...) where {K, V} |
| 116 | + y = lock(() -> iterate(t.ht, state...), t) |
129 | 117 | y === nothing && return nothing |
130 | | - wkv, i = y |
| 118 | + wkv, newstate = y |
131 | 119 | kv = Pair{K,V}(wkv[1].value::K, wkv[2]) |
132 | | - return (kv, (gc_token, i)) |
| 120 | + return (kv, newstate) |
133 | 121 | end |
134 | 122 |
|
135 | 123 | filter!(f, d::WeakKeyDict) = filter_in_one_pass!(f, d) |
0 commit comments