Repository navigation
MultiDict: Have iterate() iterate over all pairs, as if the dict actually allowed duplicate keys - #676
MultiDict: Have iterate() iterate over all pairs, as if the dict actually allowed duplicate keys#676NHDaly wants to merge 5 commits into
iterate() iterate over all pairs, as if the dict actually allowed duplicate keys#676Conversation
…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
```
| (v !== Base.secret_table_token) && (pr[2] in v) | ||
| end | ||
|
|
||
| # TODO: For efficiency, we probably want a MultiKeySet to correspond to Dict's KeySet |
There was a problem hiding this comment.
Could we use an Accumulator for this?
|
I like this, what can i do to help advance it? |
| # 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 | ||
|
|
There was a problem hiding this comment.
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
4For 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.
There was a problem hiding this comment.
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.
|
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.
iterate() iterate over all pairs, as if the dict actually allowed duplicatesiterate() iterate over all pairs, as if the dict actually allowed duplicate keys
| # 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 |
There was a problem hiding this comment.
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
1I 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.
Stop implicit overloading via `import`
|
Also also, we need to update the tests |
|
I wrote above:
And oh, wow, i'm only JUST NOW noticing the
But 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. |
|
K, i've updated some of the above, as long as it was all cached-in to my brain. Heading to sleep now :) Night! |
I agree |
Fixes #655