DictionaryEx.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.Linq;
  7. namespace OpenVIII
  8. {
  9. /// <summary>
  10. /// class to add function to dictionary
  11. /// </summary>
  12. /// <see cref="https://stackoverflow.com/questions/22595655/how-to-do-a-dictionary-reverse-lookup"/>
  13. /// <seealso cref="https://stackoverflow.com/questions/6050633/why-doesnt-dictionary-have-addrange"/>
  14. public static class DictionaryEx
  15. {
  16. #region Methods
  17. public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dic, Dictionary<TKey, TValue> dicToAdd) => dicToAdd.ForEach(x => dic.Add(x.Key, x.Value));
  18. public static void AddRangeNewOnly<TKey, TValue>(this Dictionary<TKey, TValue> dic, Dictionary<TKey, TValue> dicToAdd) => dicToAdd.ForEach(x => { if (!dic.ContainsKey(x.Key)) dic.Add(x.Key, x.Value); });
  19. public static void AddRangeOverride<TKey, TValue>(this Dictionary<TKey, TValue> dic, Dictionary<TKey, TValue> dicToAdd) => dicToAdd.ForEach(x => dic[x.Key] = x.Value);
  20. public static bool ContainsKeys<TKey, TValue>(this Dictionary<TKey, TValue> dic, IEnumerable<TKey> keys)
  21. {
  22. var result = false;
  23. keys.ForEachOrBreak((x) => { result = dic.ContainsKey(x); return result; });
  24. return result;
  25. }
  26. public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
  27. {
  28. foreach (var item in source)
  29. action(item);
  30. }
  31. public static void ForEachOrBreak<T>(this IEnumerable<T> source, Func<T, bool> func)
  32. {
  33. foreach (var item in source)
  34. {
  35. var result = func(item);
  36. if (result) break;
  37. }
  38. }
  39. /// <summary>
  40. /// </summary>
  41. /// <typeparam name="T"></typeparam>
  42. /// <param name="dictionary"></param>
  43. /// <param name="index"></param>
  44. /// <returns></returns>
  45. /// <see cref="https://stackoverflow.com/questions/2229951/how-do-i-get-a-key-from-a-ordereddictionary-in-c-sharp-by-index"/>
  46. public static T GetKey<T>(this OrderedDictionary dictionary, int index)
  47. {
  48. if (dictionary == null)
  49. {
  50. return default;
  51. }
  52. try
  53. {
  54. return (T)dictionary.Cast<DictionaryEntry>().ElementAt(index).Key;
  55. }
  56. catch (Exception)
  57. {
  58. return default;
  59. }
  60. }
  61. /// <summary>
  62. /// Get Value from ordered dictionary
  63. /// </summary>
  64. /// <typeparam name="T">Key type</typeparam>
  65. /// <typeparam name="U">Value type</typeparam>
  66. /// <param name="dictionary"></param>
  67. /// <param name="key"></param>
  68. /// <returns></returns>
  69. /// <see cref="https://stackoverflow.com/questions/2229951/how-do-i-get-a-key-from-a-ordereddictionary-in-c-sharp-by-index"/>
  70. public static U GetValue<T, U>(this OrderedDictionary dictionary, T key)
  71. {
  72. if (dictionary == null)
  73. {
  74. return default;
  75. }
  76. try
  77. {
  78. return (U)dictionary.Cast<DictionaryEntry>().AsQueryable().Single(kvp => ((T)kvp.Key).Equals(key)).Value;
  79. }
  80. catch (Exception)
  81. {
  82. return default;
  83. }
  84. }
  85. /// <summary>
  86. /// Reverses Key and Value of dictionary.
  87. /// </summary>
  88. /// <typeparam name="TKey"></typeparam>
  89. /// <typeparam name="TValue"></typeparam>
  90. /// <param name="source"></param>
  91. /// <returns></returns>
  92. public static Dictionary<TValue, TKey> Reverse<TKey, TValue>(this IDictionary<TKey, TValue> source)
  93. {
  94. var dictionary = new Dictionary<TValue, TKey>();
  95. foreach (var entry in source)
  96. {
  97. if (!dictionary.ContainsKey(entry.Value))
  98. dictionary.Add(entry.Value, entry.Key);
  99. }
  100. return dictionary;
  101. }
  102. public static Vector3 ToVector3(this Vector2 v, float z = 0f)
  103. => new Vector3(v.X, v.Y, z);
  104. #endregion Methods
  105. }
  106. //public class _OrderedDictionay<T, U> : OrderedDictionary
  107. //{
  108. // public _OrderedDictionay(int capacity) : base(capacity)
  109. // {
  110. // }
  111. // #region Indexers
  112. // public new KeyValuePair<T, U> this[int index]
  113. // {
  114. // get
  115. // {
  116. // T key = this.GetKey<T>(index);
  117. // U val = this.GetValue<T, U>(key);
  118. // return new KeyValuePair<T, U>(key, val);
  119. // }
  120. // }
  121. // public U this[T key] => this.GetValue<T, U>(key);
  122. // #endregion Indexers
  123. //}
  124. }