Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DictionarySortedByValue<TK, TV> : IDictionary<TK, TV>
- {
- #region Nested Class
- class KeyValuePairByValueComparer : IComparer<KeyValuePair<TK, TV>>
- {
- private IComparer<TK> keyComparer;
- private IComparer<TV> valueComparer;
- public KeyValuePairByValueComparer(IComparer<TK> keyComparer, IComparer<TV> valueComparer)
- {
- this.keyComparer = keyComparer;
- this.valueComparer = valueComparer;
- }
- public int Compare(KeyValuePair<TK, TV> x, KeyValuePair<TK, TV> y)
- {
- int c = this.valueComparer.Compare(x.Value, y.Value);
- if (c == 0)
- c = this.keyComparer.Compare(x.Key, y.Key);
- return c;
- }
- }
- #endregion
- #region Private Fields
- private SortedSet<KeyValuePair<TK, TV>> sortedCollection;
- private IDictionary<TK, TV> dictionary;
- #endregion
- #region CTOR
- public DictionarySortedByValue()
- {
- this.sortedCollection = new SortedSet<KeyValuePair<TK, TV>>(new KeyValuePairByValueComparer(Comparer<TK>.Default, Comparer<TV>.Default));
- this.dictionary = new Dictionary<TK, TV>();
- }
- #endregion
- #region IDictionary implementation
- public void Add(TK key, TV value)
- {
- this.Add(new KeyValuePair<TK, TV>(key, value));
- }
- public bool ContainsKey(TK key)
- {
- return dictionary.ContainsKey(key);
- }
- // can be improved implementing a custom ICollection avoiding the List instantiation
- public ICollection<TK> Keys
- {
- get { return this.sortedCollection.Select(x => x.Key).ToList(); }
- }
- public bool Remove(TK key)
- {
- TV val;
- if (!this.dictionary.TryGetValue(key, out val))
- return false;
- this.dictionary.Remove(key);
- this.sortedCollection.Remove(new KeyValuePair<TK, TV>(key, val));
- return true;
- }
- public bool TryGetValue(TK key, out TV value)
- {
- return this.dictionary.TryGetValue(key, out value);
- }
- // can be improved implementing a custom ICollection avoiding the List instantiation
- public ICollection<TV> Values
- {
- get { return this.sortedCollection.Select(x => x.Value).ToList(); }
- }
- public TV this[TK key]
- {
- get
- {
- return this.dictionary[key];
- }
- set
- {
- TV oldVal;
- if (this.dictionary.TryGetValue(key, out oldVal))
- {
- this.dictionary[key] = value;
- this.sortedCollection.Remove(new KeyValuePair<TK, TV>(key, oldVal));
- }
- else
- {
- this.dictionary.Add(key, value);
- }
- this.sortedCollection.Add(new KeyValuePair<TK, TV>(key, value));
- }
- }
- public void Add(KeyValuePair<TK, TV> item)
- {
- sortedCollection.Add(item);
- dictionary.Add(item);
- }
- public void Clear()
- {
- this.dictionary.Clear();
- this.sortedCollection.Clear();
- }
- public bool Contains(KeyValuePair<TK, TV> item)
- {
- return this.sortedCollection.Contains(item);
- }
- public void CopyTo(KeyValuePair<TK, TV>[] array, int arrayIndex)
- {
- this.sortedCollection.CopyTo(array, arrayIndex);
- }
- public int Count
- {
- get { return this.sortedCollection.Count; }
- }
- public bool IsReadOnly
- {
- get { return false; }
- }
- public bool Remove(KeyValuePair<TK, TV> item)
- {
- this.dictionary.Remove(item.Key);
- return this.sortedCollection.Remove(item);
- }
- public IEnumerator<KeyValuePair<TK, TV>> GetEnumerator()
- {
- return this.sortedCollection.GetEnumerator();
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement