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 Dec 19, 2017
1 parent 76f3c03 commit 64a15b0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 33 deletions.
11 changes: 11 additions & 0 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,17 @@ push!(t::AbstractDict, p::Pair) = setindex!(t, p.second, p.first)
push!(t::AbstractDict, p::Pair, q::Pair) = push!(push!(t, p), q)
push!(t::AbstractDict, p::Pair, q::Pair, r::Pair...) = push!(push!(push!(t, p), q), r...)

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

function convert(::Type{T}, x::AbstractDict) where T<:AbstractDict
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 @@ -190,21 +190,6 @@ end

empty(a::AbstractDict, ::Type{K}, ::Type{V}) where {K, V} = Dict{K, V}()

# conversion between Dict types
function convert(::Type{Dict{K,V}},d::AbstractDict) 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

@propagate_inbounds 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 @@ -472,5 +472,5 @@ function hash(s::Set, h::UInt)
hash(hv, 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)
15 changes: 0 additions & 15 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@ end

empty(d::WeakKeyDict, ::Type{K}, ::Type{V}) where {K, V} = WeakKeyDict{K, V}()

# conversion between Dict types
function convert(::Type{WeakKeyDict{K,V}},d::AbstractDict) 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
1 change: 0 additions & 1 deletion stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,6 @@ end
for (G, A) in ((GenericSet, AbstractSet),
(GenericDict, AbstractDict))
@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

0 comments on commit 64a15b0

Please sign in to comment.