Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return pairs instead of tuples when iterating Associatives #12327

Merged
merged 2 commits into from
Jul 27, 2015
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
9 changes: 3 additions & 6 deletions base/collections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ PriorityQueue{K,V}(a::AbstractArray{Tuple{K,V}}, o::Ordering=Forward) = Priority
length(pq::PriorityQueue) = length(pq.xs)
isempty(pq::PriorityQueue) = isempty(pq.xs)
haskey(pq::PriorityQueue, key) = haskey(pq.index, key)
peek(pq::PriorityQueue) = (kv = pq.xs[1]; (kv.first, kv.second))
peek(pq::PriorityQueue) = pq.xs[1]


function percolate_down!(pq::PriorityQueue, i::Integer)
Expand Down Expand Up @@ -277,17 +277,14 @@ function dequeue!(pq::PriorityQueue, key)
key
end


# Unordered iteration through key value pairs in a PriorityQueue
start(pq::PriorityQueue) = start(pq.index)

done(pq::PriorityQueue, i) = done(pq.index, i)

function next(pq::PriorityQueue, i)
function next{K,V}(pq::PriorityQueue{K,V}, i)
(k, idx), i = next(pq.index, i)
return ((k, pq.xs[idx].second), i)
return (pq.xs[idx], i)
end


end # module Collections

21 changes: 13 additions & 8 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__

haskey(d::Associative, k) = in(k,keys(d))

function in(p::Tuple{Any,Any}, a::Associative)
function in(p, a::Associative)
if !isa(p,Pair)
error("""Associative collections only contain Pairs;
Either look for e.g. A=>B instead, or use the `keys` or `values`
function if you are looking for a key or value respectively.""")
end
v = get(a,p[1],secret_table_token)
!is(v, secret_table_token) && (v == p[2])
end
Expand Down Expand Up @@ -170,8 +175,8 @@ end

length(v::Union{KeyIterator,ValueIterator}) = length(v.dict)
isempty(v::Union{KeyIterator,ValueIterator}) = isempty(v.dict)
_tt1{A,B}(::Type{Tuple{A,B}}) = A
_tt2{A,B}(::Type{Tuple{A,B}}) = B
_tt1{A,B}(::Type{Pair{A,B}}) = A
_tt2{A,B}(::Type{Pair{A,B}}) = B
eltype{D}(::Type{KeyIterator{D}}) = _tt1(eltype(D))
eltype{D}(::Type{ValueIterator{D}}) = _tt2(eltype(D))

Expand Down Expand Up @@ -232,7 +237,7 @@ function filter!(f, d::Associative)
end
filter(f, d::Associative) = filter!(f,copy(d))

eltype{K,V}(::Type{Associative{K,V}}) = Tuple{K,V}
eltype{K,V}(::Type{Associative{K,V}}) = Pair{K,V}

function isequal(l::Associative, r::Associative)
if isa(l,ObjectIdDict) != isa(r,ObjectIdDict)
Expand Down Expand Up @@ -340,7 +345,7 @@ _oidd_nextind(a, i) = reinterpret(Int,ccall(:jl_eqtable_nextind, Csize_t, (Any,

start(t::ObjectIdDict) = _oidd_nextind(t.ht, 0)
done(t::ObjectIdDict, i) = (i == -1)
next(t::ObjectIdDict, i) = ((t.ht[i+1],t.ht[i+2]), _oidd_nextind(t.ht, i+2))
next(t::ObjectIdDict, i) = (Pair{Any,Any}(t.ht[i+1],t.ht[i+2]), _oidd_nextind(t.ht, i+2))

function length(d::ObjectIdDict)
n = 0
Expand Down Expand Up @@ -755,7 +760,7 @@ end

start(t::Dict) = skip_deleted(t, 1)
done(t::Dict, i) = i > length(t.vals)
next(t::Dict, i) = ((t.keys[i],t.vals[i]), skip_deleted(t,i+1))
next{K,V}(t::Dict{K,V}, i) = (Pair{K,V}(t.keys[i],t.vals[i]), skip_deleted(t,i+1))

isempty(t::Dict) = (t.count == 0)
length(t::Dict) = t.count
Expand Down Expand Up @@ -818,8 +823,8 @@ isempty(wkh::WeakKeyDict) = isempty(wkh.ht)

start(t::WeakKeyDict) = start(t.ht)
done(t::WeakKeyDict, i) = done(t.ht, i)
function next{K}(t::WeakKeyDict{K}, i)
function next{K,V}(t::WeakKeyDict{K,V}, i)
kv, i = next(t.ht, i)
((kv[1].value::K,kv[2]), i)
(Pair{K,V}(kv[1].value::K,kv[2]), i)
end
length(t::WeakKeyDict) = length(t.ht)
4 changes: 2 additions & 2 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function next(::EnvHash, i)
if m == nothing
error("malformed environment entry: $env")
end
(ByteString[convert(typeof(env),x) for x in m.captures], i+1)
(Pair{ByteString,ByteString}(m.captures[1], m.captures[2]), i+1)
end
end

Expand All @@ -150,7 +150,7 @@ function next(hash::EnvHash, block::Tuple{Ptr{UInt16},Ptr{UInt16}})
if m == nothing
error("malformed environment entry: $env")
end
(ByteString[convert(typeof(env),x) for x in m.captures], (pos+len*2, blk))
(Pair{ByteString,ByteString}(m.captures[1], m.captures[2]), (pos+len*2, blk))
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ let
end

@test_throws ArgumentError first(Dict())
@test first(Dict(:f=>2)) == (:f,2)
@test first(Dict(:f=>2)) == :f=>2

# issue #1821
let
Expand Down