Introduce a TMultiMap like container for Sets

Issue #367 resolved
Markus created an issue

Spring4D already has a TMultiMap. Similarly, I would like to ask for some sort of 'TMultiSet<Key, Value>' which is basically a TDictionary<Key, ISet<Value>>. Regarding the naming I'm really not sure how to call a container like that. However, I think this container can be handy and presumably easy to implement. It would simplify the following code:

VAR MultiSet := TCollections.CreateDictionary<Integer, ISet<Integer>>;

FOR VAR Value IN SomeList DO BEGIN

  VAR InnerSet : ISet<Integer>;

  IF NOT MultiSet.TryGetValue(Value.ID, InnerSet) THEN BEGIN
    InnerSet := TCollections.CreateSet<Integer>;
    MultiSet.Add(Value.ID, InnerSet);
  END;

  InnerSet.Add(Value.OtherIntegerValue);
END;

This could would be reduced to

VAR MultiSet := TCollections.CreateMultiSet<Integer, Integer>;

FOR VAR Value IN SomeList DO BEGIN
  MultiSet.Add(Value.ID, Value.OtherID);
END;

Comments (3)

  1. Stefan Glienke repo owner

    There already is this type (in the develop branch) - you get it via TCollections.CreateHashMultiMap

    There are three flavors of multimaps - they all are like a dictionary<key, collection<value>>:

    • the “normal” multimap - also called listmultimap where collection is a list<value>
    • hashmultimap where the collection is a hashset<value>
    • treemultimap where the collection is a redblacktree<value>

    I covered them in my DelphiCon session last year - around the 44min mark.

    There also is a multiset - but that is something different - it’s like a dictionary<t,integer> (see https://en.wikipedia.org/wiki/Multiset)

  2. Log in to comment