Want more features on Pastebin? Sign Up, it's FREE!
Guest

ConcurrentSet

By: coder0xff on Dec 9th, 2012  |  syntax: C#  |  size: 2.94 KB  |  views: 413  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. namespace System.Collections.Concurrent
  5. {
  6.     public class ConcurrentSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable
  7.     {
  8.         ConcurrentDictionary<T, byte> storage;
  9.  
  10.         public ConcurrentSet()
  11.         {
  12.             storage = new ConcurrentDictionary<T, byte>();
  13.         }
  14.  
  15.         public ConcurrentSet(IEnumerable<T> collection)
  16.         {
  17.             storage = new ConcurrentDictionary<T, byte>(collection.Select(_ => new KeyValuePair<T, byte>(_, 0)));
  18.         }
  19.  
  20.         public ConcurrentSet(IEqualityComparer<T> comparer)
  21.         {
  22.             storage = new ConcurrentDictionary<T, byte>(comparer);
  23.         }
  24.  
  25.         public ConcurrentSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
  26.         {
  27.             storage = new ConcurrentDictionary<T, byte>(collection.Select(_ => new KeyValuePair<T, byte>(_, 0)), comparer);
  28.         }
  29.  
  30.         public ConcurrentSet(int concurrencyLevel, int capacity)
  31.         {
  32.             storage = new ConcurrentDictionary<T, byte>(concurrencyLevel, capacity);
  33.         }
  34.  
  35.         public ConcurrentSet(int concurrencyLevel, IEnumerable<T> collection, IEqualityComparer<T> comparer)
  36.         {
  37.             storage = new ConcurrentDictionary<T, byte>(concurrencyLevel, collection.Select(_ => new KeyValuePair<T, byte>(_, 0)), comparer);
  38.         }
  39.  
  40.         public ConcurrentSet(int concurrencyLevel, int capacity, IEqualityComparer<T> comparer)
  41.         {
  42.             storage = new ConcurrentDictionary<T, byte>(concurrencyLevel, capacity, comparer);
  43.         }
  44.  
  45.         public int Count { get { return storage.Count; } }
  46.  
  47.         public bool IsEmptry { get { return storage.IsEmpty; } }
  48.  
  49.         public void Clear()
  50.         {
  51.             storage.Clear();
  52.         }
  53.  
  54.         public bool Contains(T item)
  55.         {
  56.             return storage.ContainsKey(item);
  57.         }
  58.  
  59.         public bool TryAdd(T item)
  60.         {
  61.             return storage.TryAdd(item, 0);
  62.         }
  63.  
  64.         public bool TryRemove(T item)
  65.         {
  66.             byte dontCare;
  67.             return storage.TryRemove(item, out dontCare);
  68.         }
  69.  
  70.         void ICollection<T>.Add(T item)
  71.         {
  72.             ((ICollection<KeyValuePair<T, byte>>)storage).Add(new KeyValuePair<T, byte>(item, 0));
  73.         }
  74.  
  75.         void ICollection<T>.CopyTo(T[] array, int arrayIndex)
  76.         {
  77.             foreach (KeyValuePair<T, byte> pair in storage)
  78.                 array[arrayIndex++] = pair.Key;
  79.         }
  80.  
  81.         bool ICollection<T>.IsReadOnly
  82.         {
  83.             get { return false; }
  84.         }
  85.  
  86.         bool ICollection<T>.Remove(T item)
  87.         {
  88.             return TryRemove(item);
  89.         }
  90.  
  91.         IEnumerator<T> IEnumerable<T>.GetEnumerator()
  92.         {
  93.             return storage.Keys.GetEnumerator();
  94.         }
  95.  
  96.         IEnumerator IEnumerable.GetEnumerator()
  97.         {
  98.             return storage.Keys.GetEnumerator();
  99.         }
  100.     }
  101. }
clone this paste RAW Paste Data