Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions base/iddict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ function get(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, default)
val === default ? default : val::V
end

function getindex(d::IdDict{K,V}, @nospecialize(key)) where {K, V}
val = get(d, key, secret_table_token)
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
val === secret_table_token && throw(KeyError(key))
return val::V
end
Expand Down Expand Up @@ -134,23 +135,38 @@ length(d::IdDict) = d.count

copy(d::IdDict) = typeof(d)(d)

get!(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {K, V} = (d[key] = get(d, key, default))::V
function get!(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {K, V}
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
if val === secret_table_token
val = isa(default, V) ? default : convert(V, default)
setindex!(d, val, key)
return val
else
return val::V
end
end

function get(default::Callable, d::IdDict{K,V}, @nospecialize(key)) where {K, V}
val = get(d, key, secret_table_token)
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
if val === secret_table_token
val = default()
return default()
else
return val::V
end
return val
end

function get!(default::Callable, d::IdDict{K,V}, @nospecialize(key)) where {K, V}
val = get(d, key, secret_table_token)
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
if val === secret_table_token
val = default()
if !isa(val, V)
val = convert(V, val)
end
setindex!(d, val, key)
return val
else
return val::V
end
return val
end

in(@nospecialize(k), v::KeySet{<:Any,<:IdDict}) = get(v.dict, k, secret_table_token) !== secret_table_token
Expand Down
3 changes: 2 additions & 1 deletion test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ end
@test delete!(d, "a") === d
@test !haskey(d, "a")
@test_throws ArgumentError get!(IdDict{Symbol,Any}(), 2, "b")

@test get!(IdDict{Int,Int}(), 1, 2.0) === 2
@test get!(()->2.0, IdDict{Int,Int}(), 1) === 2

# sizehint! & rehash!
d = IdDict()
Expand Down