Skip to content

MultiDict: insert!(a, k, ARRAY) appends array instead of adding entry  #522

Closed

Description

@Petr-Hlavenka

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?

Activity

  1. oxinabox commented on Aug 9, 2019

    @oxinabox
    Member

    Yeah, seems bad.
    OTOH, can we remove MultiDict?

    I always just use DefaultDict(Vector)

  2. Petr-Hlavenka commented on Aug 9, 2019

    @Petr-Hlavenka
    ContributorAuthor

    MultiDict and DefaultDict have different usecases

    • 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).
  3. oxinabox commented on Aug 9, 2019

    @oxinabox
    Member

    Could you give me an example script of something that is easier done with a MultiDict than with a DefaultDict(Vector) ?

    AFAIK insert(multidict, key, value) is the same as push!(defdict_vector[key], value)

    julia> md = MultiDict()
    MultiDict{Any,Any}(Dict{Any,Array{Any,1}}())
    
    julia> insert!(md, :a, 1)
    MultiDict{Any,Any}(Dict{Any,Array{Any,1}}(:a => [1]))
    
    julia> insert!(md, :a, 1)
    MultiDict{Any,Any}(Dict{Any,Array{Any,1}}(:a => [1, 1]))
    
    julia> md
    MultiDict{Any,Any}(Dict{Any,Array{Any,1}}(:a => [1, 1]))
    

    vs

    julia> dd = DefaultDict(Vector)
    DefaultDict{Any,Any,UnionAll} with 0 entries
    
    julia> push!(dd[:a], 1)
    1-element Array{Any,1}:
     1
    
    julia> push!(dd[:a], 1)
    2-element Array{Any,1}:
     1
     1
    
    julia> dd
    DefaultDict{Any,Any,UnionAll} with 1 entry:
      :a => Any[1, 1]
    

    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).

  4. Petr-Hlavenka commented on Aug 12, 2019

    @Petr-Hlavenka
    ContributorAuthor

    @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".

  5. NHDaly commented on Aug 17, 2020

    @NHDaly
    Contributor

    @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}.

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions