Advertisement
digemall

Dictionary sorted by value

Aug 17th, 2011
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1.     public class DictionarySortedByValue<TK, TV> : IDictionary<TK, TV>
  2.     {
  3.         #region Nested Class
  4.         class KeyValuePairByValueComparer : IComparer<KeyValuePair<TK, TV>>
  5.         {
  6.             private IComparer<TK> keyComparer;
  7.             private IComparer<TV> valueComparer;
  8.             public KeyValuePairByValueComparer(IComparer<TK> keyComparer, IComparer<TV> valueComparer)
  9.             {
  10.                 this.keyComparer = keyComparer;
  11.                 this.valueComparer = valueComparer;
  12.             }
  13.             public int Compare(KeyValuePair<TK, TV> x, KeyValuePair<TK, TV> y)
  14.             {
  15.                 int c = this.valueComparer.Compare(x.Value, y.Value);
  16.                 if (c == 0)
  17.                     c = this.keyComparer.Compare(x.Key, y.Key);
  18.                 return c;
  19.             }
  20.         }
  21.         #endregion
  22.  
  23.         #region Private Fields
  24.         private SortedSet<KeyValuePair<TK, TV>> sortedCollection;
  25.         private IDictionary<TK, TV> dictionary;
  26.         #endregion
  27.  
  28.         #region CTOR
  29.         public DictionarySortedByValue()
  30.         {
  31.             this.sortedCollection = new SortedSet<KeyValuePair<TK, TV>>(new KeyValuePairByValueComparer(Comparer<TK>.Default, Comparer<TV>.Default));
  32.             this.dictionary = new Dictionary<TK, TV>();
  33.         }
  34.         #endregion
  35.  
  36.         #region IDictionary implementation
  37.         public void Add(TK key, TV value)
  38.         {
  39.             this.Add(new KeyValuePair<TK, TV>(key, value));
  40.         }
  41.         public bool ContainsKey(TK key)
  42.         {
  43.             return dictionary.ContainsKey(key);
  44.         }
  45.         // can be improved implementing a custom ICollection avoiding the List instantiation
  46.         public ICollection<TK> Keys
  47.         {
  48.             get { return this.sortedCollection.Select(x => x.Key).ToList(); }
  49.         }
  50.         public bool Remove(TK key)
  51.         {
  52.             TV val;
  53.             if (!this.dictionary.TryGetValue(key, out val))
  54.                 return false;
  55.             this.dictionary.Remove(key);
  56.             this.sortedCollection.Remove(new KeyValuePair<TK, TV>(key, val));
  57.             return true;
  58.         }
  59.         public bool TryGetValue(TK key, out TV value)
  60.         {
  61.             return this.dictionary.TryGetValue(key, out value);
  62.         }
  63.         // can be improved implementing a custom ICollection avoiding the List instantiation
  64.         public ICollection<TV> Values
  65.         {
  66.             get { return this.sortedCollection.Select(x => x.Value).ToList(); }
  67.         }
  68.         public TV this[TK key]
  69.         {
  70.             get
  71.             {
  72.                 return this.dictionary[key];
  73.             }
  74.             set
  75.             {
  76.                 TV oldVal;
  77.                 if (this.dictionary.TryGetValue(key, out oldVal))
  78.                 {
  79.                     this.dictionary[key] = value;
  80.                     this.sortedCollection.Remove(new KeyValuePair<TK, TV>(key, oldVal));
  81.                 }
  82.                 else
  83.                 {
  84.                     this.dictionary.Add(key, value);
  85.                 }
  86.                 this.sortedCollection.Add(new KeyValuePair<TK, TV>(key, value));
  87.             }
  88.         }
  89.         public void Add(KeyValuePair<TK, TV> item)
  90.         {
  91.             sortedCollection.Add(item);
  92.             dictionary.Add(item);
  93.         }
  94.         public void Clear()
  95.         {
  96.             this.dictionary.Clear();
  97.             this.sortedCollection.Clear();
  98.         }
  99.         public bool Contains(KeyValuePair<TK, TV> item)
  100.         {
  101.             return this.sortedCollection.Contains(item);
  102.         }
  103.         public void CopyTo(KeyValuePair<TK, TV>[] array, int arrayIndex)
  104.         {
  105.             this.sortedCollection.CopyTo(array, arrayIndex);
  106.         }
  107.         public int Count
  108.         {
  109.             get { return this.sortedCollection.Count; }
  110.         }
  111.         public bool IsReadOnly
  112.         {
  113.             get { return false; }
  114.         }
  115.         public bool Remove(KeyValuePair<TK, TV> item)
  116.         {
  117.             this.dictionary.Remove(item.Key);
  118.             return this.sortedCollection.Remove(item);
  119.         }
  120.         public IEnumerator<KeyValuePair<TK, TV>> GetEnumerator()
  121.         {
  122.             return this.sortedCollection.GetEnumerator();
  123.         }
  124.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  125.         {
  126.             return this.GetEnumerator();
  127.         }
  128.         #endregion
  129.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement