ListDictionary.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections.Generic;
  2. namespace PixiEditor.SDK
  3. {
  4. internal class ListDictionary<TKey, TValue>
  5. {
  6. private readonly Dictionary<TKey, List<TValue>> dictionary;
  7. public ListDictionary()
  8. {
  9. dictionary = new();
  10. }
  11. public ListDictionary(ListDictionary<TKey, TValue> listDictionary)
  12. {
  13. dictionary = new Dictionary<TKey, List<TValue>>(listDictionary.dictionary);
  14. }
  15. public ListDictionary(IEnumerable<KeyValuePair<TKey, IEnumerable<TValue>>> enumerable)
  16. {
  17. dictionary = new();
  18. foreach (var value in enumerable)
  19. {
  20. dictionary.Add(value.Key, new List<TValue>(value.Value));
  21. }
  22. }
  23. public List<TValue> this[TKey key]
  24. {
  25. get => dictionary[key];
  26. set => dictionary[key] = value;
  27. }
  28. public void Add(TKey key, TValue value)
  29. {
  30. if (dictionary.ContainsKey(key))
  31. {
  32. dictionary[key].Add(value);
  33. return;
  34. }
  35. dictionary.Add(key, new() { value });
  36. }
  37. public void Clear(TKey key) => this[key].Clear();
  38. public bool ContainsKey(TKey key) => dictionary.ContainsKey(key);
  39. public bool ContainsValue(TKey key) => this[key].Count != 0;
  40. public int LenghtOf(TKey key) => this[key].Count;
  41. }
  42. }