Skip to content

Commit 3af34b3

Browse files
authored
Merge pull request #25012 from JuliaLang/ajf/abstractdict
RFC: Rename `Associative` to `AbstractDict`
2 parents d192302 + 89d788d commit 3af34b3

24 files changed

+122
-118
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ CORE_SRCS := $(addprefix $(JULIAHOME)/, \
179179
base/abstractarray.jl \
180180
base/array.jl \
181181
base/bool.jl \
182-
base/associative.jl \
182+
base/abstractdict.jl \
183183
base/codevalidation.jl \
184184
base/error.jl \
185185
base/essentials.jl \

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ Deprecated or removed
754754
* The aliases `Complex32`, `Complex64` and `Complex128` have been deprecated in favor of `ComplexF16`,
755755
`ComplexF32` and `ComplexF64` respectively (#24647).
756756

757+
* `Associative` has been deprecated in favor of `AbstractDict` ([#25012]).
757758

758759
Command-line option changes
759760
---------------------------

base/associative.jl renamed to base/abstractdict.jl

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
# generic operations on associative collections
3+
# generic operations on dictionaries
44

55
"""
66
KeyError(key)
77
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
99
delete a non-existent element.
1010
"""
1111
struct KeyError <: Exception
@@ -14,33 +14,33 @@ end
1414

1515
const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
1616

17-
haskey(d::Associative, k) = in(k, keys(d))
17+
haskey(d::AbstractDict, k) = in(k, keys(d))
1818

19-
function in(p::Pair, a::Associative, valcmp=(==))
19+
function in(p::Pair, a::AbstractDict, valcmp=(==))
2020
v = get(a,p[1],secret_table_token)
2121
if v !== secret_table_token
2222
valcmp(v, p[2]) && return true
2323
end
2424
return false
2525
end
2626

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;
2929
Either look for e.g. A=>B instead, or use the `keys` or `values`
3030
function if you are looking for a key or value respectively.""")
3131
end
3232

33-
function summary(t::Associative)
33+
function summary(t::AbstractDict)
3434
n = length(t)
3535
return string(typeof(t), " with ", n, (n==1 ? " entry" : " entries"))
3636
end
3737

38-
struct KeySet{K, T <: Associative{K}} <: AbstractSet{K}
38+
struct KeySet{K, T <: AbstractDict{K}} <: AbstractSet{K}
3939
dict::T
4040
end
41-
KeySet(dict::Associative) = KeySet{keytype(dict), typeof(dict)}(dict)
41+
KeySet(dict::AbstractDict) = KeySet{keytype(dict), typeof(dict)}(dict)
4242

43-
struct ValueIterator{T<:Associative}
43+
struct ValueIterator{T<:AbstractDict}
4444
dict::T
4545
end
4646

@@ -78,9 +78,9 @@ return an iterator over the keys.
7878
function keys end
7979

8080
"""
81-
keys(a::Associative)
81+
keys(a::AbstractDict)
8282
83-
Return an iterator over all keys in an associative collection.
83+
Return an iterator over all keys in a dictionary.
8484
`collect(keys(a))` returns an array of keys.
8585
Since the keys are stored internally in a hash table,
8686
the order in which they are returned may vary.
@@ -100,10 +100,10 @@ julia> collect(keys(a))
100100
'a'
101101
```
102102
"""
103-
keys(a::Associative) = KeySet(a)
103+
keys(a::AbstractDict) = KeySet(a)
104104

105105
"""
106-
values(a::Associative)
106+
values(a::AbstractDict)
107107
108108
Return an iterator over all values in a collection.
109109
`collect(values(a))` returns an array of values.
@@ -125,7 +125,7 @@ julia> collect(values(a))
125125
2
126126
```
127127
"""
128-
values(a::Associative) = ValueIterator(a)
128+
values(a::AbstractDict) = ValueIterator(a)
129129

130130
"""
131131
pairs(collection)
@@ -136,24 +136,24 @@ This includes arrays, where the keys are the array indices.
136136
"""
137137
pairs(collection) = Generator(=>, keys(collection), values(collection))
138138

139-
pairs(a::Associative) = a
139+
pairs(a::AbstractDict) = a
140140

141141
"""
142-
empty(a::Associative, [index_type=keytype(a)], [value_type=valtype(a)])
142+
empty(a::AbstractDict, [index_type=keytype(a)], [value_type=valtype(a)])
143143
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
145145
values of type `value_type`. The second and third arguments are optional and default to the
146146
input's `keytype` and `valtype`, respectively. (If only one of the two types is specified,
147147
it is assumed to be the `value_type`, and the `index_type` we default to `keytype(a)`).
148148
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
150150
return for the given index and value types, by specializing on the three-argument signature.
151151
The default is to return an empty `Dict`.
152152
"""
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`.
155155

156-
function copy(a::Associative)
156+
function copy(a::AbstractDict)
157157
b = empty(a)
158158
for (k,v) in a
159159
b[k] = v
@@ -162,7 +162,7 @@ function copy(a::Associative)
162162
end
163163

164164
"""
165-
merge!(d::Associative, others::Associative...)
165+
merge!(d::AbstractDict, others::AbstractDict...)
166166
167167
Update collection with pairs from the other collections.
168168
See also [`merge`](@ref).
@@ -182,7 +182,7 @@ Dict{Int64,Int64} with 3 entries:
182182
1 => 4
183183
```
184184
"""
185-
function merge!(d::Associative, others::Associative...)
185+
function merge!(d::AbstractDict, others::AbstractDict...)
186186
for other in others
187187
for (k,v) in other
188188
d[k] = v
@@ -192,7 +192,7 @@ function merge!(d::Associative, others::Associative...)
192192
end
193193

194194
"""
195-
merge!(combine, d::Associative, others::Associative...)
195+
merge!(combine, d::AbstractDict, others::AbstractDict...)
196196
197197
Update collection with pairs from the other collections.
198198
Values with the same key will be combined using the
@@ -221,7 +221,7 @@ Dict{Int64,Int64} with 3 entries:
221221
1 => 0
222222
```
223223
"""
224-
function merge!(combine::Function, d::Associative, others::Associative...)
224+
function merge!(combine::Function, d::AbstractDict, others::AbstractDict...)
225225
for other in others
226226
for (k,v) in other
227227
d[k] = haskey(d, k) ? combine(d[k], v) : v
@@ -232,7 +232,7 @@ end
232232

233233
# very similar to `merge!`, but accepts any iterable and extends code
234234
# that would otherwise only use `copy!` with arrays.
235-
function copy!(dest::Union{Associative,AbstractSet}, src)
235+
function copy!(dest::Union{AbstractDict,AbstractSet}, src)
236236
for x in src
237237
push!(dest, x)
238238
end
@@ -242,35 +242,35 @@ end
242242
"""
243243
keytype(type)
244244
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).
246246
247247
# Examples
248248
```jldoctest
249249
julia> keytype(Dict(Int32(1) => "foo"))
250250
Int32
251251
```
252252
"""
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))
256256

257257
"""
258258
valtype(type)
259259
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).
261261
262262
# Examples
263263
```jldoctest
264264
julia> valtype(Dict(Int32(1) => "foo"))
265265
String
266266
```
267267
"""
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))
271271

272272
"""
273-
merge(d::Associative, others::Associative...)
273+
merge(d::AbstractDict, others::AbstractDict...)
274274
275275
Construct a merged collection from the given collections. If necessary, the
276276
types of the resulting collection will be promoted to accommodate the types of
@@ -302,11 +302,11 @@ Dict{String,Float64} with 3 entries:
302302
"foo" => 0.0
303303
```
304304
"""
305-
merge(d::Associative, others::Associative...) =
305+
merge(d::AbstractDict, others::AbstractDict...) =
306306
merge!(_typeddict(d, others...), others...)
307307

308308
"""
309-
merge(combine, d::Associative, others::Associative...)
309+
merge(combine, d::AbstractDict, others::AbstractDict...)
310310
311311
Construct a merged collection from the given collections. If necessary, the
312312
types of the resulting collection will be promoted to accommodate the types of
@@ -332,21 +332,21 @@ Dict{String,Float64} with 3 entries:
332332
"foo" => 0.0
333333
```
334334
"""
335-
merge(combine::Function, d::Associative, others::Associative...) =
335+
merge(combine::Function, d::AbstractDict, others::AbstractDict...) =
336336
merge!(combine, _typeddict(d, others...), others...)
337337

338338
promoteK(K) = K
339339
promoteV(V) = V
340340
promoteK(K, d, ds...) = promoteK(promote_type(K, keytype(d)), ds...)
341341
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...)
343343
K = promoteK(keytype(d), others...)
344344
V = promoteV(valtype(d), others...)
345345
Dict{K,V}(d)
346346
end
347347

348348
"""
349-
filter!(f, d::Associative)
349+
filter!(f, d::AbstractDict)
350350
351351
Update `d`, removing elements for which `f` is `false`.
352352
The function `f` is passed `key=>value` pairs.
@@ -365,11 +365,11 @@ Dict{Int64,String} with 2 entries:
365365
1 => "a"
366366
```
367367
"""
368-
function filter!(f, d::Associative)
368+
function filter!(f, d::AbstractDict)
369369
badkeys = Vector{keytype(d)}()
370370
try
371371
for pair in d
372-
# don't delete!(d, k) here, since associative types
372+
# don't delete!(d, k) here, since dictionary types
373373
# may not support mutation during iteration
374374
f(pair) || push!(badkeys, pair.first)
375375
end
@@ -382,7 +382,7 @@ function filter!(f, d::Associative)
382382
return d
383383
end
384384

385-
function filter_in_one_pass!(f, d::Associative)
385+
function filter_in_one_pass!(f, d::AbstractDict)
386386
try
387387
for pair in d
388388
if !f(pair)
@@ -395,12 +395,12 @@ function filter_in_one_pass!(f, d::Associative)
395395
return d
396396
end
397397

398-
function filter!_dict_deprecation(e, f, d::Associative)
398+
function filter!_dict_deprecation(e, f, d::AbstractDict)
399399
if isa(e, MethodError) && e.f === f
400400
depwarn("In `filter!(f, dict)`, `f` is now passed a single pair instead of two arguments.", :filter!)
401401
badkeys = Vector{keytype(d)}()
402402
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
404404
# may not support mutation during iteration
405405
f(k, v) || push!(badkeys, k)
406406
end
@@ -414,7 +414,7 @@ function filter!_dict_deprecation(e, f, d::Associative)
414414
end
415415

416416
"""
417-
filter(f, d::Associative)
417+
filter(f, d::AbstractDict)
418418
419419
Return a copy of `d`, removing elements for which `f` is `false`.
420420
The function `f` is passed `key=>value` pairs.
@@ -431,7 +431,7 @@ Dict{Int64,String} with 1 entry:
431431
1 => "a"
432432
```
433433
"""
434-
function filter(f, d::Associative)
434+
function filter(f, d::AbstractDict)
435435
# don't just do filter!(f, copy(d)): avoid making a whole copy of d
436436
df = empty(d)
437437
try
@@ -455,9 +455,9 @@ function filter(f, d::Associative)
455455
return df
456456
end
457457

458-
eltype(::Type{Associative{K,V}}) where {K,V} = Pair{K,V}
458+
eltype(::Type{AbstractDict{K,V}}) where {K,V} = Pair{K,V}
459459

460-
function isequal(l::Associative, r::Associative)
460+
function isequal(l::AbstractDict, r::AbstractDict)
461461
l === r && return true
462462
if isa(l,ObjectIdDict) != isa(r,ObjectIdDict)
463463
return false
@@ -471,7 +471,7 @@ function isequal(l::Associative, r::Associative)
471471
true
472472
end
473473

474-
function ==(l::Associative, r::Associative)
474+
function ==(l::AbstractDict, r::AbstractDict)
475475
l === r && return true
476476
if isa(l,ObjectIdDict) != isa(r,ObjectIdDict)
477477
return false
@@ -486,15 +486,15 @@ function ==(l::Associative, r::Associative)
486486
end
487487

488488
const hasha_seed = UInt === UInt64 ? 0x6d35bb51952d5539 : 0x952d5539
489-
function hash(a::Associative, h::UInt)
489+
function hash(a::AbstractDict, h::UInt)
490490
hv = hasha_seed
491491
for (k,v) in a
492492
hv ⊻= hash(k, hash(v))
493493
end
494494
hash(hv, h)
495495
end
496496

497-
function getindex(t::Associative, key)
497+
function getindex(t::AbstractDict, key)
498498
v = get(t, key, secret_table_token)
499499
if v === secret_table_token
500500
throw(KeyError(key))
@@ -504,12 +504,12 @@ end
504504

505505
# t[k1,k2,ks...] is syntactic sugar for t[(k1,k2,ks...)]. (Note
506506
# 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...))
509509

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...)
513513

514514
# hashing objects by identity
515515

@@ -522,7 +522,7 @@ and value type and thus its `eltype` is always `Pair{Any,Any}`.
522522
523523
See [`Dict`](@ref) for further help.
524524
"""
525-
mutable struct ObjectIdDict <: Associative{Any,Any}
525+
mutable struct ObjectIdDict <: AbstractDict{Any,Any}
526526
ht::Vector{Any}
527527
ndel::Int
528528
ObjectIdDict() = new(Vector{Any}(uninitialized, 32), 0)
@@ -613,6 +613,6 @@ copy(o::ObjectIdDict) = ObjectIdDict(o)
613613

614614
get!(o::ObjectIdDict, key, default) = (o[key] = get(o, key, default))
615615

616-
# For some Associative types, it is safe to implement filter!
616+
# For some AbstractDict types, it is safe to implement filter!
617617
# by deleting keys during iteration.
618618
filter!(f, d::ObjectIdDict) = filter_in_one_pass!(f, d)

0 commit comments

Comments
 (0)