From 0130c1c2dd5b9e84af8a5c7dc7f49886a7bb1241 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 30 Aug 2017 17:14:01 -0400 Subject: [PATCH] consolidate `Associative` conversions, and allow `AbstractSet` conversions --- base/associative.jl | 11 +++++++++++ base/dict.jl | 15 --------------- base/set.jl | 4 ++-- base/test.jl | 1 - base/weakkeydict.jl | 15 --------------- 5 files changed, 13 insertions(+), 33 deletions(-) diff --git a/base/associative.jl b/base/associative.jl index 44ecd6faed3d5..96c3a356d5876 100644 --- a/base/associative.jl +++ b/base/associative.jl @@ -466,6 +466,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 """ diff --git a/base/dict.jl b/base/dict.jl index 10c326b1358a1..42b00d4b8c807 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -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 diff --git a/base/set.jl b/base/set.jl index 86f1e93b889b4..72dd3b39da905 100644 --- a/base/set.jl +++ b/base/set.jl @@ -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) diff --git a/base/test.jl b/base/test.jl index 19255d6d8e11a..44aaf8a096146 100644 --- a/base/test.jl +++ b/base/test.jl @@ -1390,7 +1390,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 diff --git a/base/weakkeydict.jl b/base/weakkeydict.jl index 1ff64f5b172b1..4a64f856dc2ce 100644 --- a/base/weakkeydict.jl +++ b/base/weakkeydict.jl @@ -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)