1
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2
2
3
- # generic operations on associative collections
3
+ # generic operations on dictionaries
4
4
5
5
"""
6
6
KeyError(key)
7
7
8
- An indexing operation into an `Associative ` (`Dict`) or `Set` like object tried to access or
8
+ An indexing operation into an `AbstractDict ` (`Dict`) or `Set` like object tried to access or
9
9
delete a non-existent element.
10
10
"""
11
11
struct KeyError <: Exception
14
14
15
15
const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
16
16
17
- haskey (d:: Associative , k) = in (k, keys (d))
17
+ haskey (d:: AbstractDict , k) = in (k, keys (d))
18
18
19
- function in (p:: Pair , a:: Associative , valcmp= (== ))
19
+ function in (p:: Pair , a:: AbstractDict , valcmp= (== ))
20
20
v = get (a,p[1 ],secret_table_token)
21
21
if v != = secret_table_token
22
22
valcmp (v, p[2 ]) && return true
23
23
end
24
24
return false
25
25
end
26
26
27
- function in (p, a:: Associative )
28
- error (""" Associative collections only contain Pairs;
27
+ function in (p, a:: AbstractDict )
28
+ error (""" AbstractDict collections only contain Pairs;
29
29
Either look for e.g. A=>B instead, or use the `keys` or `values`
30
30
function if you are looking for a key or value respectively.""" )
31
31
end
32
32
33
- function summary (t:: Associative )
33
+ function summary (t:: AbstractDict )
34
34
n = length (t)
35
35
return string (typeof (t), " with " , n, (n== 1 ? " entry" : " entries" ))
36
36
end
37
37
38
- struct KeySet{K, T <: Associative {K} } <: AbstractSet{K}
38
+ struct KeySet{K, T <: AbstractDict {K} } <: AbstractSet{K}
39
39
dict:: T
40
40
end
41
- KeySet (dict:: Associative ) = KeySet {keytype(dict), typeof(dict)} (dict)
41
+ KeySet (dict:: AbstractDict ) = KeySet {keytype(dict), typeof(dict)} (dict)
42
42
43
- struct ValueIterator{T<: Associative }
43
+ struct ValueIterator{T<: AbstractDict }
44
44
dict:: T
45
45
end
46
46
@@ -78,9 +78,9 @@ return an iterator over the keys.
78
78
function keys end
79
79
80
80
"""
81
- keys(a::Associative )
81
+ keys(a::AbstractDict )
82
82
83
- Return an iterator over all keys in an associative collection .
83
+ Return an iterator over all keys in a dictionary .
84
84
`collect(keys(a))` returns an array of keys.
85
85
Since the keys are stored internally in a hash table,
86
86
the order in which they are returned may vary.
@@ -100,10 +100,10 @@ julia> collect(keys(a))
100
100
'a'
101
101
```
102
102
"""
103
- keys (a:: Associative ) = KeySet (a)
103
+ keys (a:: AbstractDict ) = KeySet (a)
104
104
105
105
"""
106
- values(a::Associative )
106
+ values(a::AbstractDict )
107
107
108
108
Return an iterator over all values in a collection.
109
109
`collect(values(a))` returns an array of values.
@@ -125,7 +125,7 @@ julia> collect(values(a))
125
125
2
126
126
```
127
127
"""
128
- values (a:: Associative ) = ValueIterator (a)
128
+ values (a:: AbstractDict ) = ValueIterator (a)
129
129
130
130
"""
131
131
pairs(collection)
@@ -136,24 +136,24 @@ This includes arrays, where the keys are the array indices.
136
136
"""
137
137
pairs (collection) = Generator (=> , keys (collection), values (collection))
138
138
139
- pairs (a:: Associative ) = a
139
+ pairs (a:: AbstractDict ) = a
140
140
141
141
"""
142
- empty(a::Associative , [index_type=keytype(a)], [value_type=valtype(a)])
142
+ empty(a::AbstractDict , [index_type=keytype(a)], [value_type=valtype(a)])
143
143
144
- Create an empty `Associative ` container which can accept indices of type `index_type` and
144
+ Create an empty `AbstractDict ` container which can accept indices of type `index_type` and
145
145
values of type `value_type`. The second and third arguments are optional and default to the
146
146
input's `keytype` and `valtype`, respectively. (If only one of the two types is specified,
147
147
it is assumed to be the `value_type`, and the `index_type` we default to `keytype(a)`).
148
148
149
- Custom `Associative ` subtypes may choose which specific associative type is best suited to
149
+ Custom `AbstractDict ` subtypes may choose which specific dictionary type is best suited to
150
150
return for the given index and value types, by specializing on the three-argument signature.
151
151
The default is to return an empty `Dict`.
152
152
"""
153
- empty (a:: Associative ) = empty (a, keytype (a), valtype (a))
154
- empty (a:: Associative , :: Type{V} ) where {V} = empty (a, keytype (a), V) # Note: this is the form which makes sense for `Vector`.
153
+ empty (a:: AbstractDict ) = empty (a, keytype (a), valtype (a))
154
+ empty (a:: AbstractDict , :: Type{V} ) where {V} = empty (a, keytype (a), V) # Note: this is the form which makes sense for `Vector`.
155
155
156
- function copy (a:: Associative )
156
+ function copy (a:: AbstractDict )
157
157
b = empty (a)
158
158
for (k,v) in a
159
159
b[k] = v
@@ -162,7 +162,7 @@ function copy(a::Associative)
162
162
end
163
163
164
164
"""
165
- merge!(d::Associative , others::Associative ...)
165
+ merge!(d::AbstractDict , others::AbstractDict ...)
166
166
167
167
Update collection with pairs from the other collections.
168
168
See also [`merge`](@ref).
@@ -182,7 +182,7 @@ Dict{Int64,Int64} with 3 entries:
182
182
1 => 4
183
183
```
184
184
"""
185
- function merge! (d:: Associative , others:: Associative ... )
185
+ function merge! (d:: AbstractDict , others:: AbstractDict ... )
186
186
for other in others
187
187
for (k,v) in other
188
188
d[k] = v
@@ -192,7 +192,7 @@ function merge!(d::Associative, others::Associative...)
192
192
end
193
193
194
194
"""
195
- merge!(combine, d::Associative , others::Associative ...)
195
+ merge!(combine, d::AbstractDict , others::AbstractDict ...)
196
196
197
197
Update collection with pairs from the other collections.
198
198
Values with the same key will be combined using the
@@ -221,7 +221,7 @@ Dict{Int64,Int64} with 3 entries:
221
221
1 => 0
222
222
```
223
223
"""
224
- function merge! (combine:: Function , d:: Associative , others:: Associative ... )
224
+ function merge! (combine:: Function , d:: AbstractDict , others:: AbstractDict ... )
225
225
for other in others
226
226
for (k,v) in other
227
227
d[k] = haskey (d, k) ? combine (d[k], v) : v
232
232
233
233
# very similar to `merge!`, but accepts any iterable and extends code
234
234
# that would otherwise only use `copy!` with arrays.
235
- function copy! (dest:: Union{Associative ,AbstractSet} , src)
235
+ function copy! (dest:: Union{AbstractDict ,AbstractSet} , src)
236
236
for x in src
237
237
push! (dest, x)
238
238
end
@@ -242,35 +242,35 @@ end
242
242
"""
243
243
keytype(type)
244
244
245
- Get the key type of an associative collection type. Behaves similarly to [`eltype`](@ref).
245
+ Get the key type of an dictionary type. Behaves similarly to [`eltype`](@ref).
246
246
247
247
# Examples
248
248
```jldoctest
249
249
julia> keytype(Dict(Int32(1) => "foo"))
250
250
Int32
251
251
```
252
252
"""
253
- keytype (:: Type{Associative {K,V}} ) where {K,V} = K
254
- keytype (a:: Associative ) = keytype (typeof (a))
255
- keytype (:: Type{A} ) where {A<: Associative } = keytype (supertype (A))
253
+ keytype (:: Type{AbstractDict {K,V}} ) where {K,V} = K
254
+ keytype (a:: AbstractDict ) = keytype (typeof (a))
255
+ keytype (:: Type{A} ) where {A<: AbstractDict } = keytype (supertype (A))
256
256
257
257
"""
258
258
valtype(type)
259
259
260
- Get the value type of an associative collection type. Behaves similarly to [`eltype`](@ref).
260
+ Get the value type of an dictionary type. Behaves similarly to [`eltype`](@ref).
261
261
262
262
# Examples
263
263
```jldoctest
264
264
julia> valtype(Dict(Int32(1) => "foo"))
265
265
String
266
266
```
267
267
"""
268
- valtype (:: Type{Associative {K,V}} ) where {K,V} = V
269
- valtype (:: Type{A} ) where {A<: Associative } = valtype (supertype (A))
270
- valtype (a:: Associative ) = valtype (typeof (a))
268
+ valtype (:: Type{AbstractDict {K,V}} ) where {K,V} = V
269
+ valtype (:: Type{A} ) where {A<: AbstractDict } = valtype (supertype (A))
270
+ valtype (a:: AbstractDict ) = valtype (typeof (a))
271
271
272
272
"""
273
- merge(d::Associative , others::Associative ...)
273
+ merge(d::AbstractDict , others::AbstractDict ...)
274
274
275
275
Construct a merged collection from the given collections. If necessary, the
276
276
types of the resulting collection will be promoted to accommodate the types of
@@ -302,11 +302,11 @@ Dict{String,Float64} with 3 entries:
302
302
"foo" => 0.0
303
303
```
304
304
"""
305
- merge (d:: Associative , others:: Associative ... ) =
305
+ merge (d:: AbstractDict , others:: AbstractDict ... ) =
306
306
merge! (_typeddict (d, others... ), others... )
307
307
308
308
"""
309
- merge(combine, d::Associative , others::Associative ...)
309
+ merge(combine, d::AbstractDict , others::AbstractDict ...)
310
310
311
311
Construct a merged collection from the given collections. If necessary, the
312
312
types of the resulting collection will be promoted to accommodate the types of
@@ -332,21 +332,21 @@ Dict{String,Float64} with 3 entries:
332
332
"foo" => 0.0
333
333
```
334
334
"""
335
- merge (combine:: Function , d:: Associative , others:: Associative ... ) =
335
+ merge (combine:: Function , d:: AbstractDict , others:: AbstractDict ... ) =
336
336
merge! (combine, _typeddict (d, others... ), others... )
337
337
338
338
promoteK (K) = K
339
339
promoteV (V) = V
340
340
promoteK (K, d, ds... ) = promoteK (promote_type (K, keytype (d)), ds... )
341
341
promoteV (V, d, ds... ) = promoteV (promote_type (V, valtype (d)), ds... )
342
- function _typeddict (d:: Associative , others:: Associative ... )
342
+ function _typeddict (d:: AbstractDict , others:: AbstractDict ... )
343
343
K = promoteK (keytype (d), others... )
344
344
V = promoteV (valtype (d), others... )
345
345
Dict {K,V} (d)
346
346
end
347
347
348
348
"""
349
- filter!(f, d::Associative )
349
+ filter!(f, d::AbstractDict )
350
350
351
351
Update `d`, removing elements for which `f` is `false`.
352
352
The function `f` is passed `key=>value` pairs.
@@ -365,11 +365,11 @@ Dict{Int64,String} with 2 entries:
365
365
1 => "a"
366
366
```
367
367
"""
368
- function filter! (f, d:: Associative )
368
+ function filter! (f, d:: AbstractDict )
369
369
badkeys = Vector {keytype(d)} ()
370
370
try
371
371
for pair in d
372
- # don't delete!(d, k) here, since associative types
372
+ # don't delete!(d, k) here, since dictionary types
373
373
# may not support mutation during iteration
374
374
f (pair) || push! (badkeys, pair. first)
375
375
end
@@ -382,7 +382,7 @@ function filter!(f, d::Associative)
382
382
return d
383
383
end
384
384
385
- function filter_in_one_pass! (f, d:: Associative )
385
+ function filter_in_one_pass! (f, d:: AbstractDict )
386
386
try
387
387
for pair in d
388
388
if ! f (pair)
@@ -395,12 +395,12 @@ function filter_in_one_pass!(f, d::Associative)
395
395
return d
396
396
end
397
397
398
- function filter!_dict_deprecation (e, f, d:: Associative )
398
+ function filter!_dict_deprecation (e, f, d:: AbstractDict )
399
399
if isa (e, MethodError) && e. f === f
400
400
depwarn (" In `filter!(f, dict)`, `f` is now passed a single pair instead of two arguments." , :filter! )
401
401
badkeys = Vector {keytype(d)} ()
402
402
for (k,v) in d
403
- # don't delete!(d, k) here, since associative types
403
+ # don't delete!(d, k) here, since dictionary types
404
404
# may not support mutation during iteration
405
405
f (k, v) || push! (badkeys, k)
406
406
end
@@ -414,7 +414,7 @@ function filter!_dict_deprecation(e, f, d::Associative)
414
414
end
415
415
416
416
"""
417
- filter(f, d::Associative )
417
+ filter(f, d::AbstractDict )
418
418
419
419
Return a copy of `d`, removing elements for which `f` is `false`.
420
420
The function `f` is passed `key=>value` pairs.
@@ -431,7 +431,7 @@ Dict{Int64,String} with 1 entry:
431
431
1 => "a"
432
432
```
433
433
"""
434
- function filter (f, d:: Associative )
434
+ function filter (f, d:: AbstractDict )
435
435
# don't just do filter!(f, copy(d)): avoid making a whole copy of d
436
436
df = empty (d)
437
437
try
@@ -455,9 +455,9 @@ function filter(f, d::Associative)
455
455
return df
456
456
end
457
457
458
- eltype (:: Type{Associative {K,V}} ) where {K,V} = Pair{K,V}
458
+ eltype (:: Type{AbstractDict {K,V}} ) where {K,V} = Pair{K,V}
459
459
460
- function isequal (l:: Associative , r:: Associative )
460
+ function isequal (l:: AbstractDict , r:: AbstractDict )
461
461
l === r && return true
462
462
if isa (l,ObjectIdDict) != isa (r,ObjectIdDict)
463
463
return false
@@ -471,7 +471,7 @@ function isequal(l::Associative, r::Associative)
471
471
true
472
472
end
473
473
474
- function == (l:: Associative , r:: Associative )
474
+ function == (l:: AbstractDict , r:: AbstractDict )
475
475
l === r && return true
476
476
if isa (l,ObjectIdDict) != isa (r,ObjectIdDict)
477
477
return false
@@ -486,15 +486,15 @@ function ==(l::Associative, r::Associative)
486
486
end
487
487
488
488
const hasha_seed = UInt === UInt64 ? 0x6d35bb51952d5539 : 0x952d5539
489
- function hash (a:: Associative , h:: UInt )
489
+ function hash (a:: AbstractDict , h:: UInt )
490
490
hv = hasha_seed
491
491
for (k,v) in a
492
492
hv ⊻= hash (k, hash (v))
493
493
end
494
494
hash (hv, h)
495
495
end
496
496
497
- function getindex (t:: Associative , key)
497
+ function getindex (t:: AbstractDict , key)
498
498
v = get (t, key, secret_table_token)
499
499
if v === secret_table_token
500
500
throw (KeyError (key))
@@ -504,12 +504,12 @@ end
504
504
505
505
# t[k1,k2,ks...] is syntactic sugar for t[(k1,k2,ks...)]. (Note
506
506
# that we need to avoid dispatch loops if setindex!(t,v,k) is not defined.)
507
- getindex (t:: Associative , k1, k2, ks... ) = getindex (t, tuple (k1,k2,ks... ))
508
- setindex! (t:: Associative , v, k1, k2, ks... ) = setindex! (t, v, tuple (k1,k2,ks... ))
507
+ getindex (t:: AbstractDict , k1, k2, ks... ) = getindex (t, tuple (k1,k2,ks... ))
508
+ setindex! (t:: AbstractDict , v, k1, k2, ks... ) = setindex! (t, v, tuple (k1,k2,ks... ))
509
509
510
- push! (t:: Associative , p:: Pair ) = setindex! (t, p. second, p. first)
511
- push! (t:: Associative , p:: Pair , q:: Pair ) = push! (push! (t, p), q)
512
- push! (t:: Associative , p:: Pair , q:: Pair , r:: Pair... ) = push! (push! (push! (t, p), q), r... )
510
+ push! (t:: AbstractDict , p:: Pair ) = setindex! (t, p. second, p. first)
511
+ push! (t:: AbstractDict , p:: Pair , q:: Pair ) = push! (push! (t, p), q)
512
+ push! (t:: AbstractDict , p:: Pair , q:: Pair , r:: Pair... ) = push! (push! (push! (t, p), q), r... )
513
513
514
514
# hashing objects by identity
515
515
@@ -522,7 +522,7 @@ and value type and thus its `eltype` is always `Pair{Any,Any}`.
522
522
523
523
See [`Dict`](@ref) for further help.
524
524
"""
525
- mutable struct ObjectIdDict <: Associative {Any,Any}
525
+ mutable struct ObjectIdDict <: AbstractDict {Any,Any}
526
526
ht:: Vector{Any}
527
527
ndel:: Int
528
528
ObjectIdDict () = new (Vector {Any} (uninitialized, 32 ), 0 )
@@ -613,6 +613,6 @@ copy(o::ObjectIdDict) = ObjectIdDict(o)
613
613
614
614
get! (o:: ObjectIdDict , key, default) = (o[key] = get (o, key, default))
615
615
616
- # For some Associative types, it is safe to implement filter!
616
+ # For some AbstractDict types, it is safe to implement filter!
617
617
# by deleting keys during iteration.
618
618
filter! (f, d:: ObjectIdDict ) = filter_in_one_pass! (f, d)
0 commit comments