Skip to content

Commit

Permalink
consolidate Associative conversions, and allow AbstractSet conver…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
JeffBezanson committed Sep 15, 2017
1 parent 189222f commit 39d9018
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 33 deletions.
11 changes: 11 additions & 0 deletions base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,17 @@ push!(t::Associative, p::Pair) = setindex!(t, p.second, p.first)
push!(t::Associative, p::Pair, q::Pair) = push!(push!(t, p), q)
push!(t::Associative, p::Pair, q::Pair, r::Pair...) = push!(push!(push!(t, p), q), r...)

# Associatives are convertible
convert(::Type{T}, x::T) where {T<:Associative} = x

function convert(::Type{T}, x::Associative) where T<:Associative
h = T(x)
if length(h) != length(x)
error("key collision during dictionary conversion")
end
return h
end

# hashing objects by identity

"""
Expand Down
15 changes: 0 additions & 15 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,6 @@ end
similar(d::Dict{K,V}) where {K,V} = Dict{K,V}()
similar(d::Dict, ::Type{Pair{K,V}}) where {K,V} = Dict{K,V}()

# conversion between Dict types
function convert(::Type{Dict{K,V}},d::Associative) where V where K
h = Dict{K,V}()
for (k,v) in d
ck = convert(K,k)
if !haskey(h,ck)
h[ck] = convert(V,v)
else
error("key collision during dictionary conversion")
end
end
return h
end
convert(::Type{Dict{K,V}},d::Dict{K,V}) where {K,V} = d

hashindex(key, sz) = (((hash(key)%Int) & (sz-1)) + 1)::Int

isslotempty(h::Dict, i::Int) = h.slots[i] == 0x0
Expand Down
4 changes: 2 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,5 +475,5 @@ function hash(s::Set, h::UInt)
return h
end

convert(::Type{Set{T}}, s::Set{T}) where {T} = s
convert(::Type{Set{T}}, x::Set) where {T} = Set{T}(x)
convert(::Type{T}, s::T) where {T<:AbstractSet} = s
convert(::Type{T}, s::AbstractSet) where {T<:AbstractSet} = T(s)
1 change: 0 additions & 1 deletion base/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,6 @@ end
for (G, A) in ((GenericSet, AbstractSet),
(GenericDict, Associative))
@eval begin
Base.convert(::Type{$G}, s::$A) = $G(s)
Base.done(s::$G, state) = done(s.s, state)
Base.next(s::$G, state) = next(s.s, state)
end
Expand Down
15 changes: 0 additions & 15 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ end
similar(d::WeakKeyDict{K,V}) where {K,V} = WeakKeyDict{K,V}()
similar(d::WeakKeyDict, ::Type{Pair{K,V}}) where {K,V} = WeakKeyDict{K,V}()

# conversion between Dict types
function convert(::Type{WeakKeyDict{K,V}},d::Associative) where V where K
h = WeakKeyDict{K,V}()
for (k,v) in d
ck = convert(K,k)
if !haskey(h,ck)
h[ck] = convert(V,v)
else
error("key collision during dictionary conversion")
end
end
return h
end
convert(::Type{WeakKeyDict{K,V}},d::WeakKeyDict{K,V}) where {K,V} = d

islocked(wkh::WeakKeyDict) = islocked(wkh.lock)
lock(f, wkh::WeakKeyDict) = lock(f, wkh.lock)
trylock(f, wkh::WeakKeyDict) = trylock(f, wkh.lock)
Expand Down

0 comments on commit 39d9018

Please sign in to comment.