views:

395

answers:

5

I find myself very intrigued by the existence of a ConcurrentBag<T> class in the upcoming .NET 4.0 framework:

Bags are useful for storing objects when ordering doesn't matter, and unlike sets, bags support duplicates.

My question is: how might this idea be implemented? Most collections I'm familiar with essentially amount to (under the hood) some form of array, in which order may not "matter," but there is an order (which is why, even though it doesn't need to, enumeration will pretty much always go through an unchanged collection, be it List, Queue, Stack, etc. in the same sequence).

If I had to guess, I might suggest that internally it could be a Dictionary<T, LinkedList<T>>; but that actually seems quite dubious considering it wouldn't make sense to use just any type T as a key.

What I'm expecting/hoping is that this is actually an established object type that has already been "figured out" somewhere, and that somebody who knows of this established type can tell me about it. It's just so unusual to me--one of those concepts that's easy to understand in real life, but is difficult to translate into a usable class as a developer--which is why I'm curious as to the possibilities.

EDIT:

Some responders have suggested that a Bag could be a form of a hashtable internally. This was my initial thought as well, but I foresaw two problems with this idea:

  1. A hashtable is not all that useful when you don't have a suitable hashcode function for the type in question.
  2. Simply tracking an object's "count" in a collection is not the same as storing the object.

As Meta-Knight suggested, perhaps an example would make this more clear:

public class ExpensiveObject() {
    private ExpensiveObject() {
        // very intense operations happening in here
    }

    public ExpensiveObject CreateExpensiveObject() {
        return new ExpensiveObject();
    }
}

static void Main() {
    var expensiveObjects = new ConcurrentBag<ExpensiveObject>();

    for (int i = 0; i < 5; i++) {
        expensiveObjects.Add(ExpensiveObject.CreateExpensiveObject());
    }

    // after this point in the code, I want to believe I have 5 new
    // expensive objects in my collection

    while (expensiveObjects.Count > 0) {
        ExpensiveObject expObj = null;
        bool objectTaken = expensiveObjects.TryTake(out expObj);
        if (objectTaken) {
            // here I THINK I am queueing a particular operation to be
            // executed on 5 separate threads for 5 separate objects,
            // but if ConcurrentBag is a hashtable then I've just received
            // the object 5 times and so I am working on the same object
            // from 5 threads at the same time!
            ThreadPool.QueueUserWorkItem(DoWorkOnExpensiveObject, expObj);
        } else {
            break;
        }
    }
}

static void DoWorkOnExpensiveObject(object obj) {
    ExpensiveObject expObj = obj as ExpensiveObject;
    if (expObj != null) {
        // some work to be done
    }
}
+5  A: 

If you look at the details of ConcurrentBag<T>, you'll find that it's, internally, basically a customized linked list.

Since Bags can contain duplicates, and are not accessible by index, a doubly linked list is a very good option for implementation. This allows locking to be fairly fine grained for insert and removal (you don't have to lock the entire collection, just the nodes around where you're inserting/removing). Since you're not worried about duplicates, no hashing is involved. This makes a double linked list perfect.

Reed Copsey
Good point: I didn't even think about the fact that a bag does not need to do any matching--it just takes objects and returns them. What seemed to me like a truly confounding problem suddenly seems not so puzzling.
Dan Tao
Since some other responders have different answers (though yours makes the most sense to me), I'd be curious to know where you found these details? Also, that's a great point about only having to lock the nodes where insertion/removal are happening... I'd be curious to know, then, is `ConcurrentQueue<T>` actually, on the inside, a `LinkedList<T>` as well? (Otherwise it seems that enqueuing/dequeueing would be needlessly expensive).
Dan Tao
You can use Reflector on System.dll in .NET 4 beta 2 - you'll see the complete implementation of it. It's not actually a LinkedList<T>, but rather an internal ConcurrentBag<T>.ThreadLocalList using a ConcurrentBag<T>.Node - but it's basically a customized double linked list.
Reed Copsey
A: 

Since ordering doesn't matter a ConcurrentBag could be using a hashtable behind the scenes to allow for fast retrieval of data. But unlike a Hashset a bag accepts duplicates. Maybe each item could be paired with a Count property which is set to 1 when an item is added. If you add the same item for a second time, you could just increment the Count property of this item.

Then, to remove an item which has a count greater than one, you could just decrease the Count for this item. If the count was one, you would remove the Item-Count pair from the hashtable.

Meta-Knight
It sounds like you and I had similar ideas, but consider this: first, this would restrict the use of `ConcurrentBag<T>` to types that are suitable to be used as keys. Second, if you simply have a `Count` property on each entry in the `Hashset`, then the object aren't really *in* the bag, which I feel basically defeats the purpose. (So if I have 10 copies of the same `Thing` in my `ConcurrentBag<Thing>`, and I call `TryTake` 10 times, what gets returned after the first time? The same `Thing`? Then I think I have 10 objects but really I have just 1.)
Dan Tao
If two items are considered as duplicates in the ConcurrentBag then aren't they the same? If they're the same isn't it expected that if you call TryTake several times you will get the exact same object? I'm not sure how a ConcurrentBag could work well with "types that aren't suitable to be used as keys"...
Meta-Knight
If you had a concrete example it would help me a lot visualize the problem you mention ;-)
Meta-Knight
@Meta-Knight: I added a pretty detailed example to my question. Let me know what you think.
Dan Tao
A: 

Well, in smalltalk (where the notion of a Bag came from), the collection is basically the same as a hash, albeit one that allows duplicates. Instead of storing the duplicate object though, it maintains an "occurrence count", e.g., a refcount of each object. If ConcurrentBag is a faithful implementation, this should give you a starting point.

Jeff Paquette
A: 

I believe the concept of a 'Bag' is synonymous with 'Multiset'.

There are a number of "Bag"/"Multiset" implementations (these happen to be java) that are open source if you are interested in how they are implemented.

These implementations show that a 'Bag' can be implemented in any number of ways depending on your needs. There are examples of TreeMultiset, HashMultiset, LinkedHashMultiset, ConcurrentHashMultiset.

Google Collections
Google has a number of "MultiSet" implementations, one being a ConcurrentHashMultiset.

Apache Commons
Apache has a number of "Bag" implementations.

Greg Gianforcaro
A: 

Dan-o: Your 5-line comment in your example code makes no sense. Of course you have 5 independant objects in the bag at that point.. the "new" operator in [public ExpensiveObject CreateExpensiveObject()] guarantees that.

Boogaloo
@Boogaloo: Take a look at Meta-Knight's and flyfishr64's responses. They are suggesting that a `Bag` could be implemented as a HashTable with objects as keys and values set to the number of occurrences of the associated key. *If this were the case*, then "adding" an object would be the same as incrementing this value (the # of occurrences) by one, and "removing" an object would return the object and simply decrement this value. You are right that CreateExpensiveObject would guarantee that an object is *created*, but not that it is added to the `Bag`, *if* the implementation were a HashTable.
Dan Tao
mmm.. my mistake. I have not used hashes in the past.. I had assumed that the default hash generator would create a unique hash value per object - which you could override with your own hash generator. Don't mind me. :)
Boogaloo