You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MultiDict has a very unexpected behavior. If one inserts a key=>val combination, the val is added to an array of previously inserted values of the corresponding key.
BUT, if val<:AbstractArray, the elements in the val array are appended to the previous array.
This is completely broken behavior and not mentioned in documentation.
Is it OK to make PR that fixes it or is that a well thought behavior - in that case I'll just make my own MultiDict?
the multi-dict is actually a Dict where for each Key an array of Vals are stored. By every insert! action the new Val is appended to the array. So to say, it is an accumulating dictionary. I'd keep it here as it may be useable for others, but I'd definitely change the behavior (and also tests as they indicate somebody wanted the special behavior for arrays).
Similar kind of thing is true for Accumulator and DefaultDict(zero), but in the Accumulators case it has some extra convience for a bunch of functions (e.g. the MultiSet/Bag stuff).
@oxinabox Could you check the proposal in the PR #523
I've changed the behavior:
before:
d =MultiDict{Char,String}()
insert!(d, 'a', "foo")
insert!(d, 'a', "bar")
d['a'] == ["foo", "bar"] #objects are added to array as they come in
d =MultiDict{Char,Int}()
insert!(d, 'a', [1,2])
insert!(d, 'a', [3,4])
d['a'] == [1,2,3,4] #but arrays are just added to a growing array
this PR
d =MultiDict{Char,Array{Int,1}}()
insert!(d, 'a', [1,2])
insert!(d, 'a', [3,4])
d['a'] == [[1,2],[3,4]] #arrays are treated as any other object, added to an array-of-array as they come in
I'm using the MultiArray as a "temporary result storage" to see evolution during an iterative call within a cycle. And the array "specialization" was very surprising (almost seemed to me as a bug) and definitely not desired. You can always just vcat the stored array-of-array. With old behavior there is no chance to separate it to reconstruct each "insertion call".
@oxinabox: The main place where I expected a MultiDict to be different than a DefaultDict{Vector} is that I expected a MultiDict to pretend it contains multiple pairs 1=>1 and 1=>2, even though it stores 1=>[1,2]. So i would expect that iterating a MultiDict should return two Pair{Int,Int}, instead of one Pair{Int,Vector{Int}.
That is, mainly, i expected MultiDict to be providing a dictionary that can hold multiple pairs even if they have the same key, but can still provide all matches for a given key.
Activity
oxinabox commentedon Aug 9, 2019
Yeah, seems bad.
OTOH, can we remove MultiDict?
I always just use
DefaultDict(Vector)Petr-Hlavenka commentedon Aug 9, 2019
MultiDict and DefaultDict have different usecases
insert!action the new Val is appended to the array. So to say, it is an accumulating dictionary. I'd keep it here as it may be useable for others, but I'd definitely change the behavior (and also tests as they indicate somebody wanted the special behavior for arrays).oxinabox commentedon Aug 9, 2019
Could you give me an example script of something that is easier done with a
MultiDictthan with aDefaultDict(Vector)?AFAIK
insert(multidict, key, value)is the same aspush!(defdict_vector[key], value)vs
Similar kind of thing is true for
AccumulatorandDefaultDict(zero), but in theAccumulators case it has some extra convience for a bunch of functions (e.g. the MultiSet/Bag stuff).Petr-Hlavenka commentedon Aug 12, 2019
@oxinabox Could you check the proposal in the PR #523
I've changed the behavior:
before:
this PR
I'm using the MultiArray as a "temporary result storage" to see evolution during an iterative call within a cycle. And the array "specialization" was very surprising (almost seemed to me as a bug) and definitely not desired. You can always just vcat the stored array-of-array. With old behavior there is no chance to separate it to reconstruct each "insertion call".
NHDaly commentedon Aug 17, 2020
@oxinabox: The main place where I expected a
MultiDictto be different than aDefaultDict{Vector}is that I expected aMultiDictto pretend it contains multiple pairs1=>1and1=>2, even though it stores1=>[1,2]. So i would expect that iterating a MultiDict should return two Pair{Int,Int}, instead of one Pair{Int,Vector{Int}.But in fact it doesn't do this currently: #655
That is, mainly, i expected MultiDict to be providing a dictionary that can hold multiple pairs even if they have the same key, but can still provide all matches for a given key.