Created
December 6, 2019 01:43
-
-
Save Banane9/be2b136e680aafe614c5d2a682e2441f to your computer and use it in GitHub Desktop.
IDictionary that keeps each key added to it instead of overwriting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ExpandoDictionary | |
{ | |
public sealed class ExpandoDictionary<TKey, TValue> : IDictionary<TKey, TValue> | |
{ | |
private readonly Dictionary<TKey, List<TValue>> content = new Dictionary<TKey, List<TValue>>(); | |
public int Count { get; } | |
public bool IsReadOnly { get; } | |
public ICollection<TKey> Keys | |
{ | |
get { return content.Keys; } | |
} | |
public TValue this[TKey key] | |
{ | |
get { throw new NotSupportedException("Suck it, bitch!"); } | |
set { Add(key, value); } | |
} | |
public ICollection<TValue> Values | |
{ | |
get { return content.Values.SelectMany(i => i).ToList(); } | |
} | |
public void Add(TKey key, TValue value) | |
{ | |
if (!content.ContainsKey(key)) | |
content.Add(key, new List<TValue>()); | |
content[key].Add(value); | |
} | |
public void Add(KeyValuePair<TKey, TValue> item) | |
{ | |
Add(item.Key, item.Value); | |
} | |
public void Clear() | |
{ | |
content.Clear(); | |
} | |
public bool Contains(KeyValuePair<TKey, TValue> item) | |
{ | |
return content.ContainsKey(item.Key) && content[item.Key].Contains(item.Value); | |
} | |
public bool ContainsKey(TKey key) | |
{ | |
return content.ContainsKey(key); | |
} | |
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) | |
{ | |
throw new NotSupportedException("Meh"); | |
} | |
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() | |
{ | |
return content.Keys.SelectMany(key => content[key].Select(val => new KeyValuePair<TKey, TValue>(key, val))).GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public bool Remove(TKey key) | |
{ | |
return content.Remove(key); | |
} | |
public bool Remove(KeyValuePair<TKey, TValue> item) | |
{ | |
return content[item.Key].Remove(item.Value); | |
} | |
public bool TryGetValue(TKey key, out TValue value) | |
{ | |
throw new NotSupportedException("Meh"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment