Skip to content

Add a delete!(::MultiDict, k, v)? #654

Open

Description

@NHDaly

Does it make sense to add a delete!(::MultiDict, k, v) function where you can delete a specific key=>value pair?

Similar to the existing insert(::MultiDict, k, v) which inserts a specific key/value pair.

I was expecting this, and was surprised it's not offered.

Activity

  1. NHDaly commented on Aug 17, 2020

    @NHDaly
    ContributorAuthor

    Like, maybe something like this?:

    function delete_pair!(d::MultiDict, key, val)
        vs = get(d, key, Base.secret_table_token)
        if vs !== Base.secret_table_token
            filter!(x->x≠val, vs)
        end
        isempty(vs) && delete!(d, key)
        return d
    end

    Unfortunately, this does a linear scan of the values vector to find the target, which isn't great. But to fix this I guess we'd have to use a different datastructure there, like a Set{V}..

  2. NHDaly commented on Sep 24, 2020

    @NHDaly
    ContributorAuthor

    Indeed, the golang vector-based MultiDict does exactly the above, including the terrible worst-case O(n^2) performance if you delete all pairs from the dict with the same keys, one at a time.
    https://github.com/jwangsadinata/go-multimap/blob/c29f3d7f33b68d8bcd82880a558e9f8eed58e2b3/slicemultimap/slicemultimap.go#L76-L88

  3. oxinabox commented on Sep 24, 2020

    @oxinabox
    Member

    I think this makes sense

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