Skip to content

MultiDict: Have iterate() iterate over all pairs, as if the dict actually allowed duplicate keys - #676

Draft
NHDaly wants to merge 5 commits into
JuliaCollections:masterfrom
NHDaly:nhd-MultiDict-iterate
Draft

NHDaly wants to merge 5 commits into
JuliaCollections:masterfrom
NHDaly:nhd-MultiDict-iterate

Conversation

@NHDaly

@NHDaly NHDaly commented Sep 5, 2020 •

Copy link
Copy Markdown
Contributor
julia> collect(MultiDict(3=>3, 1=>2, 1=>4))
3-element Array{Pair{Int64,Int64},1}:
 3 => 3
 1 => 2
 1 => 4

Fixes #655

…tually allowed duplicates

```julia
julia> collect(MultiDict(3=>3, 1=>2, 1=>4))
3-element Array{Pair{Int64,Int64},1}:
 3 => 3
 1 => 2
 1 => 4
```
Comment thread src/multi_dict.jl Resolved
Comment thread src/multi_dict.jl
(v !== Base.secret_table_token) && (pr[2] in v)
end

# TODO: For efficiency, we probably want a MultiKeySet to correspond to Dict's KeySet

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use an Accumulator for this?

@oxinabox

Copy link
Copy Markdown
Member

I like this, what can i do to help advance it?
What questions do we need to answer?

Comment thread src/multi_dict.jl Outdated
Comment on lines +77 to +82
# TODO: what should values() do?
Base.values(md::MultiDict) = values(md.d)
#function Base.values(md::MultiDict)
# flatten(vs for (_,vs) in md.d)
#end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One remaining question is what should values(md::MultiDict) return? Should it iterate all of the (flattened) values? Or should it iterate over the vectors?

That is, which of these options is most consistent and useful:

julia> values(MultiDict(3=>3, 1=>2, 1=>4))
Base.ValueIterator for a Dict{Int64,Array{Int64,1}} with 2 entries. Values:
  [3]
  [2, 4]

or

julia> values(MultiDict(3=>3, 1=>2, 1=>4))
Base.ValueIterator for a MultiDict{Int64,Int64} with 3 entries. Values:
  3
  2
  4

For consistency with iterate() I would think it should be the latter. But for consistency with getindex, I could see an argument for the former.

In fact, for consistency with getindex, I can understand why iterate might return what it did before this PR!
I'm starting to wonder if maybe we actually want to provide like two different types of interfaces over this dict? One for the aggregated view and one for the pairs view?

But, I guess, if you just want the aggregated view, you could just use a DefaultDict{K, Vector{V}}... So i think continuing to pursue the pairs-based interface makes sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the golang multimap does indeed flatten the values (the second approach), despite its Get(k) function returning a vector. So I think we should do this same. this is what felt most intuitive to me.

https://github.com/jwangsadinata/go-multimap/blob/c29f3d7f33b68d8bcd82880a558e9f8eed58e2b3/setmultimap/setmultimap.go#L144-L154

@NHDaly

NHDaly commented Sep 24, 2020

Copy link
Copy Markdown
Contributor Author

Thanks @oxinabox! I guess the questions are part of the overall MultiDict redesign: #666

The main remaining question is about whether MultiDict should allow duplicates or not. It seems that different languages take different approaches, including providing both options via different implementations. I think we could do something similar, and/or control this behavior via type parameter or something.

I don't necessarily think that should impact this PR though.

…extend.

Per BlueStyle, we should overload via qualified name, and import names via `using`. I simply lapsed in my earlier commit.
@NHDaly NHDaly changed the title MultiDict: Have iterate() iterate over all pairs, as if the dict actually allowed duplicates MultiDict: Have iterate() iterate over all pairs, as if the dict actually allowed duplicate keys Sep 24, 2020
Comment thread src/multi_dict.jl Outdated
Comment on lines +72 to +75
# TODO: I'm not actually sure if we need this eachindex?
function Base.eachindex(md::MultiDict)
flatten((repeated(k, length(vs)) for (k,vs) in md.d))
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is the other TODO..

I don't remember what i was referring to... For Base.Dict, it looks like eachindex just calls keys():
https://github.com/JuliaLang/julia/blob/7e8f2c05cd39aec8d4349ec915bf5c68cdf4c072/base/abstractarray.jl#L208-L209

So i'm not really sure why i wrote it?


EDIT: AH, this is a similar question to the below. It's because in order for collect() to work, eachindex() has to return the same number of elements. So given md = MultiDict(1=>2, 3=>3, 1=>4), eachindex(md) must return (unordered) [1,1,3]. But currently, keys() delegates to the dict:

julia> keys(md)
Base.KeySet for a Dict{Int64,Array{Int64,1}} with 2 entries. Keys:
  3
  1

julia> collect(eachindex(md))
3-element Array{Int64,1}:
 3
 1
 1

I guess i should probably just change this so that keys returns [3,1,1], and then just use the standard delegation from eachindex => keys. I think keys() should certainly include duplicates, because that's sort of the whole thing about a MultiDict.

So that needs to change too before this is finished.

@NHDaly

NHDaly commented Sep 24, 2020

Copy link
Copy Markdown
Contributor Author

Also also, we need to update the tests

@NHDaly

NHDaly commented Sep 24, 2020

Copy link
Copy Markdown
Contributor Author

I wrote above:

I'm starting to wonder if maybe we actually want to provide like two different types of interfaces over this dict? One for the aggregated view and one for the pairs view?

And oh, wow, i'm only JUST NOW noticing the enumerateall(md::MultiDict) at the bottom of this file. It looks like this was exactly intended to provide the pair-wise view over the MultiDict. there are a few "pairwise" functions that it provided:

  • enumerateall(): allows iterating the pairs (as we've done in this PR)
  • count(): return the total number of inserted pairs, whereas length() was returning the total number of unique keys.

But enumerateall() no longer works. I'll probably just delete it since we're replacing it in this PR 👍

I do see the merit in this idea, but as I stated above, I think that for a MultiDict type, we should treat everything as if it really was storing multiple entires that just happen to have the same key. That is consistent with Golang's and Python's multidicts, and i think it makes sense.

@NHDaly

NHDaly commented Sep 24, 2020

Copy link
Copy Markdown
Contributor Author

K, i've updated some of the above, as long as it was all cached-in to my brain. Heading to sleep now :) Night!

@oxinabox

Copy link
Copy Markdown
Member

, I think that for a MultiDict type, we should treat everything as if it really was storing multiple entires that just happen to have the same key.

I agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shouldn't iterate(::MultiDict) return each pair, like it was inserted?

2 participants