@@ -16,20 +16,6 @@ const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
16
16
17
17
haskey (d:: Associative , k) = in (k, keys (d))
18
18
19
- function in (p:: Pair , a:: Associative , valcmp= (== ))
20
- v = get (a,p[1 ],secret_table_token)
21
- if v != = secret_table_token
22
- valcmp (v, p[2 ]) && return true
23
- end
24
- return false
25
- end
26
-
27
- function in (p, a:: Associative )
28
- error (""" Associative collections only contain Pairs;
29
- Either look for e.g. A=>B instead, or use the `keys` or `values`
30
- function if you are looking for a key or value respectively.""" )
31
- end
32
-
33
19
function summary (t:: Associative )
34
20
n = length (t)
35
21
return string (typeof (t), " with " , n, (n== 1 ? " entry" : " entries" ))
@@ -44,26 +30,38 @@ struct ValueIterator{T<:Associative}
44
30
dict:: T
45
31
end
46
32
47
- summary (iter:: T ) where {T<: Union{KeySet,ValueIterator} } =
33
+ struct PairIterator{T<: Associative }
34
+ dict:: T
35
+ end
36
+
37
+ function in (p:: Pair , a:: PairIterator , valcmp= (== ))
38
+ v = get (a,p[1 ],secret_table_token)
39
+ if v != = secret_table_token
40
+ valcmp (v, p[2 ]) && return true
41
+ end
42
+ return false
43
+ end
44
+
45
+ summary (iter:: T ) where {T<: Union{KeySet,ValueIterator,PairIterator} } =
48
46
string (T. name, " for a " , summary (iter. dict))
49
47
50
- show (io:: IO , iter:: Union{KeySet,ValueIterator} ) = show (io, collect (iter))
48
+ show (io:: IO , iter:: Union{KeySet,ValueIterator,PairIterator } ) = show (io, collect (iter))
51
49
52
- length (v:: Union{KeySet,ValueIterator} ) = length (v. dict)
53
- isempty (v:: Union{KeySet,ValueIterator} ) = isempty (v. dict)
54
- _tt2 (:: Type{Pair{A,B }} ) where {A,B } = B
55
- eltype (:: Type{ValueIterator {D}} ) where {D} = _tt2 ( eltype (D) )
50
+ length (v:: Union{KeySet,ValueIterator,PairIterator } ) = length (v. dict)
51
+ isempty (v:: Union{KeySet,ValueIterator,PairIterator } ) = isempty (v. dict)
52
+ eltype (:: Type{ValueIterator{D }} ) where {D } = valuetype (D)
53
+ eltype (:: Type{PairIterator {D}} ) where {D} = pairtype (D )
56
54
57
- start (v:: Union{KeySet,ValueIterator} ) = start (v. dict)
58
- done (v:: Union{KeySet,ValueIterator} , state) = done (v. dict, state)
55
+ start (v:: Union{KeySet,ValueIterator,PairIterator } ) = start (v. dict)
56
+ done (v:: Union{KeySet,ValueIterator,PairIterator } , state) = done (v. dict, state)
59
57
60
58
function next (v:: KeySet , state)
61
- n = next (v. dict, state)
59
+ n = next (PairIterator ( v. dict) , state)
62
60
n[1 ][1 ], n[2 ]
63
61
end
64
62
65
63
function next (v:: ValueIterator , state)
66
- n = next (v. dict, state)
64
+ n = next (PairIterator ( v. dict) , state)
67
65
n[1 ][2 ], n[2 ]
68
66
end
69
67
@@ -136,7 +134,12 @@ This includes arrays, where the keys are the array indices.
136
134
"""
137
135
pairs (collection) = Generator (=> , keys (collection), values (collection))
138
136
139
- pairs (a:: Associative ) = a
137
+ pairs (a:: Associative ) = PairIterator (a)
138
+
139
+ function next (a:: Associative , i)
140
+ stacktrace ()
141
+ next (PairIterator (a), i)
142
+ end
140
143
141
144
"""
142
145
empty(a::Associative, [index_type=keytype(a)], [value_type=valtype(a)])
@@ -155,7 +158,7 @@ empty(a::Associative, ::Type{V}) where {V} = empty(a, keytype(a), V) # Note: thi
155
158
156
159
function copy (a:: Associative )
157
160
b = empty (a)
158
- for (k,v) in a
161
+ for (k,v) in pairs (a)
159
162
b[k] = v
160
163
end
161
164
return b
@@ -184,7 +187,7 @@ Dict{Int64,Int64} with 3 entries:
184
187
"""
185
188
function merge! (d:: Associative , others:: Associative... )
186
189
for other in others
187
- for (k,v) in other
190
+ for (k,v) in pairs ( other)
188
191
d[k] = v
189
192
end
190
193
end
@@ -223,7 +226,7 @@ Dict{Int64,Int64} with 3 entries:
223
226
"""
224
227
function merge! (combine:: Function , d:: Associative , others:: Associative... )
225
228
for other in others
226
- for (k,v) in other
229
+ for (k,v) in pairs ( other)
227
230
d[k] = haskey (d, k) ? combine (d[k], v) : v
228
231
end
229
232
end
@@ -250,9 +253,9 @@ julia> keytype(Dict(Int32(1) => "foo"))
250
253
Int32
251
254
```
252
255
"""
253
- keytype (:: Type{Associative{K,V}} ) where {K,V} = K
256
+ keytype (:: Type{Associative{K, V}} ) where {K, V} = K
254
257
keytype (a:: Associative ) = keytype (typeof (a))
255
- keytype (:: Type{A} ) where {A<: Associative } = keytype (supertype (A))
258
+ keytype (:: Type{A} ) where {A <: Associative } = keytype (supertype (A))
256
259
257
260
"""
258
261
valtype(type)
@@ -265,10 +268,25 @@ julia> valtype(Dict(Int32(1) => "foo"))
265
268
String
266
269
```
267
270
"""
268
- valtype (:: Type{Associative{K,V}} ) where {K,V} = V
269
- valtype (:: Type{A} ) where {A<: Associative } = valtype (supertype (A))
271
+ valtype (:: Type{Associative{K, V}} ) where {K, V} = V
272
+ valtype (:: Type{A} ) where {A <: Associative } = valtype (supertype (A))
270
273
valtype (a:: Associative ) = valtype (typeof (a))
271
274
275
+ """
276
+ pairtype(type)
277
+
278
+ Get the `Pair` type of an associative collection type. Behaves similarly to [`eltype`](@ref).
279
+
280
+ # Examples
281
+ ```jldoctest
282
+ julia> pairtype(Dict(Int32(1) => "foo"))
283
+ Pair{Int32,String}
284
+ ```
285
+ """
286
+ pairtype (:: Type{Associative{K, V}} ) where {K, V} = Pair{K, V}
287
+ pairtype (:: Type{A} ) where {A <: Associative } = pairtype (supertype (A))
288
+ pairtype (a:: Associative ) = pairtype (typeof (a))
289
+
272
290
"""
273
291
merge(d::Associative, others::Associative...)
274
292
@@ -368,7 +386,7 @@ Dict{Int64,String} with 2 entries:
368
386
function filter! (f, d:: Associative )
369
387
badkeys = Vector {keytype(d)} ()
370
388
try
371
- for pair in d
389
+ for pair in pairs (d)
372
390
# don't delete!(d, k) here, since associative types
373
391
# may not support mutation during iteration
374
392
f (pair) || push! (badkeys, pair. first)
384
402
385
403
function filter_in_one_pass! (f, d:: Associative )
386
404
try
387
- for pair in d
405
+ for pair in pairs (d)
388
406
if ! f (pair)
389
407
delete! (d, pair. first)
390
408
end
@@ -399,7 +417,7 @@ function filter!_dict_deprecation(e, f, d::Associative)
399
417
if isa (e, MethodError) && e. f === f
400
418
depwarn (" In `filter!(f, dict)`, `f` is now passed a single pair instead of two arguments." , :filter! )
401
419
badkeys = Vector {keytype(d)} ()
402
- for (k,v) in d
420
+ for (k,v) in pairs (d)
403
421
# don't delete!(d, k) here, since associative types
404
422
# may not support mutation during iteration
405
423
f (k, v) || push! (badkeys, k)
@@ -435,15 +453,15 @@ function filter(f, d::Associative)
435
453
# don't just do filter!(f, copy(d)): avoid making a whole copy of d
436
454
df = empty (d)
437
455
try
438
- for pair in d
456
+ for pair in pairs (d)
439
457
if f (pair)
440
458
df[pair. first] = pair. second
441
459
end
442
460
end
443
461
catch e
444
462
if isa (e, MethodError) && e. f === f
445
463
depwarn (" In `filter(f, dict)`, `f` is now passed a single pair instead of two arguments." , :filter )
446
- for (k, v) in d
464
+ for (k, v) in pairs (d)
447
465
if f (k, v)
448
466
df[k] = v
449
467
end
@@ -455,16 +473,14 @@ function filter(f, d::Associative)
455
473
return df
456
474
end
457
475
458
- eltype (:: Type{Associative{K,V}} ) where {K,V} = Pair{K,V}
459
-
460
476
function isequal (l:: Associative , r:: Associative )
461
477
l === r && return true
462
478
if isa (l,ObjectIdDict) != isa (r,ObjectIdDict)
463
479
return false
464
480
end
465
481
if length (l) != length (r) return false end
466
- for pair in l
467
- if ! in (pair, r , isequal)
482
+ for pair in pairs (l)
483
+ if ! in (pair, pairs (r) , isequal)
468
484
return false
469
485
end
470
486
end
@@ -477,8 +493,8 @@ function ==(l::Associative, r::Associative)
477
493
return false
478
494
end
479
495
if length (l) != length (r) return false end
480
- for pair in l
481
- if ! in (pair, r , == )
496
+ for pair in pairs (l)
497
+ if ! in (pair, pairs (r) , == )
482
498
return false
483
499
end
484
500
end
488
504
const hasha_seed = UInt === UInt64 ? 0x6d35bb51952d5539 : 0x952d5539
489
505
function hash (a:: Associative , h:: UInt )
490
506
hv = hasha_seed
491
- for (k,v) in a
507
+ for (k,v) in pairs (a)
492
508
hv ⊻= hash (k, hash (v))
493
509
end
494
510
hash (hv, h)
@@ -599,11 +615,11 @@ _oidd_nextind(a, i) = reinterpret(Int,ccall(:jl_eqtable_nextind, Csize_t, (Any,
599
615
600
616
start (t:: ObjectIdDict ) = _oidd_nextind (t. ht, 0 )
601
617
done (t:: ObjectIdDict , i) = (i == - 1 )
602
- next (t:: ObjectIdDict , i) = (Pair {Any,Any} (t. ht[i+ 1 ],t. ht[i+ 2 ]), _oidd_nextind (t. ht, i+ 2 ))
618
+ next (t:: PairIterator{<: ObjectIdDict} , i) = (Pair {Any,Any} (t. dict . ht[i+ 1 ],t. dict . ht[i+ 2 ]), _oidd_nextind (t. dict . ht, i+ 2 ))
603
619
604
620
function length (d:: ObjectIdDict )
605
621
n = 0
606
- for pair in d
622
+ for pair in pairs (d)
607
623
n+= 1
608
624
end
609
625
n
0 commit comments