Introduce a TMultiMap like container for Sets
Issue #367
resolved
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)
-
repo owner -
reporter Cool! I’ll check it out. Thank you for your great work.
-
reporter - changed status to resolved
Already implemented.
- Log in to comment
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>>
:list<value>
hashset<value>
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)