views:

174

answers:

3

I'm looking for a .Net implementation of a multiset. Can anyone recommend a good one?

(A multiset, or bag, is a set that can have duplicate values, and on which you can do set operations: intersection, difference, etc. A shopping cart for instance could be thought of as a multiset because you can have multiple occurrences of the same product.)

+1  A: 

Please see: C# Set collection?

Mitch Wheat
Thanks. A couple of posters mentioned Wintellect Power Collections, which has a Bag<T> type. It looks pretty good.
dangph
There's also the C5 stuff, but I don't think it implements set operations.
Mitch Wheat
+1  A: 

I do not know about one, however you could use a Dictionary for that, in which the value is the quantity of the item. And when the item is added for the second time, you vould increase the value for it in the dictionary.

An other possibility would be to simply use a List of items, in which you could put duplicates. This might be a better approach for a shopping cart.

treaschf
Nice idea. But I need to be able to find the difference between two sets efficiently. If I rolled my own, I'd have to put a lot of effort into making sure it had that property. So I'd rather not do that.
dangph
Your idea for dictionaries with counts has grown on me. I think it would work well if your items have a Count property (which they do happen to have in my case) rather than being discrete values. Set difference should be O(N). A multiset would be better if you have discrete values.
dangph
+1  A: 

Hey,

You might also want to have a look at http://www.koders.com/csharp/fid38F43B7D46B1F44EA81C7EADA9E4B91F58E75123.aspx?s=textbox

Hope it helps.

Izmoto