How might a class like .NET's ConcurrentBag be implemented?

后端 未结 5 885
夕颜
夕颜 2021-02-07 21:49

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

Bags are useful for storing objects wh

5条回答
  •  不要未来只要你来
    2021-02-07 22:49

    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.

提交回复
热议问题